import java.awt.*; public class hw2 extends BufferedApplet { long time0; boolean doUpDown = true; boolean doLR = true; boolean doScale = true; //define points to make things a little cleaner double origin[] = {0, 0, 0}; double blf[] = {-1, -1, -1}; //(b)ottom-(l)eft-(f)ront double brf[] = { 1, -1, -1}; //(b)ottom-(r)ight-(f)ront double blb[] = {-1, -1, 1}; //(b)ottom-(l)eft-(b)ack double brb[] = { 1, -1, 1}; //(b)ottom-(l)eft-(b)ack double tlf[] = {-1, 1, -1}; //(t)op-(l)eft-(f)ront double trf[] = { 1, 1, -1}; //(t)op-(r)ight-(f)ront double tlb[] = {-1, 1, 1}; //(t)op-(l)eft-(b)ack double trb[] = { 1, 1, 1}; //(t)op-(l)eft-(b)ack //define how points fit together later in init() double tetra1[][]; double tetra2[][]; double hourglass[][][]; // = {dec1, dec2}; double a[] = {0,0,0}, b[] = {0,0,0}; int w, h, count = 0; double t = 0; //transformation matrix Matrix3D m; public void init() { super.init(); //set up geometry initPyramids(); time0 = System.currentTimeMillis(); //start the clock m = new Matrix3D(); } public void render(Graphics g) { count = tick(); //set clock w = bounds().width; // FIND OUT HOW BIG THE APPLET WINDOW IS h = bounds().height; g.setColor(Color.white); // MAKE A CLEAR WHITE BACKGROUND g.fillRect(0,0,w,h); g.setColor(Color.black); // SET THE DRAWING COLOR TO BLACK animate(); // DO ANIMATION CALCULATIONS for (int i = 0 ; i < hourglass.length ; i++) // LOOP THROUGH ALL THE SHAPES for (int j = 1 ; j < hourglass[i].length ; j++) { // LOOP THROUGH ALL THE LINES IN THE SHAPE m.transform(hourglass[i][j-1], a); // TRANSFORM BOTH ENDPOINTS OF LINE m.transform(hourglass[i][j ], b); g.drawLine(x(a[0]), y(a[1]), x(b[0]), y(b[1])); // DRAW ONE LINE ON THE SCREEN } animating = true; } void animate() { m.identity(); //double theta = 0.25 * Math.cos(.1 * count); double theta = Math.PI/4; //double theta = 0.1*count; m.rotateY(theta); m.rotateX(-1*theta/2); double sz = 1.0; if (doScale) sz = 0.8*Math.abs(Math.sin(.1 * count)) + 0.2; m.scale(1.0, 1.0, sz); double tx = 0; double ty = 0; if (doUpDown) ty = 0.6*Math.cos(.2 * count); double tz = 0; if (doLR) tz = 0.6*Math.sin(.1 * count); m.translate(tx, ty, tz); } //event handling public boolean keyDown(java.awt.Event evt, int key) { if (key == 32) //space doScale = !doScale; if (key == Event.UP || key == Event.DOWN) doUpDown = !doUpDown; if (key == Event.LEFT || key == Event.RIGHT) doLR = !doLR; return true; } int x(double t) { return w/2 + (int)(t*w/4); } // CONVERT X COORDINATE TO SCREEN PIXELS int y(double t) { return h/2 - (int)(t*w/4); } // CONVERT Y COORDINATE TO SCREEN PIXELS private int tick() { long time = System.currentTimeMillis(); double elapsed = (time - time0) / 1000.0; return (int)(30 * elapsed); } private void initPyramids() { tetra1 = new double[12][3]; tetra1[0] = origin; tetra1[1] = blf; tetra1[2] = brf; tetra1[3] = origin; tetra1[4] = brf; tetra1[5] = brb; tetra1[6] = origin; tetra1[7] = brb; tetra1[8] = blb; tetra1[9] = origin; tetra1[10] = blb; tetra1[11] = blf; tetra2 = new double[12][3]; tetra2[0] = origin; tetra2[1] = tlf; tetra2[2] = trf; tetra2[3] = origin; tetra2[4] = trf; tetra2[5] = trb; tetra2[6] = origin; tetra2[7] = trb; tetra2[8] = tlb; tetra2[9] = origin; tetra2[10] = tlb; tetra2[11] = tlf; hourglass = new double[2][12][3]; hourglass[0] = tetra1; hourglass[1] = tetra2; } }