Swing and AWT components problem

This code doesn’t work :


public class TestJOGL extends JFrame
{
	public static void main(String[] args) throws InterruptedException
	{
		JFrame frame = new JFrame();
		frame.setSize(640, 480);
		frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
		
		GLCanvas canvas = new GLCanvas(new GLCapabilities(GLProfile.get(GLProfile.GL2)));
		frame.add(canvas);
		frame.setVisible(true);
	}
}

The GLCanvas is not displayed, the JFrame is white.

But this code works :


public class TestJOGL extends JFrame
{
	public static void main(String[] args) throws InterruptedException
	{
		JFrame frame = new JFrame();
		frame.setSize(640, 480);
		frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
		
		GLCanvas canvas = new GLCanvas(new GLCapabilities(GLProfile.get(GLProfile.GL2)));
		frame.setVisible(true);
		frame.add(canvas); // add after the "setVisible"
	}
}

The GLCanvas is displayed, the JFrame is black.
Use Swing and AWT components is not recommended, but, is it a bug ?

tested with the latest JOGL 2.0 nightly build

Try

public class TestJOGL
{
	public static void main(String[] args) throws InterruptedException
	{
		SwingUtilities.invokeLater(
			new Runnable()
			{
				public void run()
				{
					JFrame frame = new JFrame();
					frame.setSize(640, 480);
					frame.setDefaultCloseOperation(EXIT_ON_CLOSE);

					GLCanvas canvas = new GLCanvas(new GLCapabilities(GLProfile.get(GLProfile.GL2)));
					frame.add(canvas);
					frame.setVisible(true);
				}
			});
	}
}

Unless you’re doing in this in code not posted, you haven’t added a GLEventListener to either case. So if there’s no exception being thrown or dead-lock, all this means is that the GLCanvas and GLJPanel have different starting contents. Often when I add a GLCanvas to a scene and take too long to render my first frame, it can be filled with garbage (or at least that’s what happened in JOGL 1). The GLJPanel may be slightly better about when it shows its OpenGL contents so you get a different behavior.

Please test with actual rendering to see if it’s still a concern.

Adding a GLEventListener change nothing.

No Exception or deadlock

If i add a GLEventListener to my GLCanvas, it go into the display(GLAutoDrawable) method
But the GLCanvas is not displayed !

Using GLJPanel instead of GLCanvas works
But it’s slower …

Have you tried my code (creating your components in the AWT event thread, as the docs tell you to)?

this code doesn’t work for me :frowning:
GLCanvas must be added in the JFrame after this one become visible (for me)

Are rendering anything using GL calls in the display method? If you’re not, the GLCanvas will be blank (or full of garbage).

here a new code, with something to display, it doesn’t work (because GLCanvas is added to the JFrame before it’s visible)


public class TestJOGL
{
	public static void main(String[] args) throws InterruptedException
	{
		JFrame frame = new JFrame();
		frame.setSize(640, 480);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		GLCanvas canvas = new GLCanvas(new GLCapabilities(GLProfile.get(GLProfile.GL2)));
		canvas.addGLEventListener(new GLEventListener()
		{
			private GL2 gl;
			
			public void init(GLAutoDrawable drawable)
			{
				this.gl = drawable.getGL().getGL2();
				this.gl.setSwapInterval(0);
				this.gl.glEnable(GL2.GL_TEXTURE_2D);
			}
			
			public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height)
			{
				this.gl.glViewport(x, y, width, height);
				this.gl.glMatrixMode(GL2.GL_PROJECTION);
				this.gl.glLoadIdentity();
				this.gl.glOrtho(0, width, height, 0, -1, 1);
			}
			
			public void display(GLAutoDrawable drawable)
			{
				System.out.println("display " + System.currentTimeMillis());
				
				this.gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);
				
				this.gl.glBegin(GL2.GL_QUADS);
				{
					this.gl.glVertex2i(0, 0);
					this.gl.glVertex2i(100, 0);
					this.gl.glVertex2i(100, 100);
					this.gl.glVertex2i(0, 100);
				}
				this.gl.glEnd();
			}
			
			public void dispose(GLAutoDrawable arg0)
			{
			}
		});
		
		Animator animator = new Animator();
		animator.add(canvas);
		animator.start();
		
		frame.add(canvas);
		
		frame.setVisible(true);
	}
}

The JFrame is blank

Do NOT do this :

private GL2 gl;

Rather do this each time you need a fresh GL instance :

drawable.getGL();

Please add some glColor calls and let us know what it does.


public class TestJOGL
{
	public static void main(String[] args) throws InterruptedException
	{
		JFrame frame = new JFrame();
		frame.setSize(640, 480);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		GLCanvas canvas = new GLCanvas(new GLCapabilities(GLProfile.get(GLProfile.GL2)));
		canvas.addGLEventListener(new GLEventListener()
		{
			public void init(GLAutoDrawable drawable)
			{
				GL2 gl = drawable.getGL().getGL2();
				gl.setSwapInterval(0);
				gl.glEnable(GL2.GL_TEXTURE_2D);
			}
			
			public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height)
			{
				GL2 gl = drawable.getGL().getGL2();
				gl.glViewport(x, y, width, height);
				gl.glMatrixMode(GL2.GL_PROJECTION);
				gl.glLoadIdentity();
				gl.glOrtho(0, width, height, 0, -1, 1);
			}
			
			public void display(GLAutoDrawable drawable)
			{
				System.out.println("display " + System.currentTimeMillis());
				
				GL2 gl = drawable.getGL().getGL2();
				
				gl.glColor3d(1, 0.5, 0.25);
				
				gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);
				
				gl.glBegin(GL2.GL_QUADS);
				{
					gl.glVertex2i(0, 0);
					gl.glVertex2i(100, 0);
					gl.glVertex2i(100, 100);
					gl.glVertex2i(0, 100);
				}
				gl.glEnd();
			}
			
			public void dispose(GLAutoDrawable arg0)
			{
			}
		});
		
		Animator animator = new Animator();
		animator.add(canvas);
		animator.start();
		
		frame.add(canvas);
		
		frame.setVisible(true);
	}
}

it’s still not working :confused:

it’s ok with :


		frame.setVisible(true);
		frame.add(canvas);

but it’s tricky …

You forgot something here


gl.glMatrixMode(GL2.GL_PROJECTION);
				gl.glLoadIdentity();
				gl.glOrtho(0, width, height, 0, -1, 1);
gl.glMatrixMode(GL2.GL_MODELVIEW);

Put the code above into the method init(GLAutoDrawable drawable) too and put something into the model-view matrix.