/** * This is based on page 66 of Hill 2nd edition. It shows how we can get mouse and * keyboard input. */ import java.awt.*; import java.util.*; import java.awt.event.*; import javax.media.opengl.*; import javax.media.opengl.glu.*; public class polyLineDraw extends Frame implements GLEventListener, MouseListener, KeyListener { int width = 640; //window width and height int height = 480; ArrayList polyline = new ArrayList(); // the polyline we are creating GLCanvas canvas; //the GLCanvas we use for drawing public polyLineDraw() { canvas = new GLCanvas(); canvas.addGLEventListener(this); canvas.addMouseListener( this ); canvas.addKeyListener( this ); add("Center", canvas); setSize(width,height); setVisible(true); } public static void main( String args[]) { polyLineDraw frame = new polyLineDraw(); //exit if frame's close box is clicked frame.addWindowListener( new WindowAdapter() { public void windowClosed(WindowEvent e){ System.exit(0); } public void windowClosing(WindowEvent e) { windowClosed(e); } } ); } /* The functions below are required because we are a GLEventListener. We could also have put them in another class and put that class in the addGLEventListener method above. */ //drawing colour double red,green,blue; /** * Executed exactly once to initialize the * associated GLDrawable */ public void init(GLAutoDrawable drawable) { GL gl = drawable.getGL(); /** * Set the background colour when the GLDrawable * is cleared */ gl.glClearColor( 1.0f, 1.0f, 1.0f, 1.0f ); //white } /** * Executed if the associated GLDrawable is resized */ public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) { GL gl = drawable.getGL(); this.width = width; this.height = height; gl.glViewport( x, y, width, height ); gl.glMatrixMode( GL.GL_PROJECTION ); gl.glLoadIdentity(); gl.glOrtho( x, width , y, height, -1 ,1); } /** This method handles the painting of the GLDrawable */ public void display(GLAutoDrawable drawable) { GL gl = drawable.getGL(); /** Clear the colour buffer */ gl.glClear( GL.GL_COLOR_BUFFER_BIT ); gl.glColor3d(red, green, blue); gl.glBegin( GL.GL_LINE_STRIP ); //draw the polyline for(int i = 0; i < polyline.size(); i++) { Point p = polyline.get(i); gl.glVertex2i(p.x, p.y); } gl.glEnd(); } /** This method handles things if display depth changes */ public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged){ } //need these methods because we are a mouse listener public void mouseClicked(MouseEvent e) { if (e.isMetaDown()) { //right click //start new polyline polyline = new ArrayList(); } else { Point p = new Point (e.getX(), height - e.getY()); // AWT has y axis going up, GL, down polyline.add(p); } canvas.repaint(); } public void mouseEntered(MouseEvent e) { } public void mousePressed(MouseEvent e) { } public void mouseReleased(MouseEvent e) { } public void mouseExited(MouseEvent e) { } /** Handles keyboard events */ public void keyPressed( KeyEvent evt ) { /** * Check if we've pressed 'c' or 'C'. If so, change * the colour! */ if ( evt.getKeyChar() == 'q' || evt.getKeyChar() == 'Q' ) { System.exit(0); } if ( evt.getKeyChar() == 'c' || evt.getKeyChar() == 'C' ) { red = Math.random(); green = Math.random(); blue = Math.random(); canvas.repaint(); } return; } public void keyTyped( KeyEvent evt ) { } public void keyReleased( KeyEvent evt ) { } }