/*
 * Copyright (c) 2000 David Flanagan.  All rights reserved.
 * This code is from the book Java Examples in a Nutshell, 2nd Edition.
 * It is provided AS-IS, WITHOUT ANY WARRANTY either expressed or implied.
 * You may study, use, and modify it for any non-commercial purpose.
 * You may distribute it non-commercially as long as you retain this notice.
 * For a commercial use license, or to purchase the book (recommended),
 * visit http://www.davidflanagan.com/javaexamples2.
 */
//package com.davidflanagan.examples.applet;
import java.applet.*;
import java.awt.*;

/**
 * This applet lets the user scribble with the mouse.
 * It demonstrates the Java 1.0 event model.
 **/
public class Scribble extends Applet{
  private int lastx, lasty;    // Remember last mouse coordinates.
  Button erase_button;         // The Erase button.
  private Button drawT;
  Choice color_choice;          // Color choice menu
  private Color selectedColor;
  private TextField text;
  private int draw;
  
    /** Initialize the erase button, ask for keyboard focus */
    public void init() {
	erase_button = new Button("Erase");
	erase_button.setBackground(Color.lightGray);
	color_choice = new Choice();
	color_choice.setBackground(Color.lightGray);
	 color_choice.add("Yellow");
	 color_choice.add("Red");
	 color_choice.add("Green");
	 color_choice.add("Magenta");
	 color_choice.add("Cyan");
	 color_choice.add("Black");
	 color_choice.add("White");

	text = new TextField();
	text.setBackground(Color.lightGray);
	text.setColumns(10);
	this.add(text);

	drawT = new Button("Draw Text");
	drawT.setBackground(Color.lightGray);
	this.add(drawT);

        this.add(color_choice);
	this.add(erase_button);
	this.setBackground(Color.blue);  // Set background color for scribble
	this.requestFocus();  // Ask for keyboard focus so we get key events
	selectedColor = new Color(0,0,0);
	selectedColor = Color.yellow;

	draw = 0;

    }

  public void paint(Graphics g){
       g.setColor(Color.white);
       g.drawRect(0,0,bounds().width-1,bounds().height-1);
       g.drawRect(1,1,bounds().width-3,bounds().height-3);
       g.drawRect(2,2,bounds().width-4,bounds().height-4);
       g.drawRect(3,3,bounds().width-5,bounds().height-5);
  }


    /** Respond to mouse clicks */
    public boolean mouseDown(Event e, int x, int y) {
	lastx = x; lasty = y;             // Remember where the click was
	if(draw == 1){
	  draw = 0;
	  Graphics g = getGraphics();
	  g.setColor(selectedColor);
	  g.drawString(text.getText(),lastx,lasty);  
	}
	return true;
    }

    /** Respond to mouse drags */
    public boolean mouseDrag(Event e, int x, int y) {
	Graphics g = getGraphics();
	g.setColor(selectedColor);
	g.drawLine(lastx, lasty, x, y);   // Draw from last position to here
	lastx = x; lasty = y;             // And remember new last position
	return true;
    }

    /** Respond to key presses: Erase drawing when user types 'e' */
    public boolean keyDown(Event e, int key) {
	if ((e.id == Event.KEY_PRESS) && (key == 'e') && (e.target != text)) {
	    Graphics g = getGraphics();
	    g.setColor(this.getBackground());   
	    g.fillRect(0, 0, bounds().width, bounds().height);
	    return true;
	}
	else return false;
    }

    /** Respond to Button clicks: erase drawing when user clicks button */
    public boolean action(Event e, Object arg) {
	if (e.target == erase_button) {
	    text.setText(" ");
	    Graphics g = getGraphics();
	    g.setColor(this.getBackground());   
	    g.fillRect(0, 0, bounds().width, bounds().height);
	    g.setColor(Color.white);
	    g.drawRect(0,0,bounds().width-2,bounds().height-2);
	    g.drawRect(1,1,bounds().width-3,bounds().height-3);
	    g.drawRect(2,2,bounds().width-4,bounds().height-4);
	    g.drawRect(3,3,bounds().width-5,bounds().height-5);
	    return true;
	}
	else if (e.target == color_choice){
	  Graphics g = getGraphics();
	    if(color_choice.getSelectedItem() == "Red")
	      selectedColor = Color.red;
	    else if (color_choice.getSelectedItem() == "Green")
	      selectedColor = Color.green;
	    else if (color_choice.getSelectedItem() == "Magenta")
	      selectedColor = Color.magenta;
	    else if (color_choice.getSelectedItem() == "Cyan")
	      selectedColor = Color.cyan;
	    else if (color_choice.getSelectedItem() == "Yellow")
	      selectedColor = Color.yellow;
	    else if (color_choice.getSelectedItem() == "Black")
	      selectedColor = Color.black;
	    else if (color_choice.getSelectedItem() == "White")
	      selectedColor = Color.white;
	    else
	      selectedColor = Color.lightGray;
	    return true;
	}
	else if (e.target == drawT){
	  draw = 1;
	  /* Graphics g = getGraphics();
	  g.setColor(selectedColor);
	  g.drawString(text.getText(),lastx,lasty); */
	  return true;
	}
		
	else return false;
    }
}
