JOGL Hello World

Here is a basic JOGL application:

import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.media.opengl.GL2;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.awt.GLCanvas;

import com.sun.opengl.util.Animator;

public class JOGLAPP implements GLEventListener {
public static void main(String[] args) {
new JOGLAPP();
}

Frame fr;
GLCanvas canvas;

public JOGLAPP() {
fr = new Frame("JOGLAPP");
canvas = new GLCanvas();
canvas.addGLEventListener(this);
fr.add(canvas);

Animator anim = new Animator(canvas);

fr.setSize(300,300);
fr.setVisible(true);
anim.start();

fr.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
super.windowClosing(e);
System.exit(0);
}
});
}

@Override
public void init(GLAutoDrawable drawable) {
GL2 gl = drawable.getGL().getGL2();

gl.glClearColor(1.0f, 0.0f, 0.0f, 0.0f);

// init opengl here
}

@Override
public void display(GLAutoDrawable drawable) {
GL2 gl = drawable.getGL().getGL2();

gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);

// draw things here

gl.glFinish();
}

@Override
public void dispose(GLAutoDrawable drawable) {

}

@Override
public void reshape(GLAutoDrawable drawable, int x, int y, int width,
int height) {

}

}

This entry was posted in Code Snippets/Tricks. Bookmark the permalink.

4 Responses to JOGL Hello World

  1. dep241 says:

    Nice example. And compiles fine with JOGL 2.0 Beta10. But on my system each time the frame is resized the background color starts to switch rapidly between white and red. Rather annoying. 🙂

  2. Ricky says:

    Well, you could set the background color of the Frame with a line such as fr.setBackground(Color.RED) – but of course this would only mask the problem. I’m sure it’s something to do with how GLCanvas is implemented and how the repaint event is called during a resize.

  3. dep241 says:

    Agree. fr.setBackground(Color.RED) would help only in particular case. Obviously drawing any other scene will bring the problem back. Think it should be fixed in the library itself.

  4. dep241 says:

    Just to close the discussion with a working solution. When running Jogl applications on Windows, it is necessary to specify the system property -Dsun.java2d.noddraw=true In addition to this passing -Dsun.awt.noerasebackground=true will remove flickering during live resizing of a GLCanvas. Details are in Jogl – User’s Guide.

    Regards.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s