// Handle the keystrokes public void keyTyped(KeyEvent e) { switch (e.getKeyChar()) { case 'h': gl.matrixMode(GL.MODELVIEW); /* manipulate modelview matrix */ gl.rotate(15.0, 0.0, 1.0, 0.0); break; case 'j': gl.matrixMode(GL.MODELVIEW); /* manipulate modelview matrix */ gl.rotate(15.0, 1.0, 0.0, 0.0); break; case 'k': gl.matrixMode(GL.MODELVIEW); /* manipulate modelview matrix */ gl.rotate(-15.0, 1.0, 0.0, 0.0); break; case 'l': gl.matrixMode(GL.MODELVIEW); /* manipulate modelview matrix */ gl.rotate(-15.0, 0.0, 1.0, 0.0); break; case '+': gl.matrixMode(GL.PROJECTION); /* manipulate Projection matrix */ gl.translate(0.0, 0.0, 0.5); e.consume(); break; case '-': gl.matrixMode(GL.PROJECTION); /* manipulate Projection matrix */ gl.translate(0.0, 0.0, -0.5); e.consume(); break; case 's': if (r_thread == null) { r_thread = new Thread(this); r_thread.start(); } break; case 'p': if (r_thread != null) { r_thread.interrupt(); r_thread = null; } break; case 27: /* Esc will quit */ System.exit(1); break; default: break; } e.consume(); display(); }
// Sort of a legacy type function call, actually inside Java // This stuff should be in the paint call public synchronized void display() { // clear the screen and draw a yellow square gl.clear(GL.COLOR_BUFFER_BIT); // First rectangle gl.pushMatrix(); gl.translate(-0.5, 0.0, 0.0); gl.rotate(spin, 0.0, 0.0, 1.0); gl.rect(-0.5, -0.5, 0.5, 0.5); gl.popMatrix(); // Second rectangle gl.pushMatrix(); gl.translate(0.5, 0.0, 0.0); gl.rotate(-spin, 0.0, 0.0, 1.0); gl.rect(-0.5, -0.5, 0.5, 0.5); gl.popMatrix(); gl.flush(); // Make sure all commands have completed. gl.swap(); // Swap the render buffer with the screen buffer }