PageRenderTime 46ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/src/jogl/demos/GLNewtRun.java

http://jzy3d.googlecode.com/
Java | 250 lines | 223 code | 26 blank | 1 comment | 52 complexity | 92b702944e3e0ec7c407868e96d8d6ba MD5 | raw file
Possible License(s): BSD-3-Clause
  1. package demos;
  2. import java.lang.reflect.*;
  3. import javax.media.opengl.*;
  4. import javax.media.nativewindow.*;
  5. import com.jogamp.newt.*;
  6. import com.jogamp.newt.event.*;
  7. import com.jogamp.newt.opengl.*;
  8. public class GLNewtRun extends WindowAdapter implements KeyListener, MouseListener {
  9. static GLWindow window;
  10. static volatile boolean quit = false;
  11. public void windowDestroyNotify(WindowEvent e) {
  12. quit = true;
  13. }
  14. static int dx=0;
  15. static int dy=0;
  16. static int dw=0;
  17. static int dh=0;
  18. public void keyPressed(KeyEvent e) {
  19. System.out.println(e);
  20. if(e.getKeyChar()=='f') {
  21. window.setFullscreen(!window.isFullscreen());
  22. } else if(e.getKeyChar()=='q') {
  23. quit = true;
  24. } else if(e.getKeyChar()=='p') {
  25. int x = window.getX() + dx;
  26. int y = window.getY() + dy;
  27. System.out.println("Reset Pos "+x+"/"+y);
  28. window.setPosition(x, y);
  29. } else if(e.getKeyChar()=='s') {
  30. int w = window.getWidth() + dw;
  31. int h = window.getHeight() + dh;
  32. System.out.println("Reset Size "+w+"x"+h);
  33. window.setSize(w, h);
  34. }
  35. }
  36. public void keyReleased(KeyEvent e) {
  37. System.out.println(e);
  38. }
  39. public void keyTyped(KeyEvent e) {
  40. System.out.println(e);
  41. }
  42. public void mouseClicked(MouseEvent e) {
  43. System.out.println(" mouseevent: "+e);
  44. switch(e.getClickCount()) {
  45. case 1:
  46. if(e.getButton()>MouseEvent.BUTTON1) {
  47. window.setFullscreen(!window.isFullscreen());
  48. }
  49. break;
  50. default:
  51. quit=true;
  52. break;
  53. }
  54. }
  55. public void mouseEntered(MouseEvent e) {
  56. }
  57. public void mouseExited(MouseEvent e) {
  58. }
  59. public void mousePressed(MouseEvent e) {
  60. }
  61. public void mouseReleased(MouseEvent e) {
  62. }
  63. public void mouseMoved(MouseEvent e) {
  64. }
  65. public void mouseDragged(MouseEvent e) {
  66. }
  67. public void mouseWheelMoved(MouseEvent e) {
  68. }
  69. public boolean shouldQuit() { return quit; }
  70. public static int str2int(String str, int def) {
  71. try {
  72. return Integer.parseInt(str);
  73. } catch (Exception ex) { ex.printStackTrace(); }
  74. return def;
  75. }
  76. public static boolean setField(Object instance, String fieldName, Object value) {
  77. try {
  78. Field f = instance.getClass().getField(fieldName);
  79. if(f.getType().isInstance(value)) {
  80. f.set(instance, value);
  81. return true;
  82. } else {
  83. System.out.println(instance.getClass()+" '"+fieldName+"' field not assignable with "+value.getClass()+", it's a: "+f.getType());
  84. }
  85. } catch (NoSuchFieldException nsfe) {
  86. System.out.println(instance.getClass()+" has no '"+fieldName+"' field");
  87. } catch (Throwable t) {
  88. t.printStackTrace();
  89. }
  90. return false;
  91. }
  92. public static void main(String[] args) {
  93. boolean parented = false;
  94. boolean useAWTTestFrame = false;
  95. boolean useAWT = false;
  96. boolean undecorated = false;
  97. boolean fullscreen = false;
  98. int x_p = 0;
  99. int y_p = 0;
  100. int x = 0;
  101. int y = 0;
  102. int width = 800;
  103. int height = 480;
  104. String glProfileStr = null;
  105. if(0==args.length) {
  106. throw new RuntimeException("Usage: "+GLNewtRun.class+" <demo class name (GLEventListener)>");
  107. }
  108. GLNewtRun listener = new GLNewtRun();
  109. int i=0;
  110. while(i<args.length-1) {
  111. if(args[i].equals("-awt")) {
  112. useAWT = true;
  113. } else if(args[i].equals("-awttestframe")) {
  114. useAWT = true;
  115. useAWTTestFrame = true;
  116. } else if(args[i].equals("-undecorated")) {
  117. undecorated = true;
  118. } else if(args[i].equals("-parented")) {
  119. parented = true;
  120. } else if(args[i].equals("-fs")) {
  121. fullscreen = true;
  122. } else if(args[i].equals("-xp")) {
  123. i++;
  124. x_p = str2int(args[i], x_p);
  125. } else if(args[i].equals("-yp")) {
  126. i++;
  127. y_p = str2int(args[i], y_p);
  128. } else if(args[i].equals("-x")) {
  129. i++;
  130. x = str2int(args[i], x);
  131. } else if(args[i].equals("-y")) {
  132. i++;
  133. y = str2int(args[i], y);
  134. } else if(args[i].equals("-width")) {
  135. i++;
  136. width = str2int(args[i], width);
  137. } else if(args[i].equals("-height")) {
  138. i++;
  139. height = str2int(args[i], height);
  140. } else if(args[i].startsWith("-GL")) {
  141. glProfileStr = args[i].substring(1);
  142. } else if(args[i].equals("-dx")) {
  143. i++;
  144. dx = str2int(args[i], dx);
  145. } else if(args[i].equals("-dy")) {
  146. i++;
  147. dy = str2int(args[i], dy);
  148. } else if(args[i].equals("-dw")) {
  149. i++;
  150. dw = str2int(args[i], dw);
  151. } else if(args[i].equals("-dh")) {
  152. i++;
  153. dh = str2int(args[i], dh);
  154. }
  155. i++;
  156. }
  157. String demoClassName = args[i];
  158. Object demoObject = null;
  159. try {
  160. Class demoClazz = Class.forName(demoClassName);
  161. demoObject = demoClazz.newInstance();
  162. } catch (Throwable t) {
  163. t.printStackTrace();
  164. throw new RuntimeException("Error while instantiating demo: "+demoClassName);
  165. }
  166. if( !(demoObject instanceof GLEventListener) ) {
  167. throw new RuntimeException("Not a GLEventListener: "+demoClassName);
  168. }
  169. GLEventListener demo = (GLEventListener) demoObject;
  170. GLProfile glp = GLProfile.get(glProfileStr);
  171. try {
  172. GLCapabilities caps = new GLCapabilities(glp);
  173. NewtFactory.setUseEDT(true);
  174. Window nWindow = null;
  175. if(useAWT) {
  176. Display nDisplay = NewtFactory.createDisplay(NativeWindowFactory.TYPE_AWT, null); // local display
  177. Screen nScreen = NewtFactory.createScreen(NativeWindowFactory.TYPE_AWT, nDisplay, 0); // screen 0
  178. if(useAWTTestFrame) {
  179. java.awt.MenuBar menuTest = new java.awt.MenuBar();
  180. menuTest.add(new java.awt.Menu("External Frame Test - Menu"));
  181. java.awt.Frame frame = new java.awt.Frame("External Frame Test");
  182. frame.setMenuBar(menuTest);
  183. nWindow = NewtFactory.createWindow(NativeWindowFactory.TYPE_AWT, new Object[] { frame }, nScreen, caps);
  184. } else {
  185. nWindow = NewtFactory.createWindow(NativeWindowFactory.TYPE_AWT, nScreen, caps);
  186. }
  187. } else {
  188. Display nDisplay = NewtFactory.createDisplay(null); // local display
  189. Screen nScreen = NewtFactory.createScreen(nDisplay, 0); // screen 0
  190. if(parented) {
  191. Window parent = NewtFactory.createWindow(nScreen, caps);
  192. parent.setPosition(x_p, y_p);
  193. parent.setSize(width+width/10, height+height/10);
  194. parent.setVisible(true);
  195. nWindow = NewtFactory.createWindow(parent, caps);
  196. } else {
  197. nWindow = NewtFactory.createWindow(nScreen, caps);
  198. }
  199. }
  200. nWindow.setUndecorated(undecorated);
  201. nWindow.getScreen().setDestroyWhenUnused(true);
  202. window = GLWindow.create(nWindow);
  203. if(!setField(demo, "window", window)) {
  204. setField(demo, "glWindow", window);
  205. }
  206. window.addWindowListener(listener);
  207. window.addMouseListener(listener);
  208. window.addKeyListener(listener);
  209. window.addGLEventListener(demo);
  210. window.setPosition(x, y);
  211. window.setSize(width, height);
  212. window.setFullscreen(fullscreen);
  213. // Size OpenGL to Video Surface
  214. window.setVisible(true);
  215. window.enablePerfLog(true);
  216. do {
  217. window.display();
  218. } while (!quit && window.getDuration() < 20000) ;
  219. window.destroy();
  220. } catch (Throwable t) {
  221. t.printStackTrace();
  222. }
  223. }
  224. }