1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
| import java.awt.Frame; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent;
import net.java.games.jogl.Animator; import net.java.games.jogl.GL; import net.java.games.jogl.GLCanvas; import net.java.games.jogl.GLCapabilities; import net.java.games.jogl.GLDrawable; import net.java.games.jogl.GLDrawableFactory; import net.java.games.jogl.GLEventListener; import net.java.games.jogl.GLU;
public class PickSquare { public static void main(String[] args) { Frame frame = new Frame("PickSquare Demo"); GLCanvas canvas = GLDrawableFactory.getFactory().createGLCanvas(new GLCapabilities());
canvas.addGLEventListener(new PickSquareSample()); frame.add(canvas); frame.setSize(300, 300); final Animator animator = new Animator(canvas); frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { animator.stop(); System.exit(0); } }); frame.show(); animator.start(); } static class PickSquareSample implements GLEventListener, MouseListener { GL myGL; GLU myGLU; private int board[][] = new int[3][3]; private void init () { int i, j; for (i = 0; i < 3; i++) { for (j = 0; j < 3; j++) { board [i][j] = 0; } } myGL.glClearColor (0.0f, 0.0f, 0.0f, 0.0f); } private void drawSquares (int mode) { int i, j; for (i = 0; i < 3; i++) { if (mode == GL.GL_SELECT) { myGL.glLoadName (i); } for (j = 0; j < 3; j ++) { if (mode == GL.GL_SELECT) { myGL.glPushName (j); } myGL.glColor3f ((float)(i/3.0),(float)(j/3.0),(float)(board[i][j]/3.0)); myGL.glRecti (i, j, i+1, j+1); if (mode == GL.GL_SELECT) { myGL.glPopName (); } } } } private void processHits (int hits, int buffer []) { int i, j; int ii = 0, jj = 0, names, ptr; System.out.println ("hits = " + hits); ptr = 0; for (i = 0; i < hits; i++) { names = buffer [ptr]; System.out.println (" number of names for hit = " + names); ptr++; System.out.print (" z1 is " + buffer [ptr] + ";"); ptr++; System.out.println (" z2 is " + buffer [ptr]); ptr++; System.out.print (" the name is "); for (j = 0; j < names; j++) { System.out.print (buffer [ptr] + " "); if (j == 0) { ii = buffer [ptr]; } else { jj = buffer [ptr]; } ptr++; } System.out.println (); board [ii][jj] = (board [ii][jj] + 1) % 3; } } private static final int BUFSIZE = 512; private void pickSquares (MouseEvent e) { int selectBuf [] = new int [BUFSIZE]; int hits; int viewport [] = new int [4]; int x, y; x = e.getX (); y = e.getY (); myGL.glGetIntegerv (GL.GL_VIEWPORT, viewport); myGL.glSelectBuffer (BUFSIZE, selectBuf); myGL.glRenderMode (GL.GL_SELECT); myGL.glInitNames (); myGL.glPushName (-1); myGL.glMatrixMode (GL.GL_PROJECTION); myGL.glPushMatrix (); myGL.glLoadIdentity (); myGLU.gluPickMatrix((double)x,(double)(viewport[3]-y),5.0,5.0,viewport); myGLU.gluOrtho2D (0.0, 3.0, 0.0, 3.0); drawSquares (GL.GL_SELECT); myGL.glPopMatrix (); myGL.glFlush (); hits = myGL.glRenderMode (GL.GL_RENDER); processHits (hits, selectBuf); } public void mousePressed(MouseEvent e) { if ((e.getModifiers() & MouseEvent.BUTTON1_MASK) == MouseEvent.BUTTON1_MASK) { pickSquares (e); e.consume (); } } public void display (GLDrawable d) { myGL.glClear (GL.GL_COLOR_BUFFER_BIT); drawSquares (GL.GL_RENDER); myGL.glFlush (); } public void reshape(GLDrawable drawable, int x, int y, int w, int h) { myGL.glViewport (0, 0, w, h); myGL.glMatrixMode (GL.GL_PROJECTION); myGL.glLoadIdentity (); myGLU.gluOrtho2D (0.0, 3.0, 0.0, 3.0); myGL.glMatrixMode (GL.GL_MODELVIEW); myGL.glLoadIdentity (); } public void init (GLDrawable drawable) { myGL=drawable.getGL(); myGLU=drawable.getGLU(); drawable.addMouseListener(this); init (); }
public void displayChanged(GLDrawable arg0, boolean arg1, boolean arg2) {} public void mouseClicked(MouseEvent e) {} public void mouseEntered(MouseEvent arg0) {} public void mouseExited(MouseEvent arg0) {} public void mouseReleased(MouseEvent arg0) {} } } |