Mix JOGL graphics with 2D Java graphics

From JReality Wiki
Jump to: navigation, search

Source file: GLJPanelViewerExample


Run as Java webstart

When the tutorial is started, you should see a blue icosahedron against a green background. Use the mouse to rotate the figure. As you rotate, two text strings should appear and move diagonally across the screen, "preRender" passing behind the polyhedron and "postRender" passing in front. These strings were rendered within the Java2D graphics package; the icosahedron was rendered by the jReality system. "preRender" was rendered, naturally, before the icosahedron; and "postRender", afterwards.


This mixture is achieved by a JOGL Viewer class de.jreality.jogl.GLJPanelViewer which renders into an instance of the class javax.media.opengl.GLJPanel, a sub-class of JPanel. (The standard JOGL viewer uses an instance of javax.media.opengl.GLCanvas, a so-called heavyeweight component.) This tutorial shows how to use a GLJPanelViewer to render. The main new feature this class offers is the interface GLJPanelListener:


	public interface GLJPanelListener extends java.util.EventListener	{
		public void preRender(Graphics2D g2);
		public void postRender(Graphics2D g2);
	}


The following code snippet from the tutorial shows how to do this:


		final GLJPanelViewer glpv = (GLJPanelViewer) ((ViewerSwitch) v).getCurrentViewer();
		// code with JOGL code
		glpv.addRenderListener(new GLJPanelViewer.GLJPanelListener() {
			Font font = new Font("Times", Font.BOLD, 32);
			int count = 0;
			public void postRender(Graphics2D g2) {
				if (g2 == null) return;
				g2.setColor(Color.pink);
				g2.setFont(font);
				g2.drawString("postRender", 10+count, 50+count);
				count++;
				count = count % 500;
			}
 
			public void preRender(Graphics2D g2) {
				if (g2 == null) return;
				g2.setColor(Color.blue);
				g2.setFont(font);
				g2.drawString("preRender", 10+count, 100+count);
			}
			
		});
 


Note: The "Export -> Image" menu item in JRViewer doesn't work with this viewer. Use "Export -> Screenshot" instead.

The GLJPanelViewer in action
Previous: Use selection capabilities Developer Tutorial: Contents Next: Use accelerated 2d textures with JOGL backend