/* * Created on 01/08/2006 * * TODO To change the template for this generated file go to * Window - Preferences - Java - Code Style - Code Templates */ /** * Sphere.java * This programa shows a wireframed sphere */ /** * @author ting * * TODO To change the template for this generated type comment go to * Window - Preferences - Java - Code Style - Code Templates */ import java.awt.*; import java.awt.event.*; import javax.swing.*; import com.sun.opengl.util.*; import javax.media.opengl.*; import javax.media.opengl.glu.*; public class Sphere // extends JFrame // implements GLEventListener,KeyListener { public static final Dimension PREFERRED_FRAME_SIZE = new Dimension(350, 350); private GLUT glut; /** Constructor. */ public Sphere () { // init JFrame super ("Sphere - JMDM version "); System.out.println("Constructor"); } /** Dimension of the window 350x350. */ public Dimension getPreferredSize(){ return PREFERRED_FRAME_SIZE; } /* * METHODS DEFINED BY GLEventListener */ /** Called by drawable to initiate drawing. */ public void display(GLAutoDrawable drawable) { GL gl = drawable.getGL(); GLU glu = new GLU(); GLUT glut = new GLUT(); /* clear all pixels */ gl.glClear (GL.GL_COLOR_BUFFER_BIT); /* clear the modeling stack matrix */ gl.glLoadIdentity(); /* set the observer */ glu.gluLookAt(0.0f, 0.0f, 3.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f); /* draw a white */ gl.glColor3f (1.0f, 1.0f, 1.0f); /* wireframed sphere */ glut.glutWireSphere(1.0f, 20, 20); /* don't wait! * start processing buffered OpenGL routines */ gl.glFlush (); } /** Called by drawable to indicate mode or device has changed. */ public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged){} /** Called after OpenGL is init'ed */ public void init(GLAutoDrawable drawable) { System.out.println("init()"); GL gl = drawable.getGL(); System.err.println("INIT GL IS: " + gl.getClass().getName()); 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)); /* select clearing color */ gl.glClearColor (0.0f, 0.0f, 0.0f, 0.0f); /* set polygon drawing mode to the front and back face */ /* gl.glPolygonMode(GL.GL_FRONT_AND_BACK, GL.GL_LINE); */ /* initialize viewing values */ gl.glMatrixMode(GL.GL_PROJECTION); /* clear the projection stack matrix */ gl.glLoadIdentity(); gl.glFrustum (-1.5f, 1.5f, -1.5f, 1.5f, 1.5f, 20.0f); /* restore the modeling matrix mode */ gl.glMatrixMode (GL.GL_MODELVIEW); /* add the key listener */ drawable.addKeyListener(this); } /** Called to indicate the drawing surface has been moved and/or resized. */ public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height){} /* * OUR HELPER METHODS */ // Methods required for the implementation of KeyListener public void keyPressed(KeyEvent e){ System.out.println("Key pressed"); switch (e.getKeyChar()) { case 27: /* Escape Key */ /* exit */ System.exit(0); break; } } public void keyReleased(KeyEvent e){} public void keyTyped(KeyEvent e){} /** main creates and shows a Hello-JFrame * * Declare initial window size, position, and display mode * (single buffer and RGBA). Open window with "hello" * in its title bar. Call initialization routines. * Register callback function to display graphics. * Enter main loop and process events. */ public static void main(String[] args){ Sphere g = new Sphere(); //Set frame location g.setLocation(100,100); GLCapabilities gl_c = new GLCapabilities(); // Disable double buffer gl_c.setDoubleBuffered(false); // get a GLCanvas GLCanvas canvas = new GLCanvas(gl_c); // add a GLEventListener, which will get called when the // canvas is resized or needs a repaint canvas.addGLEventListener(g); // now add the canvas to the JFrame. Note we use BorderLayout.CENTER // to make the canvas stretch to fill the container (ie, the frame) g.getContentPane().add(canvas, BorderLayout.CENTER); g.pack(); g.setVisible(true); } }