summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorSven Gothel <[email protected]>2009-09-14 06:12:33 -0700
committerSven Gothel <[email protected]>2009-09-14 06:12:33 -0700
commite7003e596519e225ecec5b4423314c4dcaa05db8 (patch)
tree86fe34ddea1f6b3e6a76bcd64453c6572718b49c /src
parent2166980a1d7f4b107571a5bdcff648718b21662f (diff)
NEWT Applet work ..
Diffstat (limited to 'src')
-rwxr-xr-xsrc/demos/GLNewtRun.java2
-rwxr-xr-xsrc/demos/applets/JOGLNewtApplet1Run.java80
-rwxr-xr-xsrc/demos/applets/JOGLNewtAppletBase.java256
-rwxr-xr-xsrc/demos/applets/RedSquareNEWTAWTApplet.java33
4 files changed, 337 insertions, 34 deletions
diff --git a/src/demos/GLNewtRun.java b/src/demos/GLNewtRun.java
index 2e54e8b..9e31d6b 100755
--- a/src/demos/GLNewtRun.java
+++ b/src/demos/GLNewtRun.java
@@ -142,7 +142,7 @@ public class GLNewtRun implements WindowListener, KeyListener, MouseListener {
demoObject = demoClazz.newInstance();
} catch (Throwable t) {
t.printStackTrace();
- throw new RuntimeException("Error while instaniating demo: "+demoClassName);
+ throw new RuntimeException("Error while instantiating demo: "+demoClassName);
}
if( !(demoObject instanceof GLEventListener) ) {
throw new RuntimeException("Not a GLEventListener: "+demoClassName);
diff --git a/src/demos/applets/JOGLNewtApplet1Run.java b/src/demos/applets/JOGLNewtApplet1Run.java
new file mode 100755
index 0000000..08522de
--- /dev/null
+++ b/src/demos/applets/JOGLNewtApplet1Run.java
@@ -0,0 +1,80 @@
+package demos.applets;
+
+import java.applet.*;
+import java.awt.Container;
+
+import javax.media.opengl.*;
+import javax.media.nativewindow.*;
+import com.sun.javafx.newt.*;
+import com.sun.javafx.newt.opengl.*;
+
+/** Shows how to deploy an applet using JOGL. This demo must be
+ referenced from a web page via an &lt;applet&gt; tag. */
+
+public class JOGLNewtApplet1Run extends Applet {
+ JOGLNewtAppletBase base;
+
+ public void init() {
+ if(!(this instanceof Container)) {
+ throw new RuntimeException("This Applet is not a AWT Container");
+ }
+ Container container = (Container) this; // have to think about that, we may use a Container
+
+ String glEventListenerClazzName=null;
+ String glProfileName=null;
+ int glSwapInterval=0;
+ boolean handleWindowEvents=true;
+ boolean useGLInEventHandler=false;
+ boolean glDebug=false;
+ boolean glTrace=false;
+ String tmp;
+ try {
+ glEventListenerClazzName = getParameter("gl_event_listener_class");
+ glProfileName = getParameter("gl_profile");
+ glSwapInterval = JOGLNewtAppletBase.str2Int(getParameter("gl_swap_interval"), glSwapInterval);
+ useGLInEventHandler = JOGLNewtAppletBase.str2Bool(getParameter("gl_use_in_events"), useGLInEventHandler);
+ glDebug = JOGLNewtAppletBase.str2Bool(getParameter("gl_debug"), glDebug);
+ glTrace = JOGLNewtAppletBase.str2Bool(getParameter("gl_trace"), glTrace);
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ if(null==glEventListenerClazzName) {
+ throw new RuntimeException("No applet parameter 'gl_event_listener_class'");
+ }
+ base = new JOGLNewtAppletBase(glEventListenerClazzName,
+ glProfileName,
+ glSwapInterval,
+ handleWindowEvents,
+ useGLInEventHandler,
+ glDebug,
+ glTrace);
+
+ try {
+ GLCapabilities caps = new GLCapabilities(GLProfile.get(glProfileName));
+ Display nDisplay = NewtFactory.createDisplay(NativeWindowFactory.TYPE_AWT, null); // local display
+ Screen nScreen = NewtFactory.createScreen(NativeWindowFactory.TYPE_AWT, nDisplay, 0); // screen 0
+ Window nWindow = NewtFactory.createWindow(NativeWindowFactory.TYPE_AWT, new Object[] { container },
+ nScreen, caps, true /* undecorated */);
+ // nWindow.setPosition(x, y);
+ // nWindow.setSize(width, height);
+ if(null!=nWindow) {
+ base.init(nWindow);
+ }
+ } catch (Throwable t) {
+ throw new RuntimeException(t);
+ }
+ }
+
+ public void start() {
+ base.start();
+ }
+
+ public void stop() {
+ base.stop();
+ }
+
+ public void destroy() {
+ base.destroy();
+ }
+}
+
diff --git a/src/demos/applets/JOGLNewtAppletBase.java b/src/demos/applets/JOGLNewtAppletBase.java
new file mode 100755
index 0000000..a1ea482
--- /dev/null
+++ b/src/demos/applets/JOGLNewtAppletBase.java
@@ -0,0 +1,256 @@
+package demos.applets;
+
+import java.util.*;
+import java.lang.reflect.*;
+
+import com.sun.javafx.newt.*;
+import com.sun.javafx.newt.opengl.GLWindow;
+
+import javax.media.opengl.*;
+import com.sun.opengl.util.*;
+
+/** Shows how to deploy an applet using JOGL. This demo must be
+ referenced from a web page via an &lt;applet&gt; tag. */
+
+public class JOGLNewtAppletBase implements WindowListener, KeyListener, MouseListener, GLEventListener {
+ String glEventListenerClazzName;
+ String glProfileName;
+ int glSwapInterval;
+ boolean handleWindowEvents;
+ boolean useGLInEventHandler;
+ boolean glDebug;
+ boolean glTrace;
+
+ GLWindow glWindow = null;
+ Animator glAnimator=null;
+ boolean isValid = false;
+ boolean quit = false;
+
+ public JOGLNewtAppletBase(String glEventListenerClazzName,
+ String glProfileName,
+ int glSwapInterval,
+ boolean handleWindowEvents,
+ boolean useGLInEventHandler,
+ boolean glDebug,
+ boolean glTrace) {
+
+ this.glEventListenerClazzName=glEventListenerClazzName;
+ this.glProfileName=glProfileName;
+ this.glSwapInterval=glSwapInterval;
+ this.handleWindowEvents=handleWindowEvents;
+ this.useGLInEventHandler=useGLInEventHandler;
+ this.glDebug = glDebug;
+ this.glTrace = glTrace;
+ }
+
+ public GLWindow getGLWindow() { return glWindow; }
+ public Animator getGLAnimator() { return glAnimator; }
+ public boolean isValid() { return isValid; }
+ public boolean shouldQuit() { return quit; }
+
+ public static boolean str2Bool(String str, boolean def) {
+ if(null==str) return def;
+ try {
+ return Boolean.valueOf(str).booleanValue();
+ } catch (Exception ex) { ex.printStackTrace(); }
+ return def;
+ }
+
+ public static int str2Int(String str, int def) {
+ if(null==str) return def;
+ try {
+ return Integer.parseInt(str);
+ } catch (Exception ex) { ex.printStackTrace(); }
+ return def;
+ }
+
+ public static GLEventListener createInstance(String clazzName) {
+ Object instance = null;
+
+ try {
+ Class clazz = Class.forName(clazzName);
+ instance = clazz.newInstance();
+ } catch (Throwable t) {
+ t.printStackTrace();
+ throw new RuntimeException("Error while instantiating demo: "+clazzName);
+ }
+ if( null == instance ) {
+ throw new RuntimeException("Null GLEventListener: "+clazzName);
+ }
+ if( !(instance instanceof GLEventListener) ) {
+ throw new RuntimeException("Not a GLEventListener: "+clazzName);
+ }
+ return (GLEventListener) instance;
+ }
+
+ public static boolean setField(Object instance, String fieldName, Object value) {
+ try {
+ Field f = instance.getClass().getField(fieldName);
+ if(f.getType().isInstance(value)) {
+ f.set(instance, value);
+ return true;
+ } else {
+ System.out.println(instance.getClass()+" '"+fieldName+"' field not assignable with "+value.getClass()+", it's a: "+f.getType());
+ }
+ } catch (NoSuchFieldException nsfe) {
+ System.out.println(instance.getClass()+" has no '"+fieldName+"' field");
+ } catch (Throwable t) {
+ t.printStackTrace();
+ }
+ return false;
+ }
+
+ public void init(Window nWindow) {
+ GLEventListener glEventListener = createInstance(glEventListenerClazzName);
+
+ try {
+ glWindow = GLWindow.create(nWindow);
+
+ if(!setField(glEventListener, "window", glWindow)) {
+ setField(glEventListener, "glWindow", glWindow);
+ }
+
+ glWindow.addGLEventListener(this);
+ glWindow.addGLEventListener(glEventListener);
+ glWindow.addWindowListener(this);
+ glWindow.addMouseListener(this);
+ glWindow.addKeyListener(this);
+ glWindow.setEventHandlerMode( useGLInEventHandler ? GLWindow.EVENT_HANDLER_GL_CURRENT : GLWindow.EVENT_HANDLER_GL_NONE );
+ glWindow.setRunPumpMessages(handleWindowEvents);
+ glWindow.setVisible(true);
+ glWindow.enablePerfLog(true);
+
+ // glAnimator = new FPSAnimator(canvas, 60);
+ glAnimator = new Animator(glWindow);
+ } catch (Throwable t) {
+ throw new RuntimeException(t);
+ }
+ isValid = true;
+ }
+
+ public void start() {
+ if(isValid) {
+ glAnimator.start();
+ }
+ }
+
+ public void stop() {
+ if(null!=glAnimator) {
+ glAnimator.stop();
+ }
+ }
+
+ public void destroy() {
+ quit = true;
+ isValid = false;
+ if(null!=glAnimator) {
+ glAnimator.stop();
+ glAnimator.remove(glWindow);
+ glAnimator=null;
+ }
+ if(null!=glWindow) {
+ glWindow.destroy();
+ glWindow=null;
+ }
+ }
+
+ // ***********************************************************************************
+ // ***********************************************************************************
+ // ***********************************************************************************
+
+ public void init(GLAutoDrawable drawable) {
+ GL _gl = drawable.getGL();
+
+ if(glDebug) {
+ try {
+ _gl = _gl.getContext().setGL( GLPipelineFactory.create("javax.media.opengl.Debug", null, _gl, null) );
+ } catch (Exception e) {e.printStackTrace();}
+ }
+
+ if(glTrace) {
+ try {
+ // Trace ..
+ _gl = _gl.getContext().setGL( GLPipelineFactory.create("javax.media.opengl.Trace", null, _gl, new Object[] { System.err } ) );
+ } catch (Exception e) {e.printStackTrace();}
+ }
+
+ if(glSwapInterval>=0) {
+ _gl.setSwapInterval(glSwapInterval);
+ }
+ }
+ public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
+ }
+ public void display(GLAutoDrawable drawable) {
+ }
+ public void dispose(GLAutoDrawable drawable) {
+ }
+
+ // ***********************************************************************************
+ // ***********************************************************************************
+ // ***********************************************************************************
+
+ public void windowResized(WindowEvent e) {
+ }
+
+ public void windowMoved(WindowEvent e) {
+ }
+
+ public void windowDestroyNotify(WindowEvent e) {
+ quit=true;
+ }
+ public void windowGainedFocus(WindowEvent e) { }
+ public void windowLostFocus(WindowEvent e) { }
+
+ // ***********************************************************************************
+ // ***********************************************************************************
+ // ***********************************************************************************
+
+ public void keyPressed(KeyEvent e) {
+ System.out.println(e);
+ if(e.getKeyChar()=='f') {
+ glWindow.setFullscreen(!glWindow.isFullscreen());
+ } else if(e.getKeyChar()=='q') {
+ quit = true;
+ }
+ }
+ public void keyReleased(KeyEvent e) {
+ System.out.println(e);
+ }
+ public void keyTyped(KeyEvent e) {
+ System.out.println(e);
+ }
+
+ // ***********************************************************************************
+ // ***********************************************************************************
+ // ***********************************************************************************
+
+ public void mouseClicked(MouseEvent e) {
+ System.out.println(" mouseevent: "+e);
+ switch(e.getClickCount()) {
+ case 1:
+ if(e.getButton()>MouseEvent.BUTTON1) {
+ glWindow.setFullscreen(!glWindow.isFullscreen());
+ }
+ break;
+ default:
+ quit=true;
+ break;
+ }
+ }
+ public void mouseEntered(MouseEvent e) {
+ }
+ public void mouseExited(MouseEvent e) {
+ }
+ public void mousePressed(MouseEvent e) {
+ }
+ public void mouseReleased(MouseEvent e) {
+ }
+ public void mouseMoved(MouseEvent e) {
+ }
+ public void mouseDragged(MouseEvent e) {
+ }
+ public void mouseWheelMoved(MouseEvent e) {
+ }
+
+}
+
diff --git a/src/demos/applets/RedSquareNEWTAWTApplet.java b/src/demos/applets/RedSquareNEWTAWTApplet.java
deleted file mode 100755
index dce71ca..0000000
--- a/src/demos/applets/RedSquareNEWTAWTApplet.java
+++ /dev/null
@@ -1,33 +0,0 @@
-package demos.applets;
-
-import java.applet.*;
-import java.awt.*;
-import demos.gears.Gears;
-import javax.media.opengl.awt.GLCanvas;
-import com.sun.opengl.util.Animator;
-import com.sun.opengl.util.FPSAnimator;
-
-/** Shows how to deploy an applet using JOGL. This demo must be
- referenced from a web page via an &lt;applet&gt; tag. */
-
-public class RedSquareNEWTAWTApplet extends Applet {
- private Animator animator;
-
- public void init() {
- setLayout(new BorderLayout());
- GLCanvas canvas = new GLCanvas();
- canvas.addGLEventListener(new Gears());
- canvas.setSize(getSize());
- add(canvas, BorderLayout.CENTER);
- animator = new FPSAnimator(canvas, 60);
- }
-
- public void start() {
- animator.start();
- }
-
- public void stop() {
- // FIXME: do I need to do anything else here?
- animator.stop();
- }
-}