Homework 3

When you have finished the assignment below, email your source code to the grader at wanghua@cs.nyu.edu, and post the working applet onto the web. Make sure the grader knows the URL of your web site. Remember, you have an extra week to do this assignment, since we have no class next Monday. I'm sure you'll all have it done before next Monday anyway :-)

I've provided a very simple Applet which conforms to the Java 1.0.2 spec, below, for the benefit of those of you who've never used Java before. I tried to err on the side of simplicity, to make it easy for you to get started, but I implemented double-buffering for you, so you won't get annoying flicker in your applets. All my applet does is let you drag the mouse around to move a big 'X' shape. You can compile it with: javac MySimpleApplet.java (or use jvc if you're on a PC) and you can run it by loading file MySimpleApplet.html in your Web browser.

There is an on-line API for Java 1.0.2, which you should definitely make use of (I do, continuously).


  1. Cube:

    Make an applet which displays a cube as 12 vectors, and lets the user drag the mouse around to rotate the cube. Use the Java classes that you implemented last week to do the 3D matrix and vector math. Think of the mouse as a simulated track-ball, and try to make your program rotate the cube in a way which feels intuitive to the person dragging the mouse.

  2. Sphere:

    Make an applet which displays a sphere as a parametric mesh of longitude (o) and latitude (ø). Use the parametric function:

    Your applet should allow the user to rotate the sphere using the method you developed for the cube, above. Also, let the user hit numeric keys to change the number of mesh subdivisions of the sphere. When the user hits any digit d, where 1 <= d <= 9, you should produce 2*d latitute segments from south pole to north pole, and 4*d longitude segments around the equator.

    You can get keyboard events by overriding event-handler method:

       public boolean keyDown(Event e, int key)
    
    just as I did for mouse dragging below.


MySimpleApplet.html

<html> <head> <title>MySimpleApplet</title> </head> <body> <applet code=MySimpleApplet.class width=400 height=400> </applet> </body> </html>

MySimpleApplet.java

import java.awt.*; public class MySimpleApplet extends GenericApplet { int x = 100, y = 100; public void render(Graphics g) { if (damage) { g.setColor(Color.white); g.fillRect(0, 0, bounds().width, bounds().height); g.setColor(Color.black); g.drawLine(x - 20, y + 20, x + 20, y - 20); g.drawLine(x - 20, y - 20, x + 20, y + 20); } } public boolean mouseDown(Event e, int x, int y) { moveXY(x, y); return true; } public boolean mouseDrag(Event e, int x, int y) { moveXY(x, y); return true; } void moveXY(int x, int y) { this.x = x; this.y = y; damage = true; } } class GenericApplet extends java.applet.Applet implements Runnable { public boolean damage = true; // you can force a render public void render(Graphics g) { } // you can define how to render private Image image = null; private Graphics buffer = null; private Thread t; private Rectangle r = new Rectangle(0, 0, 0, 0); public void start() { if (t == null) { t = new Thread(this); t.start(); } } public void stop() { if (t != null) { t.stop(); t = null; } } public void run() { try { while (true) { repaint(); t.sleep(30); } } catch(InterruptedException e){}; } public void update(Graphics g) { if (r.width != bounds().width || r.height != bounds().height) { image = createImage(bounds().width, bounds().height); buffer = image.getGraphics(); r = bounds(); damage = true; } render(buffer); damage = false; if (image != null) g.drawImage(image,0,0,this); } }