import java.applet.*; import java.awt.*; /* @Author: Ingo Festag @Date: January 97 @Version: 1.0 @Description : an animation controlled by the Media Tracker */ /* modified2 class */ public class modified2 extends Applet implements Runnable { Thread spinThread = null; Image img[] = new Image[53]; int counter = 1; int controller = 3; int differ = 0; int end = 0; Button b1, b2, b4, b5; MediaTracker tracker; /* class initialization */ public void init() { int i; b1 = new Button("back"); b2 = new Button("play/pause"); b4 = new Button("stop"); b5 = new Button("forward"); //Add Components to the Applet, using the default FlowLayout. add(b1); add(b2); add(b4); add(b5); setBackground(Color.white); //Necessary when adding buttons to an already visible container: validate(); tracker = new MediaTracker(this); for (i=0;i<53;i++) { img[i]=getImage(getCodeBase(),"cropcomp/cimag"+i+".gif"); tracker.addImage (img [i], 0); } } /* Next Spin image */ public void nextSpin(){ if (controller == 1) { // if "back" is pressed if (counter == 52) end = 0; differ = 0; // set play/pause to pause counter = counter - 1; if (counter == 0) { // control beginning of animation counter = 1; } controller = 3; // call pause function repaint(); } if (controller == 2) { // if "play/pause" is pressed if (differ == 1 ) // if play/pause is set to play if (end!=1) { // and end of animation is not reached counter = counter + 1; // count up if (counter == 52) { // if end of animation is reached controller = 3; // call pause function end = 1; // initalize end of animation } } else { counter = 1; end = 0; } if (differ == 0) { // if button in pause modus controller = 3; // set pause modus } } if (controller == 3) { //do nothing } if (controller == 4) { // if "stop" is pressed counter = 1; //reset counter controller = 3; //do nothing differ = 0; //set play/pause to pause } if (controller == 5) { // if "forward" is pressed differ = 0; counter = counter + 1; if (counter == 53) { counter = 52; differ = 1; end =1; } controller = 3; } } public void paint(Graphics g) { update(g); } /* draw the Spin image */ public void update(Graphics g) { Dimension d = size(); if (!tracker.checkAll()) { g.drawString("Loading images...please wait", d.width/2-85, d.height/2); } else{ g.drawImage(img[counter], 4, 50, this); g.setColor(Color.white); g.drawRect(150, 300, 250, 50); g.fillRect(150, 300, 250, 50); g.setColor(Color.black); g.drawString("week number " + counter +" of 1982", d.width/2-60, 310); } } /* start the thread */ public void start() { if (spinThread == null) { spinThread = new Thread(this); spinThread.start(); } } /* stop the thread */ public void stop() { if (spinThread!=null) { spinThread.stop(); spinThread = null; } } /* applet's run() method */ public void run() { try { //Start downloading the images. Wait until they're loaded before requesting repaints tracker.waitForAll(); } catch (InterruptedException e) {} while (spinThread != null) { repaint(); try { spinThread.sleep(500); } catch(InterruptedException e) { } nextSpin(); } } /* handle MOUSE_DOWN event */ public boolean action(Event e, Object arg) { Object target = e.target; if (target == b1) { //if button "back" has been pressed controller = 1; } if (target == b2) { //if button "play/pause" has been pressed controller = 2; if (end != 1) { differ = 1 - differ; } } if (target == b4) { //if button "stop" has been pressed controller = 4; } if (target == b5) { //if button "forward" has been pressed controller = 5; } return true; } }