package demo; import com.jogamp.opengl.GL; import com.jogamp.opengl.GL2; /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. * https://www.programcreek.com/java-api-examples/?api=com.jogamp.opengl.awt.GLCanvas */ import com.jogamp.opengl.GLAutoDrawable; import com.jogamp.opengl.GLEventListener; import com.jogamp.opengl.GLProfile; import com.jogamp.opengl.GLCapabilities; import com.jogamp.opengl.awt.GLCanvas; import javax.swing.JFrame; import java.awt.BorderLayout; import java.awt.Color; import java.awt.GraphicsConfiguration; import java.awt.GraphicsEnvironment; import java.awt.Rectangle; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.awt.geom.AffineTransform; /** * A minimal program that draws with JOGL in a Swing JFrame using the AWT GLCanvas. * * @author Wade Walker */ public class OneTriangleSwingGLCanvas { public static void main( String [] args ) { GLProfile glprofile = GLProfile.getDefault(); GLCapabilities glcapabilities = new GLCapabilities( glprofile ); final GLCanvas glcanvas = new GLCanvas( glcapabilities ); glcanvas.addGLEventListener( new GLEventListener() { @Override public void reshape( GLAutoDrawable glautodrawable, int x, int y, int width, int height ) { OneTriangle.setup( glautodrawable.getGL().getGL2(), width, height ); System.out.printf("reshape canvas %d x %d\n", width, height); } @Override public void init( GLAutoDrawable glautodrawable ) { } @Override public void dispose( GLAutoDrawable glautodrawable ) { } @Override public void display( GLAutoDrawable glautodrawable ) { glautodrawable.getGL().getGL2().glClearColor(1.0f, 1.0f, 1.0f, 1.0f); OneTriangle.render( glautodrawable.getGL().getGL2(), glautodrawable.getSurfaceWidth(), glautodrawable.getSurfaceHeight() ); Rectangle bnds = glcanvas.getBounds(); GL2 gl = glautodrawable.getGL().getGL2(); gl.glColor3f( 0.0f, 0.0f, 1.0f ); gl.glLineWidth(3.0f); gl.glBegin( GL.GL_LINE_STRIP ); gl.glVertex2i( bnds.x, bnds.y ); gl.glVertex2i( bnds.x + bnds.width - 1, bnds.y ); gl.glVertex2i( bnds.x + bnds.width - 1, bnds.y + bnds.height - 1); gl.glVertex2i( bnds.x, bnds.y + bnds.height - 1 ); gl.glVertex2i( bnds.x, bnds.y ); gl.glEnd(); System.out.printf("display %d x %d\n", bnds.width, bnds.height); } }); final JFrame jframe = new JFrame( "One Triangle Swing GLCanvas" ); jframe.addWindowListener( new WindowAdapter() { public void windowClosing( WindowEvent windowevent ) { jframe.dispose(); System.exit( 0 ); } }); jframe.getContentPane().add( glcanvas, BorderLayout.CENTER ); jframe.setSize( 640, 480 ); jframe.setVisible( true ); } }