Homework 1, due Tuesday, Sept 21.

When you have finished the assignment below, post the working applet onto the web. Post the source code too.

I've provided a very simple Applet which conforms to the Java 1.1 spec, below. If you don't have a Java development environment already, you can fetch the Java 2 Standard Edition (J2SE) from Sun at http://java.sun.com/downloads/. If you already had an earlier version installed, you can use that instead. Everything we'll do this semester will be compatible all the way back to Java 1.1, and therefore will run in all browsers and with any development environment you're likely to have.

All VerySimpleApplet does right now is display an 'X' shape. It extends class BufferedApplet, which just takes care of double buffering. You can compile it by typing: "javac VerySimpleApplet.java" in a command line window, and you can run it by loading file VerySimpleExample.html in your Web browser.

Your assignment for this week:

  1. Modify VerySimpleApplet to draw something exciting, interesting, aesthetically pleasing, silly, politically informative, existentially significant, deeply comic in an ironically profound way, or just plain cool. This assignment is largely for fun, and to give me a chance to get to find out a little about how you think, so the only real requirement is that you have fun with it, and experiment a bit (hint: it would be very bad if by amazing coincidence you and somebody else made exactly the same exciting/cool picture). 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.

    One thing to note is the construction

       if (damage) {
          ...
       }
    
    in the render method. I put that test in so that the "X" shape only gets drawn once (which is appropriate, since it never changes). If you remove this test, then the render method will get called repeatedly, and you will be able to make animations, which might be fun. :-)

  2. Post your result to the web. I suggest that you make a web folder for the assignments in this class.

  3. Email the grader, and send him the url of the web folder that you will be using for this class. The subject line of your email to the grader should be "GRAPHICS HW 1".



VerySimpleExample.html

<html> <head> <title>Very Simple Example of a Java Applet</title> </head> <body bgcolor=#ffe0e0> <center> <applet code=VerySimpleApplet.class width=400 height=400> </applet> </center> </body> </html>

VerySimpleApplet.java

/* THIS IS A REALLY STUPID APPLET THAT JUST DRAWS AN X. VERY BORING, REALLY. I'M SURE YOU CAN DO MUCH BETTER. :-) */ import java.awt.*; public class VerySimpleApplet extends BufferedApplet { int x = 100, y = 100; // COORDINATES OF THE CENTER OF THE X public void render(Graphics g) { // WHENEVER THERE IS "DAMAGE", WE NEED TO REDRAW if (damage) { // SET COLOR TO WHITE AND CLEAR THE APPLET WINDOW g.setColor(Color.white); g.fillRect(0, 0, bounds().width, bounds().height); // SET COLOR TO BLACK AND DRAW AN X SHAPE g.setColor(Color.black); g.drawLine(x - 20, y + 20, x + 20, y - 20); g.drawLine(x - 20, y - 20, x + 20, y + 20); } } }

BufferedApplet.java

import java.awt.*; public class BufferedApplet extends java.applet.Applet implements Runnable { /* THIS CLASS HANDLES DOUBLE BUFFERING FOR YOU, SO THAT THE IMAGE DOESN'T FLICKER WHILE YOU'RE RENDERING IT. YOU DON'T REALLY NEED TO WORRY ABOUT THIS TOO MUCH, AND YOU'LL PROBABLY NEVER NEED TO CHANGE IT. IT'S REALLY JUST USEFUL LOW LEVEL PLUMBING. */ public void render(Graphics g) { } // *you* define how to render public boolean damage = true; // you can force a render private Image image = null; private Graphics buffer = null; private Thread t; private Rectangle r = new Rectangle(0, 0, 0, 0); // A BACKGROUND THREAD CHECKS FOR CHANGES ABOUT 30 TIMES PER SECOND 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){}; } /* UPDATE GETS CALLED BY THE SYSTEM. IT CALLS YOUR RENDER METHOD, WHICH DRAWS INTO AN OFF-SCREEN IMAGE. THEN UPDATE COPIES THE IMAGE YOU'VE RENDERED ONTO THE APPLET WINDOW. */ 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); } }