This is a simple animation example that shows exception handling and page redirection.
/**
* Simple Animation Example
*
* @author Jing Dong
*/
import java.awt.*;
import java.net.*;
import java.applet.*;
public class SimpleAnimation extends Applet {
URL errorPage; //error html page
int currentImage; //current image counter
Image animation[]; //images
MediaTracker tracker;
public void init(){
currentImage = 0;
animation = new Image[8];
tracker = new MediaTracker(this);
//add images to MediaTracker
for (int i = 0; i < 8; i++ ){
animation[i] = getImage(getCodeBase(),
"images/free" + (i + 1) + ".gif");
tracker.addImage(animation[i], 0);
}
try {
showStatus("Now Loading...");
//wait for all images downloaded
tracker.waitForAll();
} catch(InterruptedException E) {
try {
errorPage = new URL(getCodeBase().toString()
+ "errorPage.html");
} catch(MalformedURLException ME) {
// page redirection
getAppletContext().showDocument(errorPage);
}
}
}
//override paint()
public void paint(Graphics g) {
//draw the current image
g.drawImage(animation[currentImage], 10, 10, 250, 250, this);
try {
//program stops for 0.125 second
Thread.sleep(125);
} catch(InterruptedException E) {
//create error html page
try {
errorPage = new URL(getCodeBase().toString()
+ "errorPage.html");
}catch(MalformedURLException ME) {
//page redirection to error page
getAppletContext().showDocument(errorPage);
}
}
if (currentImage == 7)
currentImage = 0;
else
currentImage = currentImage + 1;
repaint(); //patint it again!
}
}


0 Comments: