//AlphaTime.java //Demonstrates synchronization //Andy Harris, 06/00 import java.awt.*; import java.applet.*; import java.awt.event.*; import java.util.*; import java.text.*; public class AlphaTime extends Applet implements Runnable, ActionListener{ Image imgA; Image imgB; Image imgC; Image imgD; Image imgE; Image imgF; AudioClip sndPoem; Canvas drawSurface = new Canvas(); Button btnPlay = new Button("Start"); Label lblTime = new Label("00:00"); Thread looper; SimpleDateFormat formatter = new SimpleDateFormat("mm:ss"); Date startTime = new Date(); Date currentTime = new Date(); Date elapsedTime = new Date(); boolean keepPlaying = false; public void init(){ System.out.println(String.valueOf(getDocumentBase())); imgA = this.getImage(this.getDocumentBase(), "A.jpg"); imgB = this.getImage(this.getDocumentBase(), "B.jpg"); imgC = this.getImage(this.getDocumentBase(), "C.jpg"); imgD = this.getImage(this.getDocumentBase(), "D.jpg"); imgE = this.getImage(this.getDocumentBase(), "E.jpg"); imgF = this.getImage(this.getDocumentBase(), "F.jpg"); sndPoem = this.getAudioClip(getDocumentBase(), "Poem.au"); //set up mediatracker MediaTracker tracker = new MediaTracker(this); tracker.addImage(imgA, 0); tracker.addImage(imgB, 1); tracker.addImage(imgC, 2); tracker.addImage(imgD, 3); tracker.addImage(imgE, 4); tracker.addImage(imgF, 5); //wait until all images are loaded try{ tracker.waitForAll(); } catch (InterruptedException exc){ System.out.println(exc.getMessage()); } // end try //Set up applet setLayout (new BorderLayout()); add (lblTime, BorderLayout.NORTH); add (drawSurface, BorderLayout.CENTER); add (btnPlay, BorderLayout.SOUTH); lblTime.setAlignment(Label.CENTER); btnPlay.addActionListener(this); } // end init public void actionPerformed(ActionEvent e){ keepPlaying = true; sndPoem.play(); startTime = new Date(); //start the thread if (looper == null){ looper = new Thread(this); looper.start(); } // end if } // end actionPerformed public void run(){ Graphics g = drawSurface.getGraphics(); Dimension size = drawSurface.getSize(); int ht = size.height; int wd = size.width; String elTime = ""; while (keepPlaying == true){ try { looper.sleep(500); } catch(InterruptedException exp){ System.out.println(exp.getMessage()); } // end try currentTime = new Date(); long elTimeRaw = currentTime.getTime() - startTime.getTime(); elapsedTime = new Date(elTimeRaw); elTime = formatter.format(elapsedTime); lblTime.setText(elTime); //Display the appropriate image if (elTime.equals("00:00")){ g.drawImage(imgA, 0, 0, wd, ht, this); } else if (elTime.equals("00:04")){ g.drawImage(imgB, 0, 0, wd, ht, this); } else if (elTime.equals("00:08")){ g.drawImage(imgC, 0, 0, wd, ht, this); } else if (elTime.equals("00:12")){ g.drawImage(imgD, 0, 0, wd, ht, this); } else if (elTime.equals("00:16")){ g.drawImage(imgE, 0, 0, wd, ht, this); } else if (elTime.equals("00:20")){ g.drawImage(imgF, 0, 0, wd, ht, this); } else if (elTime.equals("00:25")){ keepPlaying = false; } // end if } // end while } // end run public void stop(){ //kill the thread keepPlaying = false; } // end stop } // end class def