Figure 5
A VCR animation applet in Java
/** A VCR in Java @author Mark C. Reynolds @version 1.0, 23 Feb, 1996 */ import java.applet.*; import java.awt.*; import java.net.*; public class Vcr extends Applet implements Runnable { MediaTracker tracker; Dimension nodim; Thread animthread = null; Image imgs[]; static final String FWD = "FWD"; static final String REV = "REV"; static final String STOP = "STOP"; static final String EJECT = "EJECT"; int whichimg = 0; int imginc = 0; // +1 = forward, -1 = reverse, 0 = stop int nimgs = 0; boolean done = false; public void init() { BorderLayout nl; FlowLayout fl; Button bu[]; Font fo; Panel so; Panel no; String tmp; String imgloc; imgloc = getParameter("imgloc"); if ( imgloc != null ) { tmp = getParameter("nimgs"); if ( tmp != null ) { nimgs = Integer.parseInt(tmp); if ( nimgs > 0 ) { imgs = new Image[nimgs]; tracker = new MediaTracker(this); for(int i = 0, j = 1; i < nimgs; i++, j++) { imgs[i] = getImage(getDocumentBase(), imgloc + j + ".gif"); tracker.addImage(imgs[i], 0); } nl = new BorderLayout(); setLayout(nl); no = new Panel(); nodim = preferredSize(); no.resize(nodim.width, nodim.height-50); add("North", no); so = new Panel(); fl = new FlowLayout(FlowLayout.LEFT); so.setLayout(fl); add("South", so); bu = new Button[4]; bu[0] = new Button(Vcr.FWD); bu[1] = new Button(Vcr.REV); bu[2] = new Button(Vcr.STOP); bu[3] = new Button(Vcr.EJECT); fo = new Font("Helvetica", Font.PLAIN, 14); for(int i = 0; i < 4; i++) bu[i].setFont(fo); so.add(bu[0]); so.add(bu[1]); so.add(bu[2]); so.add(bu[3]); } } } } public void run() { Thread me; if ( nimgs < 1 || imgs == null ) return; try { tracker.waitForID(0); } catch (InterruptedException e) { return; } me = Thread.currentThread(); me.setPriority(Thread.NORM_PRIORITY-1); while ( animthread != null ) { whichimg += imginc; if ( whichimg < 0 ) { whichimg = 0; imginc = 0; } if ( whichimg >= nimgs ) { whichimg = (nimgs - 1); imginc = 0; } if ( imginc != 0 ) repaint(); try { Thread.sleep(100); } catch (InterruptedException e) { break; } } } public void paint(Graphics g) { if ( done == false ) { if ( ( imgs != null ) && ( 0 <= whichimg ) && ( whichimg < nimgs ) && ( imgs[whichimg] != null ) ) { g.drawImage(imgs[whichimg], 0, 0, this); } } else { g.clearRect(0, 0, nodim.width, nodim.height); } } public void start() { if ( animthread == null ) { animthread = new Thread(this); animthread.start(); } } public void stop() { if ( animthread != null ) { animthread.stop(); animthread = null; repaint(); } } public boolean action(Event evt, Object arg) { Button thebut; String thelab; boolean handled = false; if ( evt.target instanceof Button ) { thebut = (Button)(evt.target); thelab = thebut.getLabel(); if ( thelab.equals(Vcr.FWD) ) { imginc = 1; handled = true; } else if ( thelab.equals(Vcr.REV) ) { imginc = (-1); handled = true; } else if ( thelab.equals(Vcr.STOP) ) { imginc = 0; handled = true; } else if ( thelab.equals(Vcr.EJECT) ) { imginc = 0; done = true; stop(); handled = true; } } return(handled); } } /** A VCR in Java @author Mark C. Reynolds @version 1.0, 23 Feb, 1996 */ import java.applet.*; import java.awt.*; import java.net.*; public class Vcr extends Applet implements Runnable { MediaTracker tracker; Dimension nodim; Thread animthread = null; Image imgs[]; static final String FWD = "FWD"; static final String REV = "REV"; static final String STOP = "STOP"; static final String EJECT = "EJECT"; int whichimg = 0; int imginc = 0; // +1 = forward, -1 = reverse, 0 = stop int nimgs = 0; boolean done = false; public void init() { BorderLayout nl; FlowLayout fl; Button bu[]; Font fo; Panel so; Panel no; String tmp; String imgloc; imgloc = getParameter("imgloc"); if ( imgloc != null ) { tmp = getParameter("nimgs"); if ( tmp != null ) { nimgs = Integer.parseInt(tmp); if ( nimgs > 0 ) { imgs = new Image[nimgs]; tracker = new MediaTracker(this); for(int i = 0, j = 1; i < nimgs; i++, j++) { imgs[i] = getImage(getDocumentBase(), imgloc + j + ".gif"); tracker.addImage(imgs[i], 0); } nl = new BorderLayout(); setLayout(nl); no = new Panel(); nodim = preferredSize(); no.resize(nodim.width, nodim.height-50); add("North", no); so = new Panel(); fl = new FlowLayout(FlowLayout.LEFT); so.setLayout(fl); add("South", so); bu = new Button[4]; bu[0] = new Button(Vcr.FWD); bu[1] = new Button(Vcr.REV); bu[2] = new Button(Vcr.STOP); bu[3] = new Button(Vcr.EJECT); fo = new Font("Helvetica", Font.PLAIN, 14); for(int i = 0; i < 4; i++) bu[i].setFont(fo); so.add(bu[0]); so.add(bu[1]); so.add(bu[2]); so.add(bu[3]); } } } } public void run() { Thread me; if ( nimgs < 1 || imgs == null ) return; try { tracker.waitForID(0); } catch (InterruptedException e) { return; } me = Thread.currentThread(); me.setPriority(Thread.NORM_PRIORITY-1); while ( animthread != null ) { whichimg += imginc; if ( whichimg < 0 ) { whichimg = 0; imginc = 0; } if ( whichimg >= nimgs ) { whichimg = (nimgs - 1); imginc = 0; } if ( imginc != 0 ) repaint(); try { Thread.sleep(100); } catch (InterruptedException e) { break; } } } public void paint(Graphics g) { if ( done == false ) { if ( ( imgs != null ) && ( 0 <= whichimg ) && ( whichimg < nimgs ) && ( imgs[whichimg] != null ) ) { g.drawImage(imgs[whichimg], 0, 0, this); } } else { g.clearRect(0, 0, nodim.width, nodim.height); } } public void start() { if ( animthread == null ) { animthread = new Thread(this); animthread.start(); } } public void stop() { if ( animthread != null ) { animthread.stop(); animthread = null; repaint(); } } public boolean action(Event evt, Object arg) { Button thebut; String thelab; boolean handled = false; if ( evt.target instanceof Button ) { thebut = (Button)(evt.target); thelab = thebut.getLabel(); if ( thelab.equals(Vcr.FWD) ) { imginc = 1; handled = true; } else if ( thelab.equals(Vcr.REV) ) { imginc = (-1); handled = true; } else if ( thelab.equals(Vcr.STOP) ) { imginc = 0; handled = true; } else if ( thelab.equals(Vcr.EJECT) ) { imginc = 0; done = true; stop(); handled = true; } } return(handled); } }