FileDocCategorySizeDatePackage
Mover.javaAPI DocExample6308Thu Mar 25 21:30:24 GMT 2004None

Mover

public class Mover extends Applet implements Runnable
Mover -- move an image, slowly.
author
Ian Darwin, http://www.darwinsys.com/
version
$Id: Mover.java,v 1.11 2004/03/26 03:30:23 ian Exp $

Fields Summary
protected volatile boolean
done
The done or not done flag
protected Image
img
The current Image, or null
protected String
imageName
The NAME of the current Image, or null
protected int
imgWid
the size of the current image
protected int
imgHt
public static final int
DEFAULT_INTERVAL
DEFAULT msec between updates
protected int
interval
msec between updates
protected int
offset
Where we are
protected Thread
ticker
The 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.DimensiongetPreferredSize()
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 voidinit()
Setup a Mover applet.

		/** If set by non-default constructor, don't call Applet methods! */
		if (imageName == null)
			imageName = getParameter("imagename");
		if (imageName == null)
			imageName = "Mover.gif";
		System.out.println("imageName = " + imageName);
		setImage(imageName);
		System.out.println("setImage done");
	
public static voidmain(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);
		//f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		m.init();
		m.start();
		f.pack();
		f.setVisible(true);
	
public voidpaint(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 voidrun()
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 voidsetImage(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);
		img = getImage(getDocumentBase(), 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 voidstart()

		done = false;
		startThread();
	
protected voidstartThread()

		ticker = new Thread(this);
		ticker.setName("Ticker animation");
		ticker.setPriority(Thread.MAX_PRIORITY);
		ticker.start();
	
public voidstop()

		done = true;
		ticker = null;