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, 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 that VerySimpleApplet does right now is display an 'X' shape. It extends class BufferedApplet, which just takes care of double buffering. Once you have installed the J2SE development environment, you can compile it by typing: "javac VerySimpleApplet.java" in a command line window, and then you can run it by loading file VerySimpleExample.html in your Web browser.
Your assignment for this week:
Post your working assignment up on your web folder.
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 VERY SIMPLE JAVA APPLET THAT JUST DRAWS AN X SHAPE. */ 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); } } } 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 SUPPORT CODE, AND YOU'LL PROBABLY NEVER NEED TO CHANGE IT. IT IS 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); } }