JOGL Hello World

Here is a basic JOGL application:

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


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();

Animator anim = new Animator(canvas);


fr.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {

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

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

// init opengl here

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


// draw things here


public void dispose(GLAutoDrawable drawable) {


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.


Leave a Reply

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

You are commenting using your 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