GLEventListener JavaFX Style

Posted By Hoyt Summers Pittman

So far we have perverted the JavaFX Frame to be more JOGL Friendly. Now we can make the GLEventListener more JavaFX Friendly.

First a bit of warning and history. The GLEventListener defines a method called init. Init, in compiled JavaFX, is a reserved word and thus we can not make functions called init. My solution to this problem was to implement GLEventListener using an abstract class.

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.saga.javafx3d;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLEventListener;
/**
 *
 * @author summers
 */
public abstract class AbstractJavaFXGLEventListener implements GLEventListener{
    @Override
    public final  void init(GLAutoDrawable drawable) {
      _init(drawable);
    }
    public abstract void _init(GLAutoDrawable drawable);
}

While this does seem (deservedly) cheesy, I implemented the init method and had it call a safe method, _init.

Now we implement the JavaFXGLEventListener.

/*
 * JavaFXGLEventListener.fx
 *
 * Created on Aug 3, 2008, 11:30:02 AM
 */
package net.saga.javafx3d;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GL;
/**
 * @author summers
 */
public class JavaFXGLEventListener extends AbstractJavaFXGLEventListener {
    public function display(drawable: GLAutoDrawable) : Void {
        onDisplay(drawable);
    }
    public function displayChanged( drawable :GLAutoDrawable, modeChanged: Boolean, deviceChanged: Boolean) : Void{
        onDisplayChanged(drawable,modeChanged,deviceChanged);
    }
    public function _init(drawable: GLAutoDrawable ) : Void {
      onInit(drawable);
    }
    public function reshape(drawable: GLAutoDrawable , x: Integer, y:Integer, width:Integer, height:Integer) : Void{
        onReshape(drawable,x,y,width,height);
    }
    public attribute onReshape : function (drawable: GLAutoDrawable , x: Integer, y:Integer, width:Integer, height:Integer) : Void =
        function (drawable: GLAutoDrawable , x: Integer, y:Integer, width:Integer, height:Integer) : Void {
            drawable.getGL().glClear(GL.GL_COLOR_BUFFER_BIT);
            drawable.getGL().glClear(GL.GL_DEPTH_BUFFER_BIT);
        }
    public attribute onInit : function (drawable: GLAutoDrawable ) : Void =
        function (drawable: GLAutoDrawable ) : Void {
          var gl:GL = drawable.getGL();
          gl.glClearColor(0.0.floatValue(), 1.0.floatValue(), 0.0.floatValue(),1.0.floatValue());
          gl.glClear(GL.GL_COLOR_BUFFER_BIT);
          gl.glClear(GL.GL_DEPTH_BUFFER_BIT);
          gl.glColor3d(1.0, 0.0, 0.0);
        };
    public attribute onDisplay : function (drawable: GLAutoDrawable) : Void =
        function (drawable) {
        };
    public attribute onDisplayChanged : function ( drawable :GLAutoDrawable, modeChanged: Boolean, deviceChanged: Boolean) : Void =
        function ( drawable :GLAutoDrawable, modeChanged: Boolean, deviceChanged: Boolean) : Void{
            java.lang.System.out.println("KoL");
        };
}

Here, I implemented the functions defined in GLEventListener, and the abstract function _init. I made each of these call a locally defined attribute. Now, we can build this class declaratively. All put together we get something that looks like this:

/*
 * Main.fx
 *
 * Created on Jul 26, 2008, 4:17:58 AM
 */
package net.saga.javafx3d;
/**
* @author summers
*/
import javax.media.opengl.GLEventListener;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GL;
var win:GLFrame = GLFrame {
    title:"Hello World",
    visible:true,
    glEventListeners : [
          JavaFXGLEventListener {
                  onInit :function (drawable: GLAutoDrawable ) : Void {
                          java.lang.System.out.println("BEGIN!");
                          var gl:GL = drawable.getGL();
                          gl.glClearColor(0.0.floatValue(), 1.0.floatValue(), 0.0.floatValue(),1.0.floatValue());
                          gl.glClear(GL.GL_COLOR_BUFFER_BIT);
                          gl.glClear(GL.GL_DEPTH_BUFFER_BIT);
                          gl.glColor3d(0.0, 1.0, 0.0);
                  }
          }
    ],
    width:100,
    height:100
}

I am still calling JOGL code inside of the function, but things are beginning to look more homogeneous.

3DJavaFX.tgz

Aug 5th, 2008

No Comments! Be The First!

Leave a Reply