FileDocCategorySizeDatePackage
PixCanvas.javaAPI DocExample2152Sun Feb 08 21:33:50 GMT 2004None

PixCanvas

public class PixCanvas extends Canvas
PixCanvas - a helper class for PhotoUI, or any other program that needs to store and display Images. Originally written as part of the "Photo manipulation GUI" for JabaDex
author
Ian Darwin, http://www.darwinsys.com/
version
$DarwinSys 1.1$

Fields Summary
Image
im
int
wid
int
ht
Constructors Summary
PixCanvas(int w, int h)
Construct a PixCanvas, given a width and height

		setSize(wid=w, ht=h);
	
PixCanvas(String fn)
Construct a PixCanvas, given an Image name

		setImage(fn);
		setSize(im.getWidth(this), im.getHeight(this));
	
Methods Summary
public java.awt.DimensiongetPreferredSize()
Return how big we'd like to be. If image loaded, use its size. If not, use 200, 100 (why not?).

		if (im == null || im.getWidth(this) < 0 || im.getHeight(this) < 0)
			return new Dimension(200, 100);
		return new Dimension(im.getWidth(this), im.getHeight(this));
	
public voidpaint(java.awt.Graphics g)
Actually draw the Image onto the screen

		if (im == null) {
			g.setColor(Color.red);
			g.fillRect(0, 0, wid, ht);
		} else
			g.drawImage(im, 0, 0, this);
	
public voidsetImage(java.lang.String fn)
Set the image to a given filename

		if (fn == null)
			return;
		// Only the Application version of getImage shown here
		Image i = Toolkit.getDefaultToolkit().getImage(fn);
		setImage(i);
	
public voidsetImage(java.awt.Image i)
Set the image to a given Image object

		if (i == null)
			return;
		im = i;

		// ----- This part omitted from course notes for brevity -----
		// 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(im, 0);
		try {
			mt.waitForID(0);
		} catch(InterruptedException e) {
			throw new IllegalArgumentException(
				"Unexpected InterruptedException");
		}
		if (mt.isErrorID(0)) {
			throw new IllegalArgumentException(
				"Couldn't load image");
		}
		// Now that we know the image has been loaded,
		// it is safe to paint it onto the screen.
		// ----- End of part omitted from course notes for brevity -----
		repaint();