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 ex4a extends Applet { exampleCanvas canvas = null; jooglRealTimeClock rtClock = null; jooglTransform tempTrans, permTrans; /** * 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 exampleCanvas(d.width, d.height); add("Center", canvas); /* retrieve the root object */ jooglRoot root = new jooglRoot(); canvas.addRoot(root); rtClock = ((jooglRealTimeClock)(canvas.getClock())); /* create a camera */ jooglCamera camera = new jooglPerspectiveCamera(0.0, 0.0, 30.0, /* eye is at (0,0,30) */ 0.0, 0.0, 0.0, /* center is at (0,0,0) */ 0.0, 1.0, 0.0, /* up is in positive Y direction */ 40.0, /* field of view in degree */ 1.0, /* Z near */ 100.0);/* Z far */ root.setCamera(camera); root.setShadeMode(new jooglShadeMode(jooglShadeMode.SMOOTH)); /* create an ambient light and add it to the scene graph. */ jooglAmbientLight lightAmb = new jooglAmbientLight(0.2, 0.2, 0.2); root.add(lightAmb); /*********************** * Create two point light sources, with little icons for them (small spheres) ***********************/ jooglGroup group1 = new jooglGroup(); root.add(group1); /* Next add a small green sphere to group1, and translate it to (6,3,3) */ jooglSphere sphere1 = new jooglSphere(0.25, 10); sphere1.setTransform(new jooglTransform().translate(8.0, 0.0, 5.0)); sphere1.setMaterial(new jooglMaterial(0.2, 1.0, 0.2, 0.8)); jooglLight light1 = new jooglSpotLight(0.6, 3.5, 0.4, /* green-tinted */ 8.0, 0.0, 5.0, /* out on the x axis */ -6.0, 0.0, -5.0, 1.0, 0.3, 0.1, 25.0, 2.0); group1.add(sphere1); group1.add(light1); /* Next add a small red sphere to group1, and translate it to (-6,-3,-3) */ jooglSphere sphere2 = new jooglSphere(0.25, 10); sphere2.setTransform(new jooglTransform().translate(-6.0, -3.0, -3.0)); sphere2.setMaterial(new jooglMaterial(1.0, 0.2, 0.2, 0.8)); jooglLight light2 = new jooglPositionalLight(1.5, 0.4, 0.6, /* red-tinted */ -6.0, -3.0, -3.0, /* out on the x axis */ 1.0, 0.0, 0.0); group1.add(sphere2); group1.add(light2); /* create a group and add it to the root. */ jooglGroup group3 = new jooglGroup(); jooglGroup group2 = new jooglGroup(); root.add(group3); group3.add(group2); permTrans = new jooglTransform(); tempTrans = new jooglTransform(); group2.setTransform(permTrans); group3.setTransform(tempTrans); jooglDrawMode[] dm = new jooglDrawMode[3]; dm[0] = new jooglDrawMode(jooglDrawMode.LINE); dm[1] = new jooglDrawMode(jooglDrawMode.POINT); dm[2] = new jooglDrawMode(jooglDrawMode.FILL); for (int i = 0; i<3; i++) { jooglGeometry obj = new jooglCube(2.0); obj.setDrawMode(dm[i]); jooglTransform t = new jooglTransform().translate(5.0 - 6.0 * i , 5.0, 0.0); t.rotateY(-45.0); obj.setTransform(t); group2.add(obj); obj = new jooglTeapot(2.0); obj.setDrawMode(dm[i]); t = new jooglTransform().translate(5.0 - 6.0 * i , 0.0, 0.0); t.rotateY(30.0); obj.setTransform(t); group2.add(obj); obj = new jooglSphere(1.5,30); obj.setDrawMode(dm[i]); t = new jooglTransform().translate(5.0 - 6.0 * i , -5.0, 0.0); obj.setTransform(t); group2.add(obj); } } /** * 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[] ) { ex4a applet = new ex4a(); Frame f = new Frame("CS4451A Joogl Example 4A"); 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 exampleCanvas extends jooglCanvas implements MouseListener, MouseMotionListener { public exampleCanvas(int w, int h) { super(w, h); } /** * void preInit() * * Called just BEFORE the GL-Context is created. */ public void preInit() { // initialize joogl super.preInit(); addMouseMotionListener(this); } protected int x0; protected int y0; protected void newposition(int x, int y) { float dx = ((float)(x - x0))/5.0f; float dy = ((float)(y - y0))/5.0f; tempTrans.setIdentity(); tempTrans.rotateX(dy); tempTrans.rotateY(-dx); } /* * Methods required for the implementation of MouseMotionListener */ public void mouseMoved(MouseEvent evt) { } public void mouseDragged(MouseEvent evt) { int x1 = evt.getX(); int y1 = evt.getY(); newposition(x1, y1); repaint(); } /* * Methods required for the implementation of MouseListener */ public void mouseEntered(MouseEvent evt) { } public void mouseExited(MouseEvent evt) { } public void mousePressed(MouseEvent evt) { x0 = evt.getX(); y0 = evt.getY(); } public void mouseReleased(MouseEvent evt) { int x1 = evt.getX(); int y1 = evt.getY(); newposition(x1, y1); permTrans.multiply(tempTrans); tempTrans.setIdentity(); repaint(); } public void mouseClicked(MouseEvent evt) { } } }