could someone please tell me why my program, which for now simply rotates a cylinder, crashes after 2 seconds? Here it is:
import java.awt.*;
import java.awt.event.*;
import net.java.games.jogl.*;
public class Pipes extends Frame{
Animator animator;
GLCanvas canvas;
Pipes pipeFrame;
public Pipes(){
setTitle("3D Pipes program --Daniel D. Marino");
canvas = GLDrawableFactory.getFactory().createGLCanvas(new GLCapabilities());
canvas.addGLEventListener(new Renderer());
add(canvas);
animator = new Animator(canvas);
setSize(640, 480);
setResizable(false);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
animator.stop();
System.exit(0);
}
});
setVisible(true);
animator.start();
canvas.requestFocus();
pipeFrame = this;
}
private class Renderer implements GLEventListener{
private float rquad = 0.0f;
private float rtri = 0.0f;
//GLDrawable is the OpenGL canvas?
public void display(GLDrawable glDrawable){
final GL gl = glDrawable.getGL();
final GLU glu = glDrawable.getGLU();
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
/*
gl.glLoadIdentity();
gl.glTranslatef(-1.5f, 0.0f, -6.0f);
gl.glRotatef(rtri, 0.0f, 1.0f, 0.0f);
gl.glBegin(GL.GL_TRIANGLES); // Drawing Using Triangles
gl.glColor3f(1.0f, 0.0f, 0.0f); // Set the current drawing color to red
gl.glVertex3f( 0.0f, 1.0f, 0.0f); // Top
gl.glColor3f(0.0f, 1.0f, 0.0f); // Set the current drawing color to green
gl.glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left
gl.glColor3f(0.0f, 0.0f, 1.0f); // Set the current drawing color to blue
gl.glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right
gl.glEnd(); // Finished Drawing The Triangle
gl.glLoadIdentity();
gl.glTranslatef(1.5f, 0.0f, -6.0f);
gl.glRotatef(rquad, 1.0f, 0.0f, 0.0f);
gl.glBegin(GL.GL_QUADS); // Draw A Quad
gl.glColor3f(0.5f, 0.5f, 1.0f); // Set the current drawing color to light blue
gl.glVertex3f(-1.0f, 1.0f, 0.0f); // Top Left
gl.glVertex3f( 1.0f, 1.0f, 0.0f); // Top Right
gl.glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right
gl.glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left
gl.glEnd(); // Done Drawing The Quad
*/
gl.glLoadIdentity();
gl.glTranslatef(1.5f, 0.0f, -16.0f);
gl.glRotatef(rtri, 0.0f, 1.0f, 0.0f);
//gl.glBegin(GL.GL_QUADS);
gl.glColor3f(0.0f, 0.0f, 1.0f);
GLUquadric quad = new GLUquadric();
glu.gluQuadricDrawStyle(quad, glu.GLU_FILL);
glu.gluCylinder(quad, 1, 1, 3, 23, 1);
//glDrawable.getGLU().gluCylinder(new GLUquadric(), 5, 5, 1, 20, 4);
//gl.glEnd();
gl.glFlush();
rtri += 0.2f;
rquad += 0.15f;
}
public void drawStraightPipe(GLDrawable glDrawable, int x, int y) {
final GL gl=glDrawable.getGL();
final GLU glu=glDrawable.getGLU();
gl.glLoadIdentity();
gl.glTranslatef((float)(x*10.0f), (float)(y*10.0f), 0.0f);
glu.gluCylinder(new GLUquadric(), 5.0, 5.0, 10.0, 10, 1);
}
//never use this
public void displayChanged(GLDrawable gLDrawable, boolean modeChanged,
boolean deviceChanged){
}
/** Called by the drawable immediately after the OpenGL context is
* initialized for the first time. Can be used to perform one-time OpenGL
* initialization such as setup of lights and display lists.
* @param gLDrawable The GLDrawable object.
*/
public void init(GLDrawable gLDrawable){
final GL gl = gLDrawable.getGL();
gl.glShadeModel(GL.GL_SMOOTH); // Enable Smooth Shading
gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // Black Background
gl.glClearDepth(1.0f); // Depth Buffer Setup
gl.glEnable(GL.GL_DEPTH_TEST); // Enables Depth Testing
gl.glDepthFunc(GL.GL_LEQUAL); // The Type Of Depth Testing To Do
gl.glHint(GL.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST); // Really Nice Perspective Calculations
gLDrawable.addKeyListener(new KeyListener(){
public void keyPressed(KeyEvent e){
}
public void keyReleased(KeyEvent e){
}
public void keyTyped(KeyEvent e){
}
});
}
public void reshape(GLDrawable gLDrawable, int x, int y, int width, int height){
final GL gl = gLDrawable.getGL();
final GLU glu = gLDrawable.getGLU();
if (height <= 0) height = 1; // avoid a divide by zero error!
final float h = (float)width / (float)height;
//gl.glViewport(0, 0, width, height); //unnecessary
gl.glMatrixMode(GL.GL_PROJECTION);
gl.glLoadIdentity();
glu.gluPerspective(45.0f, h, 1.0, 20.0);
gl.glMatrixMode(GL.GL_MODELVIEW);
gl.glLoadIdentity();
}
}
public static void main(String[] args){
Pipes pipes = new Pipes();
}
}