import java.applet.*; import java.awt.*; import java.awt.event.*; import java.lang.*; import java.util.*; import java.io.*; import java.util.*; import gl4java.GLContext; import gl4java.awt.GLCanvas; import gl4java.utils.glut.*; public class hw2 extends Applet { hw2Canvas canvas = null; /** * void init() * * Initialise the applet. */ public void init() { Dimension d = getSize(); //We will use BorderLayout to layout the applet components setLayout(new BorderLayout()); //Create our canvas and add it to the center of the applet canvas = new hw2Canvas(d.width, d.height); add("Center", canvas); } /** * void start() * * Start the applet. */ public void start() { } /** * void stop() * * Stop the applet. */ public void stop() { } /* main, for when it is run from the command line. * Set up a window for the "applet" to run in. */ public static void main( String args[] ) { hw2 applet = new hw2(); Frame f = new Frame("CS4451A HW2"); f.addWindowListener( new WindowAdapter() { public void windowClosed(WindowEvent e) { System.exit(0); } public void windowClosing(WindowEvent e) { windowClosed(e); } }); f.setLayout(new BorderLayout()); f.add("Center", applet); applet.setSize(500,300); applet.init(); applet.start(); Dimension ps = applet.getPreferredSize(); f.setBounds(-100,-100,99,99); f.setVisible(true); f.setVisible(false); f.setVisible(true); Insets i = f.getInsets(); f.setBounds(0,0, ps.width+i.left+i.right, ps.height+i.top+i.bottom); f.setVisible(true); } /* GLCanvas class for this application. * We would use GLAnimCanvas because if we wanted the canvas * to be constantly redrawn. */ private class hw2Canvas extends GLCanvas implements MouseListener { int windW, windH; GLUTFunc glut = null; public hw2Canvas(int w, int h) { super(w, h); } /** * void preInit() * * Called just BEFORE the GL-Context is created. */ public void preInit() { //We want double buffering doubleBuffer = true; //But we dont want stereo view stereoView = false; } /** * void init() * * Called just AFTER the GL-Context is created. */ public void init() { // need a GLUT handle to create objects glut = new GLUTFuncLightImpl(gl, glu); windW = getSize().width; windH = getSize().height; reshape(getSize().width, getSize().height); // set up some lighting float light_ambient[] = {0.0f, 0.0f, 0.0f, 1.0f}; float light_diffuse[] = {1.0f, 1.0f, 1.0f, 1.0f}; float light_specular[] = {1.0f, 1.0f, 1.0f, 1.0f}; float light_position[] = {1.0f, 0.0f, 1.0f, 0.0f}; float global_ambient[] = {0.75f, 0.75f, 0.75f, 1.0f}; gl.glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient); gl.glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse); gl.glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular); gl.glLightfv(GL_LIGHT0, GL_POSITION, light_position); gl.glLightModelfv(GL_LIGHT_MODEL_AMBIENT, global_ambient); gl.glFrontFace(GL_CW); gl.glEnable(GL_LIGHTING); gl.glEnable(GL_LIGHT0); gl.glEnable(GL_AUTO_NORMAL); gl.glEnable(GL_NORMALIZE); gl.glDepthFunc(GL_LESS); gl.glEnable(GL_DEPTH_TEST); // for some reason I don't need to set the listener // addMouseListener(this); } /** * void reshape(int width, int height) * * Called after the first paint command and when the window is * resized by the user */ public void reshape(int width, int height) { windW = (int)width; windH = (int)height; // set the viewport to the entire window gl.glViewport(0, 0, windW, windH); } /** * void display() * * Draw to the canvas. */ public void display() { float color1[] = {0.4f, 0.1f, 0.2f, 1.0f}; float color2[] = {0.4f, 0.6f, 0.3f, 1.0f}; float color3[] = {0.1f, 0.5f, 0.5f, 1.0f}; //Ensure GL is initialised correctly if( glj.gljMakeCurrent() == false ) { System.out.println("problem in use() method"); return; } // save the current matrix: good practice, even though we // know it's not used for anything in our program gl.glPushMatrix(); // reset the project matrix gl.glMatrixMode(GL_PROJECTION); gl.glLoadIdentity(); // you need to use a perspective projection! glu.gluOrtho2D(-5, 5, -5, 5); gl.glMatrixMode(GL_MODELVIEW); // you need to specify a position and orientation for your camera // Clear The Screen and depth buffer gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); gl.glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // draw our "scene": a single teapot, sphere and cube for now gl.glMaterialfv(GL_FRONT, GL_AMBIENT, color1); gl.glPushMatrix(); gl.glTranslatef(0.0f, 3.0f, 0.0f); glut.glutSolidTeapot(1.0f); gl.glPopMatrix(); gl.glMaterialfv(GL_FRONT, GL_AMBIENT, color2); gl.glPushMatrix(); glut.glutSolidSphere(1.0f, 30, 30); gl.glPopMatrix(); gl.glMaterialfv(GL_FRONT, GL_AMBIENT, color3); gl.glPushMatrix(); gl.glTranslatef(0.0f, -3.0f, 0.0f); glut.glutSolidCube(2.0f); gl.glPopMatrix(); // restore the old matrix gl.glPopMatrix(); // swap buffers to display what we just rendered glj.gljSwap(); glj.gljFree(); } /* * Methods required for the implementation of MouseListener */ public void mouseEntered(MouseEvent evt) { } public void mouseExited(MouseEvent evt) { } public void mousePressed(MouseEvent evt) { } public void mouseReleased(MouseEvent evt) { int x1 = evt.getX(); int y1 = evt.getY(); repaint(); } public void mouseClicked(MouseEvent evt) { } } }