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.*; /** * Example Joogl Program #1 * * Draws a single sphere in the middle of the screen. */ public class ex1e extends Applet { /** * The class variable that holds the canvas, so we can access it * from inner classes. */ exampleCanvas canvas = null; jooglCamera camera = null; jooglClock clock = 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); clock = canvas.getClock(); /* retrieve the root object */ jooglRoot root = new jooglRoot(); canvas.addRoot(root); /* create a camera */ camera = new jooglPerspectiveCamera(0.0, 0.0, 15.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); camera.setTransform(new jooglTransform().translate(0.0, 0.0, 0.0)); root.add(camera); /* 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 a directional light and add it to the scene graph. */ jooglLight light1 = new jooglDirectionalLight(0.2, 1.0, 0.4, /* green-tinted */ -2.0, 0.0, 1.0); /* from the +x axis */ root.add(light1); /* create a positional light and add it to the scene graph. */ jooglLight light2 = new jooglPositionalLight(0.4, 1.0, 3.4, /* blue-tinted */ 5.0, 0.0, 4.0, /* from the +x axis */ 1.0, 0.2, 0.0); root.add(light2); /* create a group and add it to the root. */ jooglGroup group = new jooglGroup(); root.add(group); /* create a simple sphere with some properties, and put it in the group. */ jooglSphere sphere = new jooglSphere(4.0, 30); sphere.setMaterial(new jooglMaterial(1.0, 0.5, 0.5)); group.add(sphere); } public void runTime() { jooglTransform trans = camera.getTransform(); while (true) { double tc = Math.cos((double)clock.getTime()/250.0); double ts = Math.sin((double)clock.getTime()/250.0); trans.setIdentity(); trans.translate(tc*2.0, ts*2.0, 0.0); try {Thread.sleep(5);} catch (Exception c) {} camera.setTransform(trans); // so it will redraw } } /** 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[] ) { ex1e applet = new ex1e(); Frame f = new Frame("CS4451A Joogl Example 1E"); 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); applet.runTime(); } /** jooglCanvas class for this application. */ private class exampleCanvas extends jooglCanvas { public exampleCanvas(int w, int h) { super(w, h); } } }