About Java2D/JOGL Interoperability Demo

Who can tell me about a demo window can draw 2D icon on top of 3D scene which it located at the left corner in Demo frame.

The icon is a JAVA trademark.

How to do that, because I try to use GLCanvas to draw 3D and draw 2D using the Graphics reference from GLCanvas, but when draw image, the screen will flicker. I didn’t use GLEventListener cause I need a special framework. Does anyone know if without GLEventListener, is it OK?

thanks
Mikael

AFAIK you can’t draw on a GLCanvas with the Graphics reference. It is only possible with GLJCanvas and enabled Java2D OpenGL pipeline, which is not very robust in the current implementation due to gfx card driver incompatibilities.

Thank you , so, you mean that demo only used JOGL with 2D pipeline capability without directly use Graphics object? Such as render a texture instead draw image?

You are definitely talking about the JGears example that comes with JOGL and in that case JGears uses a GLJPanel and it overrides the paintComponent() method of that panel in order to draw those icons. You can’t do that with the GLJCanvas, unfortunately. I believe that the main reason has to do with the lightweight vs heavyweight issues…the GLJCanvas will appear on top of anything else so overriding the paintcomponent is useless as the images are likely underneath.

No. I was a bit too fast. You should be able to mix swing, java2d and jogl without the j2d opengl pipeline, but you have to use the GLJPanel, which is much slower than the GLCanvas unless the j2d opengl pipeline is enabled. You just can’t do this with GLCanvas.


public class Main extends JFrame {
	
	GLJPanel panel;	
	public Main() {
		this.setBounds(100, 100, 500, 500);
		JP jp = new JP();		
		getContentPane().add(jp);
		jp.addGLEventListener(new DrawListener());
		setVisible(true);		 
	}	
		
	
	class JP extends GLJPanel {
		public JP() {
			super(new GLCapabilities());
			this.setSize(500,500);			
			
		}
		public void paintComponent(Graphics g) {
			//g.setColor(new Color(0xFFFFFF));
			g.drawString("some words", 30, 30);
		}
		
	}
	
	
	public static void main(String[] args) {
		Main m = new Main();		
		
	}
}
	
class DrawListener implements GLEventListener {
	
	 public void init(GLAutoDrawable gld) {
		  GL gl = gld.getGL();
		  GLU glu =  new GLU(); 
		  gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
		  gl.glViewport(0, 0, 500, 300);
		  gl.glMatrixMode(GL.GL_PROJECTION);
		  gl.glLoadIdentity();
		  glu.gluOrtho2D(0.0, 500.0, 0.0, 300.0);
	 }
	
	
	 public void display(GLAutoDrawable drawable) {
		  float red = 0.0f;
		  float green = 0.0f;
		  float blue = 0.0f;
		  GL gl = drawable.getGL();
		  gl.glClear(GL.GL_COLOR_BUFFER_BIT);
		  gl.glPointSize(5.0f);
		  for (int i = 0; i < 50; i++) {
		   red -= .09f;
		   green -= .12f;
		   blue -= .15f;
		   if (red < 0.15)
		    red = 1.0f;
		   if (green < 0.15)
		    green = 1.0f;
		   if (blue < 0.15)
		    blue = 1.0f;
		   gl.glColor3f(red, green, blue);
		   gl.glBegin(GL.GL_POINTS);
		   gl.glVertex2i((i * 10), 150);
		   gl.glEnd();
	  }
	 }

	 public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
	 }
	
	 public void displayChanged(GLAutoDrawable drawable, boolean modeChanged,
	   boolean deviceChanged) {
	 }
}



Please help me and let me understand what’s the correctly code structure, as the above code, it doesn’t work on drawing string and rendering 3D together. if I eliminate draw string, then render 3D is well.

I have no idea why.

thanks in advance.

Since you overrode the paintComponent method, the JOGL functionality is never called. You have to call the paintComponent() of the super class before doing your 2d rendering:


        public void paintComponent( Graphics g )
        {
            super.paintComponent( g );
            g.setColor( new Color( 0xFFFFFF ) );
            g.drawString( "some words", 30, 30 );
        }

Thank you very much, it is very exciting. Other question, Can I dont use the GLEventListener to implement? because I wan to do the initialize process and rendering action whenever as I want.

thanks

I don’t know, and even if it’s possible it’s not encouraged.

thanks, I see. I will open a new topic to ask the Applet implemention.