import java.awt.Color; import java.awt.Component; import java.awt.Frame; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; 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.GLProfile; import javax.media.opengl.awt.GLCanvas; import javax.swing.BoxLayout; public class Main { static CanvasListener listener = new CanvasListener(); public static void main(String[] args) { GLProfile.initSingleton(false); GLProfile profile = GLProfile.getMaxFixedFunc(); GLCapabilities caps = new GLCapabilities(profile); GLCanvas canvas = new GLCanvas(caps); canvas.setBackground(Color.yellow); canvas.addGLEventListener(listener); final Frame topLevelFrame = new Frame(); topLevelFrame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.err.println("closing"); topLevelFrame.dispose(); } public void windowClosed(WindowEvent e) { System.err.println("closed"); } }); topLevelFrame.setLayout(new BoxLayout(topLevelFrame, BoxLayout.Y_AXIS)); topLevelFrame.add(canvas); // Revalidate size/layout. // Always validate if component added/removed. // Ensure 1st paint of GLCanvas will have a valid size, hence drawable gets created. topLevelFrame.setSize(400, 300); topLevelFrame.validate(); topLevelFrame.setVisible(true); // Animator animator = new Animator(canvas); // animator.start(); // canvas.display(); } } class CanvasListener implements GLEventListener { @Override public void display(GLAutoDrawable arg0) { GL2 gl = arg0.getGL().getGL2(); gl.glClearColor(1.0f, 0.0f, 0.0f, 0.5f); gl.glClear(GL.GL_COLOR_BUFFER_BIT); System.out.println("display"); } @Override public void dispose(GLAutoDrawable arg0) { } @Override public void init(GLAutoDrawable arg0) { Component canvas = (Component) arg0; System.out.println(canvas.getSize().toString()); } @Override public void reshape(GLAutoDrawable arg0, int arg1, int arg2, int arg3, int arg4) { } }