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.BorderFactory; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.border.Border; 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 OneTriangleSwingGLCanvas1 { public static void main( String [] args ) { GLProfile glprofile = GLProfile.getDefault(); GLCapabilities glcapabilities = new GLCapabilities( glprofile ); final GLCanvas glcanvas = new GLCanvas( glcapabilities ); glcanvas.setSize( 640, 480 ); 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 ); JPanel panel = new JPanel(new BorderLayout()) { // @Override // public void doLayout() { // GraphicsConfiguration graphicsConfiguration = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration(); // AffineTransform displayTransform = graphicsConfiguration.getDefaultTransform(); // double scaleX = displayTransform.getScaleX(); // Windows OS Display X scaling. // double scaleY = displayTransform.getScaleY(); // Windows OS Display Y scaling. // int newWidth = (int) (getWidth() * scaleX); // int newHeight = (int) (getHeight() * scaleY); // System.out.printf("layout %d x %d; scale %.2f; %d x %d --> %d x %d\n", // getWidth(), getHeight(), // scaleX, getWidth(), getHeight(), newWidth, newHeight); // glcanvas.setBounds(0, 0, newWidth, newHeight); // glcanvas.setSize(newWidth, newHeight); // } }; Border border = BorderFactory.createLineBorder(Color.BLUE, 5); panel.setBorder(border); panel.add(glcanvas); 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); int drawWidth = glautodrawable.getSurfaceWidth(); int drawHeight = glautodrawable.getSurfaceHeight(); OneTriangle.render( glautodrawable.getGL().getGL2(), drawWidth, drawHeight ); GL2 gl = glautodrawable.getGL().getGL2(); gl.glColor3f( 1.0f, 1.0f, 0.0f ); gl.glLineWidth(7.0f); gl.glBegin( GL.GL_LINE_STRIP ); gl.glVertex2i( 0, 3 ); gl.glVertex2i( drawWidth-3, 3 ); gl.glVertex2i( drawWidth-3, drawHeight-3); gl.glVertex2i( 3, drawHeight-3); gl.glVertex2i( 3, 0 ); gl.glEnd(); System.out.printf("display drawable %d x %d\n", drawWidth, drawHeight); Rectangle bnds = glcanvas.getBounds(); System.out.printf("display canvas bnds %d x %d at %d, %d\n", bnds.width, bnds.height, bnds.x, bnds.y); System.out.printf("display panel %d x %d\n", panel.getWidth(), panel.getHeight()); System.out.printf("display frame %d x %d\n", jframe.getWidth(), jframe.getHeight()); } }); jframe.add(panel); jframe.pack(); jframe.setVisible( true ); } }