Homework 1, due Wednesday on week of Sept 9.

When you have finished the assignment below, post the working applet onto the web.

I've provided a very simple Applet which conforms to the Java 1.1 spec, below, for the benefit of those of you who've never written a Java applet before. If you don't have a Java development kit already, you can fetch one at http://java.sun.com/products/jdk/1.1/.

All MySimpleApplet does right now is to let you drag the mouse around to move a big 'X' shape. It extends class GenericApplet, which just implements double buffering. You can compile it by typing: "javac MySimpleApplet.java" in a command line window, and you can run it by loading file MySimpleApplet.html in your Web browser.

Your assignment for this week:

  1. Modify MySimpleApplet to draw something interesting, exciting, aesthetically pleasing, silly, profound, or just plain cool. This assignment is largely for fun, so have fun with it. Try out various features in java.awt.graphics, such as filling polygons (method fillPolygon), drawing text strings (method drawString), creating 3D-looking rectangles (method fill3DRect), and so on.

    If you're ambitious, try playing with the mouseDown and mouseDrag methods below to do something interactive. If you do, please note that these methods have the side effect of setting damage=true, so that the render method knows to redraw the picture. Your versions of mouseDown and mouseDrag should likewise set damage=true.

  2. Post your result to the web. You might want to make a web folder for the assignments in this class.

  3. Send me an email, at perlin@nyu.edu, telling me what the URL is. The subject of your email should be "GRADUATE GRAPHICS".


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


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); } }