Moverpublic class Mover extends Applet implements RunnableMover -- move an image, slowly. |
Fields Summary |
---|
protected volatile boolean | doneThe done or not done flag | protected Image | imgThe current Image, or null | protected String | imageNameThe NAME of the current Image, or null | protected int | imgWidthe size of the current image | protected int | imgHt | public static final int | DEFAULT_INTERVALDEFAULT msec between updates | protected int | intervalmsec between updates | protected int | offsetWhere we are | protected Thread | tickerThe Thread that keeps us ticking |
Constructors Summary |
---|
public Mover(String imgName)Construct a Mover, given an Image and using a default pause interval.
// THESE CONSTRUCTORS ARE ONLY NEEDED FOR TESTING AS A NON-APPLET
// YOU DON'T NEED THEM FOR AN APPLET-ONLY SOLUTION.
this(imgName, DEFAULT_INTERVAL);
| public Mover(String imgName, int pauseInt)Construct a Mover, given an Image and a pause interval.
interval = pauseInt;
imageName = imgName;
init();
| public Mover()Since we have the above Constructors, we need this one for Applet
|
Methods Summary |
---|
public java.awt.Dimension | getPreferredSize()Return how big we'd like to be. If image loaded, use its size.
If not, use an arbitrary default.
if (img == null || img.getWidth(this) < 0 || img.getHeight(this) < 0)
return new Dimension(100, 100);
return new Dimension(imgWid * 20, imgHt);
| public void | init()Setup a Mover applet.
if (imageName == null)
imageName = "mover.gif";
setImage(imageName);
| public static void | main(java.lang.String[] av)"main program" method - construct and show
final Frame f = new Frame("Mover Demo");
final Mover m = new Mover("mover.gif", 10);
f.add(m);
m.init();
m.start();
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
f.setVisible(false);
m.stop();
f.dispose();
System.exit(0);
}
});
f.pack();
f.setVisible(true);
| public void | paint(java.awt.Graphics g)Actually draw the Image onto the screen
if (img == null) {
g.setColor(Color.red);
g.fillRect(0, 0, getSize().width, getSize().height);
} else
g.drawImage(img, offset, 0, this);
| public void | run()Pick a new position for the image, and ask it to be painted
int w = getSize().width;
// System.out.println("Width = " + w);
while (!done) {
if (offset++ > w)
offset = 0;
try {
Thread.sleep(interval);
repaint();
} catch (InterruptedException canthappen) {
}
}
| public void | setImage(java.lang.String fn)Set the image to the given file
if (fn == null)
return;
imgWid = imgHt = 0;
offset = 0;
img = Toolkit.getDefaultToolkit().getImage(fn);
// Use a MediaTracker to show the "best"? way of waiting
// for an image to load, and how to check for errors.
MediaTracker mt = new MediaTracker(this);
mt.addImage(img, 0);
try {
mt.waitForID(0);
} catch(InterruptedException e) {
throw new IllegalArgumentException(
"Unexpected InterruptedException");
}
if (mt.isErrorID(0)) {
throw new IllegalArgumentException(
"Couldn't load image " + fn);
}
| public void | start()
done = false;
ticker = new Thread(this);
ticker.setName("Ticker animation");
ticker.setPriority(Thread.MAX_PRIORITY);
ticker.start();
| public void | stop()
done = true;
ticker = null;
|
|