import java.awt.*; import graphics.Sphere; import graphics.Cone; public class hw3 extends ThreeDApplet { //how many spheres can be on screen at once final int MAX_SPHERES = 10; //how many steps until a sphere fades away final int FADE_STEPS = 25; final double GROW_STEP = 0.1; final double MAX_GROW_LEVEL = 10.0; //unit sphere - all parametric calculations done in this sphere //which will be cloned and transformed Sphere unitSphere; //will be reused via clone() //actual spheres to be drawn Sphere[] spheres; //same deal with cone, disk Cone unitCone; Cone cursor; //keeps track of which spheres are fading boolean[] fading; //keeps track of how far along spheres are in fading int[] fadeLevel; boolean[] growing; double[] growLevel; int lastSphereIndex; //index of last sphere created public void init() { super.init(); unitSphere = new Sphere(); unitSphere.calculate(); //unitSphere.dumpFaces(); unitCone = new Cone(10, 2); unitCone.calculate(); spheres = new Sphere[MAX_SPHERES]; for (int i=0; i FADE_STEPS) //is it faded all the way? { fading[index] = false; spheres[index] = null; //remove sphere from scene return; } } //do transformations s.matrix.identity(); s.matrix.scale(0.25, 0.25, 0.25); //apply growLevel scaling even if it's not growing (so it doesn't jump back to original size) s.matrix.scale(growLevel[index], growLevel[index], growLevel[index]); //is the sphere growing? if (growing[index]) { growLevel[index] += GROW_STEP; if (growLevel[index] > MAX_GROW_LEVEL) growLevel[index] = MAX_GROW_LEVEL; } double theta = 0.025*count; s.matrix.rotateY(theta); s.matrix.rotateX(Math.PI/2); //move sphere to where it's supposed to be s.matrix.translate(1.75*s.center[0], 1.75*s.center[1], 0); s.transform(); } //move cursor with mouse - scale coordinates //so (0, 0) is center, (1.0, 1.0) is top-right, etc. public boolean mouseMove(Event e, int x, int y) { double[] point = screenTo3D(x, y); //this fn might be called b4 cursor is initialized if (cursor != null) { cursor.center[0] = point[0]; cursor.center[1] = point[1]; } return true; } public boolean mouseDown(Event e, int x, int y) { double[] point = screenTo3D(x, y); //System.err.println("mouse clicked: ("+point[0]+", "+point[1]+")"); addSphere(point[0], point[1]); return true; } public boolean mouseUp(Event e, int x, int y) { if (lastSphereIndex < 0) //should never happen { System.err.println("Warning: lastSphereIndex = " + lastSphereIndex); return false; } //last sphere added is now fading, not growing growing[lastSphereIndex] = false; fading[lastSphereIndex] = true; fadeLevel[lastSphereIndex] = 0; //reset fade level return true; } //opposite of projecting/rendering - turns screen coordinates into 3D coordinates //(sort of) private double[] screenTo3D(int x, int y) { double[] point = new double[2]; point[0] = 2.0 * (x - w/2.0) / w; point[1] = -2.0 * (y - h/2.0) / h; //flip y-axis return point; } }