Richard Carter

Computer Science & Game Development Student at North Carolina State University

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) {
    
  }
  
  
}

Share this article:
  • Print
  • RSS
  • email
  • Digg
  • del.icio.us
  • Facebook
  • Twitter
  • Live
  • StumbleUpon
  1. dep241 Said,

    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 Said,

    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 Said,

    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 Said,

    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.

Add A Comment

Sorry for the mess! I recently got hacked and lost my previous theme, so I'm working on reconstructing that. In the meantime, this theme is messy/broken but at least you can read my posts. Sorry!!


Hi, I'm Richard Carter! I use this blog to document particularly difficult-to-solve computer problems. My posts are written for clarity and keywords for search engines to pick up on, so that the next person that runs into the same problem will easily find my solution here and have an easier time than I had! I'm forging a path through the brush, so to speak. So if you came here by search engine, I guess it worked! Enjoy the solution to your problem.