//-------------------------------------------------- // GLWidget.cpp -- implementation of GLWidget class //-------------------------------------------------- import javax.media.opengl.*; import javax.media.opengl.glu.GLU; import java.awt.event.*; import java.lang.String; import java.lang.*; // the main class that facilitates OpenGL rendering // the listeners are combined to consolidate all interaction // but must add each listener separately to the canvas // --- done in mainWindow public class glWidget implements GLEventListener, MouseListener, MouseMotionListener { int x0, x1, y0, y1; int num_points; static mainWindow gui; GLJPanel canvas; private static final GLU glu = new GLU(); glWidget(mainWindow mainwin, GLJPanel cnvs) { gui = mainwin; canvas = cnvs; num_points = 0; } // all GL rendering is generally done // in the display function. public void display(GLAutoDrawable gLDrawable) { // get the gl context final GL gl = gLDrawable.getGL(); if (gui.getCurrentMode() == "Draw Line") { // two succeeding mouse clicks draws a line // controlled from mousePressed() if (num_points == 2) { // Draw a "tan" line from (x0,y0) to (x1,y1) gl.glBegin(gl.GL_LINES); gl.glColor3f( 1.0f, 0.5f, 0.0f ); gl.glVertex2i( x0, y0 ); gl.glVertex2i( x1, y1 ); gl.glEnd(); num_points = 0; } } // initial mouse click + dragging draws // a continuous line else if (gui.getCurrentMode() == "Draw Freehand") { gl.glBegin(gl.GL_LINES); gl.glColor3f( 0.0f, 1.0f, 0.5f ); gl.glVertex2i( x0, y0 ); gl.glVertex2i( x1, y1 ); gl.glEnd(); } else if (gui.getCurrentMode() == "Clear Canvas") { gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); gl.glClear(gl.GL_COLOR_BUFFER_BIT); } } public void displayChanged(GLAutoDrawable gLDrawable, boolean modeChanged, boolean deviceChanged) { } public void init(GLAutoDrawable gLDrawable) { final GL gl = gLDrawable.getGL(); gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); gl.glClear(gl.GL_COLOR_BUFFER_BIT); glu.gluOrtho2D(0, 640, 437, 0); display(gLDrawable); } // when window is resized, gl is notified // through this function public void reshape(GLAutoDrawable gLDrawable, int x, int y, int width, int height) { if (height <= 0) height = 1; final GL gl = gLDrawable.getGL(); gl.glMatrixMode(GL.GL_PROJECTION); gl.glLoadIdentity(); glu.gluOrtho2D(0, width, height, 0); gl.glMatrixMode(GL.GL_MODELVIEW); gl.glLoadIdentity(); display(gLDrawable); } public void changeMode (String m) { // do some initializations x0 = x1 = y0 = y1 = 0; num_points = 0; } // Mouse callbacks public void mousePressed(MouseEvent e) { if (gui.getCurrentMode() == "Draw Line") { if (num_points == 0) { x0 = e.getX(); y0 = e.getY(); num_points++; } else if (num_points == 1) { // Get the second point and draw line x1 = e.getX(); y1 = e.getY(); num_points++; canvas.display(); } } else if (gui.getCurrentMode() == "Draw Freehand") { x0 = x1 = e.getX(); y0 = y1 = e.getY(); } } public void mouseDragged(MouseEvent e) { if (gui.getCurrentMode() == "Draw Freehand") { x1 = e.getX(); y1 = e.getY(); canvas.display(); x0 = x1; y0 = y1; } } public void mouseMoved(MouseEvent e) { } public void mouseReleased(MouseEvent e) { } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } public void mouseClicked(MouseEvent e) { } };