I recently upgraded to JOGL 2.0, and am having trouble rendering to GLJPanel. I am trying to do something simple, draw three colored rectangles to a 400x200 frame, but I cannot see anything on the last third of the screen. If I change to use GLCanvas, or make the frame 400x300, my code works fine, and it worked under JOGL 1.1.1 as well. Is 2.0 stricter on something I missed? My code is below.
import java.awt.Dimension;
import javax.media.opengl.GL;
import javax.media.opengl.GL2;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.awt.GLJPanel;
import javax.media.opengl.awt.GLCanvas;
import javax.media.opengl.fixedfunc.GLMatrixFunc;
import javax.swing.JFrame;
/** Draws a white box. */
class Ex1_2 {
/**
* Gets the canvas displayed by this exercise.
* @return GLJPanel
*/
public static GLJPanel getCanvas() {
GLCapabilities capabilities = new GLCapabilities(null);
//* enable double buffering
capabilities.setDoubleBuffered(true);
GLJPanel canvas = new GLJPanel(capabilities);
canvas.addGLEventListener(new Ex1Listener());
return canvas;
}
/** Hidden constructor. */
private Ex1_2() { }
}
/** The event listener for this class. */
class Ex1Listener implements GLEventListener {
/** the GL instance. */
private GL2 gl;
/**
* {@inheritDoc}
*/
public void display(final GLAutoDrawable drawable) {
// includes all operations to render (or re-render) the scene
if (gl == null) {
gl = drawable.getGL().getGL2();
}
//** clear all pixels
gl.glClear(GL.GL_COLOR_BUFFER_BIT);
//* draw white polygon (rectangle) with corners at
//* (0.25, O.25 , O.0) and (0.75 , O.75 , O.0)
gl.glColor3f(1.0f, 1.0f, 1.0f);
gl.glBegin(GL2.GL_POLYGON);
{
gl.glVertex2i(5, 5);
gl.glVertex2i(195, 5);
gl.glVertex2i(195, 45);
gl.glVertex2i(5, 45);
}
gl.glEnd();
gl.glColor3f(0.75f, 1.0f, 0.375f);
gl.glBegin(GL2.GL_POLYGON);
{
gl.glVertex2i(195, 45);
gl.glVertex2i(267, 45);
gl.glVertex2i(267, 95);
gl.glVertex2i(195, 95);
}
gl.glEnd();
gl.glColor3f(0.95f, 0.15f, 0.25f);
gl.glBegin(GL2.GL_POLYGON);
{
gl.glVertex2i(267, 95);
gl.glVertex2i(395, 95);
gl.glVertex2i(395, 145);
gl.glVertex2i(267, 145);
}
gl.glEnd();
//* force the above commands to begin executing.
//* don't wait! start processing buffered OpenGL routines
gl.glFlush();
}
/**
* {@inheritDoc}
*/
public void displayChanged(final GLAutoDrawable drawable,
final boolean modeChanged, final boolean deviceChanged) { }
/**
* {@inheritDoc}
*/
public void dispose(final GLAutoDrawable drawable) { }
/**
* {@inheritDoc}
*/
public void init(final GLAutoDrawable drawable) {
if (gl == null) {
gl = drawable.getGL().getGL2();
}
// clear color is a state variable
//* select clearing (background) color
gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
//* initialize viewing values
gl.glMatrixMode(GLMatrixFunc.GL_PROJECTION);
gl.glLoadIdentity();
gl.glOrtho(0.0, // left
400.0, // right
0.0, // bottom
200.0, // top
-1.0, // near
1.0); // far
}
/**
* {@inheritDoc}
*/
public void reshape(final GLAutoDrawable drawable, final int x, final int y, final int width, final int height) { }
}
/** Application entry point. */
public final class RedBookMain {
/**
* Application entry point.
* @param args the command line arguments; ignored by this application
*/
public static void main(final String[] args) {
JFrame frame = new JFrame();
frame.addWindowListener(new WindowHandler(frame));
frame.setPreferredSize(new Dimension(400, 200));
frame.getContentPane().add(Ex1_2.getCanvas());
frame.pack();
frame.setVisible(true);
}
/** Hidden constructor. */
private RedBookMain() { }
}