import java.awt.*;
import java.util.*;
import java.awt.image.ImageObserver;

public class ImagerCanvas extends Canvas
{
    public static final String FONTNAME = "TimesRoman";

    ImageSampler applet;
    Image image;
    boolean imageComplete;
    int   imageWidth;
    int   imageHeight;
    boolean doTile;
    String  textString;
    Color   textColor;
    Font    textFont;
    Color   bgColor;
    
    public ImagerCanvas(ImageSampler sampler, Color bg) 
    {
	applet = sampler;
	image  = null;
	imageComplete = false;
	imageWidth = 0;
	imageHeight = 0;
	doTile = true;
	textString = "";
	textColor = Color.black;
	textFont = null;
	bgColor = bg;
	setBackground(bgColor);
    }

    public void setTile(boolean t) {
	if (t != doTile) {
	    doTile = t;
	    repaint();
	}
    }

    public void setImage(Image img, boolean imgcomplete) {
	if (img != image) {
	    image = img;
	    if (image != null) {
		imageComplete = imgcomplete;
		if (imgcomplete) {
		    imageHeight = image.getHeight(this);
		    imageWidth  = image.getWidth(this);
		}
		else {
		    imageHeight = 0;
		    imageWidth = 0;
		}
	    }
	    else {
		imageComplete = false;
		imageHeight = 0;
		imageWidth = 0;
	    }
	    repaint();
	}
    }

    public void paint(Graphics g) {
	// handle image
	Dimension dim = size();

	if (image != null) {
	    if (!imageComplete) {
		g.drawImage(image, 0, 0, bgColor, this);
	    }
	    else {
		if (doTile) {
		    int xp;
		    int yp;
		    for(xp = 0; xp < dim.width; xp += imageWidth) {
			for(yp = 0; yp < dim.height; yp += imageHeight) {
			    g.drawImage(image, xp, yp, bgColor, this);
			}
		    }
		}
		else {
		    int xc = (dim.width - imageWidth) / 2;
		    int yc = (dim.height - imageHeight) / 2;
		    if (xc < 0) xc = 0;
		    if (yc < 0) yc = 0;
		    g.drawImage(image, xc, yc, bgColor, this);
		}
	    }
	}

	// handle text if image is complete
	if (imageComplete && textString != null && textString.length() > 0) {
	    int xp = 8;
	    int yp = 6 + textFont.getSize();
	    g.setFont(textFont);
	    StringTokenizer toke = new StringTokenizer(textString, "\n\r");
	    while(toke.hasMoreTokens()) {
		g.setColor(textColor);
		g.drawString(toke.nextToken(), xp, yp);
		yp += textFont.getSize() + textFont.getSize()/4 + 2;
	    }
	}

	// draw a border
	g.setColor(Color.black);
	g.drawRect(0,0, dim.width - 1, dim.height - 1);

	return;
    }

    public boolean imageUpdate(Image img, int infoflags,
			       int x, int y, int w, int h)
    {
	if ((infoflags & ImageObserver.ERROR) != 0) {
	    applet.message("Error loading " + applet.currentImageURL);
	    image = null;
	    imageComplete = false;
	    imageHeight = 0;
	    imageWidth = 0;
	    applet.ditchImage(img);
	    repaint();
	    return false;
	}
	if ((infoflags & ImageObserver.WIDTH) != 0) {
	    imageWidth = w;
	}
	if ((infoflags & ImageObserver.HEIGHT) != 0) {
	    imageHeight = h;
	}
	if ((infoflags & ImageObserver.SOMEBITS) != 0) {
	    applet.message("loaded " + y + "/" + imageHeight + " of " +
			   applet.currentImageName);
	}
	if ((infoflags & ImageObserver.ALLBITS) != 0) {
	    applet.message("Display: " + applet.currentImageName + 
			   ": " + applet.currentImageDesc +
			   " (" + w + "x" + h + ")");
	    imageComplete = true;
	    imageWidth = w; imageHeight = h;
	    repaint();
	    return false;
	}

	super.imageUpdate(img, infoflags, x, y, w, h);
	return true;
    }

    public void setTextOptions(String str, int size, Color col) {
	if (str != null)
	    textString = str;
	if (size > 0)
	    textFont = new Font(FONTNAME, Font.PLAIN, size);
	if (col != null)
	    textColor = col;
	repaint();
    }
}
