
// Written by Susan Addington .  Copyright (c) 1997.
// You may study, use, modify, and distribute this example for any noncommerical
// purpose. This example is provided WITHOUT WARRANTY either expressed or implied.

// As you drag the mouse, the applet draws a path in the chosen color on a background image.
// Clear erases the path, but not the image.

// Needs more work:
// 1. Error and exception handling: What if there is no image? etc.

import java.applet.*;
import java.awt.*;
import java.lang.*;

public class ImageScribble extends Scribble 
{
	public Button clear_button;
	public Choice color_choice;
	protected Image map;
	int w,h;
	double scfac;
	public Color line_color;
	gjtUtil grUtil = new gjtUtil();

	public void init() 
	{
		super.init();
		
		// get the background image and calculate the scaled dimensions

		map = this.getImage(this.getDocumentBase(), this.getParameter("image"));
		grUtil.waitForImage(this,map);
		w = map.getWidth(this);
		h = map.getHeight(this);
		scfac = this.getScale("scale_factor");
		Integer s = new Integer(w);
		w = (int)Math.round(s.doubleValue() * scfac);
		Integer t = new Integer(h);
		h  = (int)Math.round(t.doubleValue() * scfac);
	
		clear_button = new Button("Clear");
		clear_button.setForeground(Color.black);
		this.add(clear_button);
		
		color_choice = new Choice();
		color_choice.setForeground(Color.black);
		color_choice.addItem("black");
		color_choice.addItem("white");
		color_choice.addItem("red");
		color_choice.addItem("orange");
		color_choice.addItem("yellow");
		color_choice.addItem("green");
		color_choice.addItem("blue");
		this.add(color_choice);
	}
	
	public void destroy() 
	{
		map.flush();
	}
	
	public void paint(Graphics g) 
	{
		g.drawImage(map,0,40,w,h,this);
	}
	
	public void update(Graphics g) 
	{
		paint(g);
	}
	
	// For getting the scale factor parameter. 
	protected double getScale(String name)
	{
		String value = this.getParameter(name);
		double doubvalue;
		try 
		{
			doubvalue = (Double.valueOf(value)).doubleValue();
		}
		catch (NumberFormatException e) 
		{
			doubvalue = 1;
		}
		return doubvalue;
		
	}
	
	public boolean action(Event event, Object arg) 
	{
		if (event.target == clear_button) 
		{
			Graphics g = this.getGraphics();
			update(g);
			return true;
		}
		else if (event.target == color_choice) 
		{
			switch (color_choice.getSelectedIndex()) 
			{
				case 0:
					line_color = Color.black; break;
				case 1:
					line_color = Color.white; break;
				case 2:
					line_color = Color.red; break;
				case 3:
					line_color = Color.orange; break;
				case 4:
					line_color = Color.yellow; break;
				case 5:
					line_color = Color.green; break;
				case 6:
					line_color = Color.blue; break;
				default:
					line_color = Color.black; break;
			}
			
			return true;
		}
		else return super.action(event, arg);
	}
	
	public boolean mouseDrag(Event e, int x, int y)
	{
		this.setForeground(line_color);
		// Use mouseDrag from Scribble to draw line
		super.mouseDrag(e, x, y);
		
		return true;
	}
	
}
