import java.awt.*; public class SphereApplet extends BufferedApplet { int w, h; // width/height of applet window Sphere sphere; MatrixStack stack; int M = 10, N = 10; int d = 0; public void render(Graphics g) { 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 sphere = new Sphere(M, N); stack = new MatrixStack(); Matrix3D t = stack.get(); t.rotateY(d * Math.PI / 180); t.translate(0,.75,0); stack.push(t); // initial rotation t.scale(.30, .30, .30); t.translate(0, -0.750, 0); stack.push(t); // scale balls t.translate(0.8, 0, 0); stack.push(t); // translate right double[][][] ballRight = sphere.transformedBy(stack.get()); drawMesh(g, ballRight); stack.pop(); // pop translate right t = stack.get(); t.translate(-0.8, 0, 0); stack.push(t); // translate left double[][][] ballLeft = sphere.transformedBy(stack.get()); drawMesh(g, ballLeft); stack.pop(); // pop translate left t = stack.get(); stack.pop(); // pop scale for balls t = stack.get(); t.rotateX((Math.PI / 8) * Math.sin(d / (2 * Math.PI)) + (Math.PI / 4)); t.translate(0,0,0.5); //translate for shaft stack.push(t); t.scale(0.3, 0.2, 0.5); // scale for topShaft stack.push(t); double[][][] topShaft = sphere.transformedBy(stack.get()); drawMesh(g, topShaft); stack.pop(); // pop scale for topShaft t = stack.get(); t.rotateX((Math.PI / 16) * Math.sin(d / (2 * Math.PI)) + (Math.PI / 4)); t.translate(0,0.25,0.6); stack.push(t); t.scale(0.3, 0.2, 0.5); stack.push(t); double[][][] lowerShaft = sphere.transformedBy(stack.get()); drawMesh(g, lowerShaft); stack.pop(); // pop scale for lower shaft t = stack.get(); t.translate(0,0,0.37); t.scale(0.23 + 0.02 * Math.sin(d / Math.PI), 0.23 + 0.02 * Math.sin(d / Math.PI), 0.23 + 0.02 * Math.sin(d / Math.PI)); stack.push(t); double [][][] head = sphere.transformedBy(stack.get()); drawMesh(g, head); stack.pop(); t = stack.get(); stack.pop(); t = stack.get(); //pop translate for lower shaft stack.pop(); // pop translate for shaft t = stack.get(); stack.pop(); // pop initial rotation t = stack.get(); d++; } private void drawMesh(Graphics g, double[][][] mesh) { for (int i = 0; i < M - 1; i++) for (int j = 0; j < N - 1; j++) { drawTriangle(g, mesh[i][j], mesh[i + 1][j], mesh[i + 1][j + 1]); drawTriangle(g, mesh[i][j], mesh[i + 1][j + 1], mesh[i][j + 1]); } } private void drawTriangle(Graphics g, double[] p, double[] q, double[] r) { g.drawLine(screenX(p[0]), screenY(p[1]), screenX(q[0]), screenY(q[1])); g.drawLine(screenX(q[0]), screenY(q[1]), screenX(r[0]), screenY(r[1])); g.drawLine(screenX(r[0]), screenY(r[1]), screenX(p[0]), screenY(p[1])); } // CONVERT X COORDINATE TO SCREEN PIXELS int screenX(double t) { return w / 2 + (int) (t * w / 4); } // CONVERT Y COORDINATE TO SCREEN PIXELS int screenY(double t) { return h / 2 - (int) (t * w / 4); } }