package cs3744; import com.sun.opengl.util.Animator; import com.sun.opengl.util.awt.Overlay; import com.sun.opengl.util.gl2.GLUT; import java.awt.Color; import java.awt.Graphics2D; import java.awt.event.MouseEvent; import java.awt.event.MouseMotionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import javax.media.opengl.DebugGL2; import javax.media.opengl.GL2; import javax.media.opengl.GLAutoDrawable; import javax.media.opengl.GLCapabilities; import javax.media.opengl.GLEventListener; import javax.media.opengl.GLProfile; import javax.media.opengl.awt.GLJPanel; import javax.media.opengl.glu.GLU; import javax.swing.JFrame; /** * CS 3744 Fall 2010 Homework One solution template. * Draws two white squares on a blue background. * The mouse pointer coordinates are displayed in the centers of both squares, * The lower rectangle displays the coordinates in red using the OpenGL API. * The upper rectangle displays the coordinates in green using Java2D API. * * Implements GLEventListener to support OpenGL and MouseMotionListener to * support mouse tracking. * * @author Denis Gracanin * @version 1.0 */ public class HomeworkOne implements GLEventListener, MouseMotionListener { private String text = "(-, -)"; private int height = 0; /** * The main method creates an instance of the Homework One class and * provides the necessary GUI for the OpenGL display. * * @param args Command line arguments (none supported). */ public static void main(String[] args) { HomeworkOne homeworkOne = new HomeworkOne(); JFrame frame = new JFrame("Homework One"); GLCapabilities capabilities = new GLCapabilities(GLProfile.get(GLProfile.GL2)); GLJPanel panel = new GLJPanel(capabilities); panel.addGLEventListener(homeworkOne); panel.addMouseMotionListener(homeworkOne); frame.getContentPane().add(panel); final Animator animator = new Animator(panel); frame.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { // Run this on another thread than the AWT event queue to // make sure the call to Animator.stop() completes before // exiting new Thread(new Runnable() { public void run() { animator.stop(); System.exit(0); } }).start(); } }); frame.setSize(400, 400); frame.setVisible(true); animator.start(); } /** * Initializes the application for OpenGL to set the swap interval, * background color, and shading type. * * @param drawable Provides a primary OpenGL rendering context. */ public void init(GLAutoDrawable drawable) { drawable.setGL(new DebugGL2(drawable.getGL().getGL2())); GL2 gl = drawable.getGL().getGL2(); gl.setSwapInterval(1); // OpenGL code goes in here } /** * Called during the first repaint after the component has been resized. * Used to adjust to the change in the window size. * * @param drawable Provides a primary OpenGL rendering context. */ public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) { GL2 gl = drawable.getGL().getGL2(); GLU glu = new GLU(); // OpenGL code goes in here } /** * Contains OpenGLcode to create and display graphics. * It also include Java graphics Overlay to render text. * * @param drawable Provides a primary OpenGL rendering context. */ public void display(GLAutoDrawable drawable) { GL2 gl = drawable.getGL().getGL2(); GLUT glut = new GLUT(); // OpenGL code goes in here Overlay overlay = new Overlay(drawable); Graphics2D graphics = overlay.createGraphics(); graphics.setColor(Color.green); graphics.drawString(text, 100, 100); overlay.drawAll(); graphics.dispose(); } /** * Releases all OpenGL resources that may be allocated. * * @param drawable Provides a primary OpenGL rendering context. */ public void dispose(GLAutoDrawable drawable) { } /** * The MouseMotionListener handler triggered when a mouse is * dragged over the listener area. * * @param e A mouse event that triggered this handler. */ public void mouseDragged(MouseEvent e) { } /** * The MouseMotionListener handler triggered when a mouse is * moved over the listener area. * * @param e A mouse event that triggered this handler. */ public void mouseMoved(MouseEvent e) { text = "(" + e.getX() + ", " + e.getY() + ")"; } }