Login  Register

Re: Can I put awt components over part of a GLCanvas?

Posted by John V. on May 19, 2010; 5:42pm
URL: https://forum.jogamp.org/Can-I-put-awt-components-over-part-of-a-GLCanvas-tp827759p829793.html

Hi Sven,

Thanks for the response.  I looked into this issue some more.  I can't get awt components to appear above a GLCanvas on OS X.  But I can get it to work on Linux and Windows.

Here is a test case:


import java.awt.*;
import java.awt.event.*;
import javax.media.opengl.*;
import javax.media.opengl.awt.*;
import com.sun.opengl.util.*;

public class Test extends Frame implements GLEventListener
{
    Test(boolean useGLCanvas)
    {
        super("Test");
        this.setLayout(null);
       
        final Label label1 = new Label("Hello, World (1).");
        label1.setBounds(150, 100, 100, 50);
        this.add(label1);
       
        if (useGLCanvas) {
           final GLCanvas glCanvas = new GLCanvas();
           glCanvas.addGLEventListener(this);
           glCanvas.setBounds(0, 0, 400, 400);
           this.add(glCanvas);
        }
        else {
            final TestCanvas testCanvas = new TestCanvas();
            testCanvas.setBounds(0, 0, 400, 400);
            this.add(testCanvas);
        }

        // Add a second label just in case z-ordering is mysteriously reversed somehow.
        final Label label2 = new Label("Hello, World (2).");
        label2.setBounds(150, 300, 100, 50);
        this.add(label2);

        this.setSize(400, 400);
        this.setVisible(true);
    }
   
    public void display(GLAutoDrawable drawable)
    {
        GL2 gl = drawable.getGL().getGL2();
        gl.glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
        gl.glClear(GL2.GL_COLOR_BUFFER_BIT);
        gl.glMatrixMode(GL2.GL_PROJECTION);
        gl.glLoadIdentity();
        gl.glOrtho(-1.0f, 1.0f, -1.0f, 1.0f, -1.0, 1.0);
        gl.glMatrixMode(GL2.GL_MODELVIEW);
        gl.glLoadIdentity();
        gl.glColor3f(0.6f, 0.5f, 0.5f);
        gl.glRectf(-0.5f, -0.5f, 0.5f, 0.5f);
        gl.glFlush();
    }
   
    public void dispose(GLAutoDrawable drawable) {}
    public void init(GLAutoDrawable drawable) {}
    public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {}
   
    public static void main(String[] args)
    {
        // If the argument is true, the frame will create a GLCanvas.
        // Otherwise it will create a TestCanvas (for comparison).
        Test test = new Test(true);
    }
}


class TestCanvas extends Canvas
{
    public void paint(Graphics g)
    {
        // Draw some random stuff.
        g.drawLine(0, 0, 400, 400);
        g.drawLine(0, 400, 400, 0);
        g.fillRect(150, 150, 100, 100);
    }
}


I would expect at least one of the labels to be visible, but neither one shows up on OS X.  I ran a similar test case on Linux and Windows, and the first label is visible (which is expected).

Am I making a mistake in this test case?  Is there some extra setting I need to change to make this work on OS X?  Is this an OS X bug?  (I tried two different Apple machines, and both exhibited the same behavior.)


> I don't really see the necessity for such a thing,
> since you could achieve many feature via GL rendering, viewport, stencil
> buffer, scissors ..

In my applet, I'd like to use java.awt components like buttons and labels and text areas.  And I'd like those components to appear over part of a GLCanvas.

If I can't use java.awt on OS X, I guess I'll look for a library that has implemented similar components in GL.  Any suggestions?


> Maybe you like to try NEWT ?

It doesn't look like NEWT has general UI components like the java.awt components, but I could be missing something.

Thanks,
-john.