import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import javax.media.opengl.GL; import javax.media.opengl.GLAutoDrawable; import javax.media.opengl.GLCanvas; import javax.media.opengl.GLCapabilities; import javax.media.opengl.GLEventListener; import javax.media.opengl.GLJPanel; import javax.media.opengl.glu.GLU; import javax.swing.JFrame; import java.lang.Math; import com.sun.opengl.util.GLUT; public class SolarSystem // extends glskeleton// implements GLEventListener// , KeyListener // { private GLU glu; private GLUT glut; private double year = 0; private double month = 0; public SolarSystem() { } public static void main(String[] args) { GLCapabilities caps = new GLCapabilities(); GLJPanel canvas = new GLJPanel(caps); SolarSystem demo = new SolarSystem(); canvas.addGLEventListener(demo); demo.setCanvas(canvas); demo.setDefaultListeners(demo); JFrame frame = new JFrame("SolarSystem"); frame.setSize(800, 800); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(canvas); frame.setVisible(true); canvas.requestFocusInWindow(); } public void init(GLAutoDrawable drawable) { GL gl = drawable.getGL(); glu = new GLU(); glut = new GLUT(); gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); gl.glShadeModel(GL.GL_FLAT); } public void display(GLAutoDrawable drawable) { GL gl = drawable.getGL(); gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT); gl.glMatrixMode(GL.GL_MODELVIEW); gl.glLoadIdentity(); gl.glColor3f (1.0f, 0.0f, 0.0f); glut.glutWireSphere(1.0, 50, 50); /* draw sun */ gl.glLoadIdentity(); gl.glRotated(this.year, 0.0, 1.0, 0.0); gl.glTranslatef (2.0f, 0.0f, 0.0f); gl.glColor3f(0.0f,0.0f,1.0f); glut.glutWireSphere(0.2, 20, 20); /* draw earth */ gl.glLoadIdentity(); gl.glTranslatef((float)(2.0*Math.cos(this.year*2*Math.PI/360)),0.0f, (float)(2.0*Math.sin(-this.year*2*Math.PI/360))); gl.glRotatef((float)month, 0.0f, 1.0f, 0.0f); gl.glTranslatef((float)(-2.0*Math.cos(this.year*2*Math.PI/360)),0.0f,(float)(-2.0*Math.sin(-this.year*2*Math.PI/360))); gl.glRotatef((float)year, 0.0f, 1.0f, 0.0f); gl.glTranslatef(2.5f, 0.0f, 0.0f); gl.glColor3f(0.0f,1.0f,0.0f); glut.glutWireSphere(0.1, 10, 10); /* draw moon */ gl.glFlush(); } public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) { GL gl = drawable.getGL(); gl.glViewport(0, 0, w, h); gl.glMatrixMode(GL.GL_PROJECTION); gl.glLoadIdentity(); glu.gluPerspective(90.0, (float)w/(float)h, 1.0, 40.0); //Change the parameters to see the result glu.gluLookAt(0,0,5, 0,0,0, 0,1,0); } public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) { } public void keyTyped(KeyEvent arg0) { } public void keyPressed(KeyEvent key) { switch (key.getKeyCode()) { case KeyEvent.VK_ESCAPE: super.runExit(); case KeyEvent.VK_A: this.year += 5; this.month +=15; break; case KeyEvent.VK_D: this.year -= 5; this.month -= 15; default: break; } super.refresh(); } public void keyReleased(KeyEvent arg0) { } }