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 ex3b extends Applet { exampleCanvas canvas = null; jooglClock rtClock = 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 exampleCanvas(d.width, d.height); add("Center", canvas); rtClock = new jooglManualClock(); canvas.setClock(rtClock); /* create a camera for each view */ jooglCamera camera1 = new jooglPerspectiveCamera(-10.0, -10.0, -30.0, /* eye is at (-10,-10,-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 */ jooglCamera camera2 = new jooglPerspectiveCamera(10.0, 10.0, 30.0, /* eye is at (10,10,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 degrees */ 1.0, /* Z near */ 100.0);/* Z far */ /* retrieve the root object */ jooglRoot root1 = new jooglRoot(0.01f, 0.01f, 0.48f, 0.98f); jooglRoot root2 = new jooglRoot(0.51f, 0.01f, 0.48f, 0.98f); root1.setShadeMode(new jooglShadeMode(jooglShadeMode.SMOOTH)); root1.setCamera(camera1); root2.setShadeMode(new jooglShadeMode(jooglShadeMode.FLAT)); root2.setCamera(camera2); canvas.addRoot(root1); canvas.addRoot(root2); jooglGroup root = new jooglGroup(); root1.add(root); root2.add(root); /* create some ambient lights and add them to the scene graph. */ jooglAmbientLight lightAmb = new jooglAmbientLight(0.02, 0.02, 0.2); root1.add(lightAmb); lightAmb = new jooglAmbientLight(0.2, 0.01, 0.01); root2.add(lightAmb); /* First, create two groups, and set them to rotating about the Y and Z axes. * Add the top group to the root. */ jooglGroup group1 = new jooglGroup(); jooglGroup group3 = new jooglGroup(); root.add(group3); /* Add a group to the root. */ group3.add(group1); group3.setTransform(new jooglDynamicTransform() { public double[] getValue(long time) { double ry = (time/3333.0)*360.0; setIdentity(); rotateY(ry); return matrix; } }); group1.setTransform(new jooglDynamicTransform() { public double[] getValue(long time) { double rz = (time/2000.0)*360.0; setIdentity(); rotateZ(rz); return matrix; } }); /*********************** * Create two point light sources, with little icons for them (small spheres) ***********************/ /* Next add a small green sphere to group1, and translate it to (6,3,3) */ jooglSphere sphere1 = new jooglSphere(0.5, 10); sphere1.setTransform(new jooglTransform().translate(6.0, 3.0, 3.0)); sphere1.setMaterial(new jooglMaterial(0.5, 1.0, 0.5, 0.8)); jooglPositionalLight light1 = new jooglPositionalLight(0.6, 1.5, 0.4, /* green-tinted */ 6.0, 3.0, 3.0, /* out on the x axis */ 1.0, 0.5, 0.0); group1.add(sphere1); group1.add(light1); /* Next add a small green sphere to group1, and translate it to (6,3,3) */ jooglSphere sphere2 = new jooglSphere(0.5, 10); sphere2.setTransform(new jooglTransform().translate(-6.0, -3.0, -3.0)); sphere2.setMaterial(new jooglMaterial(1.0, 0.5, 0.5, 0.8)); jooglPositionalLight 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.5, 0.0); group1.add(sphere2); group1.add(light2); /* create a group and add it to the root. */ jooglGroup group2 = new jooglGroup(); root.add(group2); /* create a simple sphere with some properties, and put it in the group. */ jooglSphere sphere = new jooglSphere(3.0, 30); sphere.setMaterial(new jooglMaterial(1.0, 0.5, 1.0)); group2.add(sphere); jooglGroup group4 = new jooglGroup(); group4.setTransform(new jooglTransform().translate(0.0, -3.0, 0.0)); group4.add(sphere); group2.add(group4); jooglGroup group5 = new jooglGroup(); group5.setTransform(new jooglTransform().translate(0.0, 3.0, 0.0)); group5.add(sphere); group2.add(group5); } /** * 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[] ) { ex3b applet = new ex3b(); Frame f = new Frame("CS4451A Joogl Example 3B"); 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); while (true) try { Thread.sleep(1000); } catch (Exception e) {}; } /* 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 long t0; protected void newposition(int x) { long dx = ((long)(x - x0))*2; rtClock.setTime(t0 + dx); } /* * Methods required for the implementation of MouseMotionListener */ public void mouseMoved(MouseEvent evt) { } public void mouseDragged(MouseEvent evt) { int x1 = evt.getX(); newposition(x1); 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(); t0 = rtClock.getTime(); } public void mouseReleased(MouseEvent evt) { int x1 = evt.getX(); newposition(x1); repaint(); } public void mouseClicked(MouseEvent evt) { } } }