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 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 InsanelySimpleApplet 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 InsanelySimpleApplet.java" in a command line window, and you can run it by loading file InsanelySimpleExample.html in your Web browser.
Your assignment for this week:
InsanelySimpleExample.html
<html> <head> <title>Insanely Simple Example of a Java Applet</title> </head> <body bgcolor=#ffe0e0> <center> <applet code=InsanelySimpleApplet.class width=400 height=400> </applet> </center> </body> </html>
InsanelySimpleApplet.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 InsanelySimpleApplet 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); } } } 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); } }