From e7003e596519e225ecec5b4423314c4dcaa05db8 Mon Sep 17 00:00:00 2001 From: Sven Gothel Date: Mon, 14 Sep 2009 06:12:33 -0700 Subject: NEWT Applet work .. --- src/demos/applets/JOGLNewtAppletBase.java | 256 ++++++++++++++++++++++++++++++ 1 file changed, 256 insertions(+) create mode 100755 src/demos/applets/JOGLNewtAppletBase.java (limited to 'src/demos/applets/JOGLNewtAppletBase.java') 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 <applet> 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) { + } + +} + -- cgit v1.2.3