diff options
Diffstat (limited to 'src/demos/gears')
-rw-r--r-- | src/demos/gears/Gears.java | 97 |
1 files changed, 54 insertions, 43 deletions
diff --git a/src/demos/gears/Gears.java b/src/demos/gears/Gears.java index 4ae780e..c20d183 100644 --- a/src/demos/gears/Gears.java +++ b/src/demos/gears/Gears.java @@ -1,10 +1,19 @@ package demos.gears; -import java.awt.*; -import java.awt.event.*; - -import javax.media.opengl.*; -import com.sun.opengl.util.*; +import java.awt.Dimension; +import java.awt.Frame; +import java.awt.event.MouseEvent; +import java.awt.event.MouseListener; +import java.awt.event.MouseMotionListener; +import java.awt.event.WindowAdapter; +import java.awt.event.WindowEvent; +import javax.media.opengl.GL; +import javax.media.opengl.GL2; +import javax.media.opengl.GLAutoDrawable; +import javax.media.opengl.GLEventListener; +import javax.media.opengl.awt.GLCanvas; +import javax.media.opengl.awt.gl2.GL2JPanel; +import javax.media.opengl.util.Animator; /** * Gears.java <BR> @@ -18,7 +27,11 @@ public class Gears implements GLEventListener, MouseListener, MouseMotionListene Frame frame = new Frame("Gear Demo"); GLCanvas canvas = new GLCanvas(); - canvas.addGLEventListener(new Gears()); + final Gears gears = new Gears(); + canvas.addMouseListener(gears); + canvas.addMouseMotionListener(gears); + canvas.addGLEventListener(gears); + frame.add(canvas); frame.setSize(300, 300); final Animator animator = new Animator(canvas); @@ -35,7 +48,7 @@ public class Gears implements GLEventListener, MouseListener, MouseMotionListene }).start(); } }); - frame.show(); + frame.setVisible(true); animator.start(); } @@ -50,7 +63,7 @@ public class Gears implements GLEventListener, MouseListener, MouseMotionListene // Use debug pipeline // drawable.setGL(new DebugGL(drawable.getGL())); - GL gl = drawable.getGL(); + GL2 gl = drawable.getGL().getGL2(); System.err.println("INIT GL IS: " + gl.getClass().getName()); @@ -63,50 +76,48 @@ public class Gears implements GLEventListener, MouseListener, MouseMotionListene float green[] = { 0.0f, 0.8f, 0.2f, 1.0f }; float blue[] = { 0.2f, 0.2f, 1.0f, 1.0f }; - gl.glLightfv(GL.GL_LIGHT0, GL.GL_POSITION, pos, 0); - gl.glEnable(GL.GL_CULL_FACE); - gl.glEnable(GL.GL_LIGHTING); - gl.glEnable(GL.GL_LIGHT0); - gl.glEnable(GL.GL_DEPTH_TEST); + gl.glLightfv(GL2.GL_LIGHT0, GL2.GL_POSITION, pos, 0); + gl.glEnable(GL2.GL_CULL_FACE); + gl.glEnable(GL2.GL_LIGHTING); + gl.glEnable(GL2.GL_LIGHT0); + gl.glEnable(GL2.GL_DEPTH_TEST); /* make the gears */ gear1 = gl.glGenLists(1); - gl.glNewList(gear1, GL.GL_COMPILE); - gl.glMaterialfv(GL.GL_FRONT, GL.GL_AMBIENT_AND_DIFFUSE, red, 0); + gl.glNewList(gear1, GL2.GL_COMPILE); + gl.glMaterialfv(GL2.GL_FRONT, GL2.GL_AMBIENT_AND_DIFFUSE, red, 0); gear(gl, 1.0f, 4.0f, 1.0f, 20, 0.7f); gl.glEndList(); gear2 = gl.glGenLists(1); - gl.glNewList(gear2, GL.GL_COMPILE); - gl.glMaterialfv(GL.GL_FRONT, GL.GL_AMBIENT_AND_DIFFUSE, green, 0); + gl.glNewList(gear2, GL2.GL_COMPILE); + gl.glMaterialfv(GL2.GL_FRONT, GL2.GL_AMBIENT_AND_DIFFUSE, green, 0); gear(gl, 0.5f, 2.0f, 2.0f, 10, 0.7f); gl.glEndList(); gear3 = gl.glGenLists(1); - gl.glNewList(gear3, GL.GL_COMPILE); - gl.glMaterialfv(GL.GL_FRONT, GL.GL_AMBIENT_AND_DIFFUSE, blue, 0); + gl.glNewList(gear3, GL2.GL_COMPILE); + gl.glMaterialfv(GL2.GL_FRONT, GL2.GL_AMBIENT_AND_DIFFUSE, blue, 0); gear(gl, 1.3f, 2.0f, 0.5f, 10, 0.7f); gl.glEndList(); - gl.glEnable(GL.GL_NORMALIZE); + gl.glEnable(GL2.GL_NORMALIZE); - drawable.addMouseListener(this); - drawable.addMouseMotionListener(this); } public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) { - GL gl = drawable.getGL(); + GL2 gl = drawable.getGL().getGL2(); float h = (float)height / (float)width; - gl.glMatrixMode(GL.GL_PROJECTION); + gl.glMatrixMode(GL2.GL_PROJECTION); - System.err.println("GL_VENDOR: " + gl.glGetString(GL.GL_VENDOR)); - System.err.println("GL_RENDERER: " + gl.glGetString(GL.GL_RENDERER)); - System.err.println("GL_VERSION: " + gl.glGetString(GL.GL_VERSION)); + System.err.println("GL_VENDOR: " + gl.glGetString(GL2.GL_VENDOR)); + System.err.println("GL_RENDERER: " + gl.glGetString(GL2.GL_RENDERER)); + System.err.println("GL_VERSION: " + gl.glGetString(GL2.GL_VERSION)); gl.glLoadIdentity(); gl.glFrustum(-1.0f, 1.0f, -h, h, 5.0f, 60.0f); - gl.glMatrixMode(GL.GL_MODELVIEW); + gl.glMatrixMode(GL2.GL_MODELVIEW); gl.glLoadIdentity(); gl.glTranslatef(0.0f, 0.0f, -40.0f); } @@ -116,16 +127,16 @@ public class Gears implements GLEventListener, MouseListener, MouseMotionListene angle += 2.0f; // Get the GL corresponding to the drawable we are animating - GL gl = drawable.getGL(); + GL2 gl = drawable.getGL().getGL2(); // Special handling for the case where the GLJPanel is translucent // and wants to be composited with other Java 2D content - if ((drawable instanceof GLJPanel) && - !((GLJPanel) drawable).isOpaque() && - ((GLJPanel) drawable).shouldPreserveColorBufferIfTranslucent()) { - gl.glClear(GL.GL_DEPTH_BUFFER_BIT); + if ((drawable instanceof GL2JPanel) && + !((GL2JPanel) drawable).isOpaque() && + ((GL2JPanel) drawable).shouldPreserveColorBufferIfTranslucent()) { + gl.glClear(GL2.GL_DEPTH_BUFFER_BIT); } else { - gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT); + gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT); } // Rotate the entire assembly of gears based on how the user @@ -163,7 +174,7 @@ public class Gears implements GLEventListener, MouseListener, MouseMotionListene public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) {} - public static void gear(GL gl, + public static void gear(GL2 gl, float inner_radius, float outer_radius, float width, @@ -181,12 +192,12 @@ public class Gears implements GLEventListener, MouseListener, MouseMotionListene da = 2.0f * (float) Math.PI / teeth / 4.0f; - gl.glShadeModel(GL.GL_FLAT); + gl.glShadeModel(GL2.GL_FLAT); gl.glNormal3f(0.0f, 0.0f, 1.0f); /* draw front face */ - gl.glBegin(GL.GL_QUAD_STRIP); + gl.glBegin(GL2.GL_QUAD_STRIP); for (i = 0; i <= teeth; i++) { angle = i * 2.0f * (float) Math.PI / teeth; @@ -201,7 +212,7 @@ public class Gears implements GLEventListener, MouseListener, MouseMotionListene gl.glEnd(); /* draw front sides of teeth */ - gl.glBegin(GL.GL_QUADS); + gl.glBegin(GL2.GL_QUADS); for (i = 0; i < teeth; i++) { angle = i * 2.0f * (float) Math.PI / teeth; @@ -213,7 +224,7 @@ public class Gears implements GLEventListener, MouseListener, MouseMotionListene gl.glEnd(); /* draw back face */ - gl.glBegin(GL.GL_QUAD_STRIP); + gl.glBegin(GL2.GL_QUAD_STRIP); for (i = 0; i <= teeth; i++) { angle = i * 2.0f * (float) Math.PI / teeth; @@ -225,7 +236,7 @@ public class Gears implements GLEventListener, MouseListener, MouseMotionListene gl.glEnd(); /* draw back sides of teeth */ - gl.glBegin(GL.GL_QUADS); + gl.glBegin(GL2.GL_QUADS); for (i = 0; i < teeth; i++) { angle = i * 2.0f * (float) Math.PI / teeth; @@ -237,7 +248,7 @@ public class Gears implements GLEventListener, MouseListener, MouseMotionListene gl.glEnd(); /* draw outward faces of teeth */ - gl.glBegin(GL.GL_QUAD_STRIP); + gl.glBegin(GL2.GL_QUAD_STRIP); for (i = 0; i < teeth; i++) { angle = i * 2.0f * (float) Math.PI / teeth; @@ -265,10 +276,10 @@ public class Gears implements GLEventListener, MouseListener, MouseMotionListene gl.glVertex3f(r1 * (float)Math.cos(0), r1 * (float)Math.sin(0), -width * 0.5f); gl.glEnd(); - gl.glShadeModel(GL.GL_SMOOTH); + gl.glShadeModel(GL2.GL_SMOOTH); /* draw inside radius cylinder */ - gl.glBegin(GL.GL_QUAD_STRIP); + gl.glBegin(GL2.GL_QUAD_STRIP); for (i = 0; i <= teeth; i++) { angle = i * 2.0f * (float) Math.PI / teeth; |