// This example is from the book _Java in a Nutshell_ by David Flanagan.
// Written by David Flanagan.  Copyright (c) 1996 O'Reilly & Associates.
// You may study, use, modify, and distribute this example for any purpose.
// This example is provided WITHOUT WARRANTY either expressed or implied.

// Extended by Yoonsuck Choe <yschoe@cs.utexas.edu>
// Fri Apr  4 05:01:50 CST 1997
// updated : Mon Mar 30 13:16:53 CST 1998

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

public class Base extends Applet {
    private int origin_x = 0;
    private int origin_y = 0;
    private int last_x = 0;
    private int last_y = 0;
    private Color current_color = Color.black;
    private TextField range_text;		// color range input text
    private TextField spectrum_string_text;	// spectrum string input text
    private int range=128;			// color range
    private Colormap cmap = new Colormap("0ry1",range); // default colormap
    private String color_string="0ry1";		// spectrum string
    
    // Clearup window
    private void refresh() {
      Graphics g = this.getGraphics();
      Rectangle r = this.bounds();
      g.setColor(this.getBackground());
      g.clearRect(r.x, r.y, r.width, r.height);
      cmap.displaySpectrum(g,0,100,r.width,r.height-100);
    }

    // Called to initialize the applet.
    public void init() {
        // Set the background color
        this.setBackground(Color.white);

	// Create textfield for radius change
        this.add(new Label("Color Range: "));
	range_text = new TextField("",5);
        this.add(range_text);

	// Create textfield for display rate change 
        this.add(new Label("Color string: "));
	spectrum_string_text = new TextField("",5);
        this.add(spectrum_string_text);
    }
    
    // Called when the user clicks the mouse to start a scribble
    public boolean mouseDown(Event e, int x, int y)
    {
        last_x = x; last_y = y;
	origin_x = x; origin_y = y;
        return true;
    }

    // mouse button was released
    public boolean mouseUp(Event e, int x, int y)
    {
        Graphics g = this.getGraphics();
        last_x = x; last_y = y;
        return true;
    }

    // Called when the user scribbles with the mouse button down
    public boolean mouseDrag(Event e, int x, int y)
    {
        Graphics g = this.getGraphics();
        g.setColor(current_color);
        last_x = x;
        last_y = y;
        return true;
    }

    // process focus event
    public boolean gotFocus(Event event,Object what) 
    {
        Graphics g = this.getGraphics();
        Rectangle r = this.bounds();
        g.setColor(this.getBackground());
        g.clearRect(r.x, r.y, r.width, r.height);
	// som.display_map(g,r.width,r.height);
	// draw it twice to reduce flickering
	// som.display_map(g,r.width,r.height);
	cmap.displaySpectrum(g,0,100,r.width,r.height-100);
	return true;
    }

    // Called when the user clicks the button or chooses a color
    public boolean action(Event event, Object arg) {
        if (event.target == range_text) {
	    Integer new_range = new Integer(0);
	    range = new_range.parseInt(range_text.getText());
	    cmap = new Colormap(color_string,range);
	    this.refresh();
	    return true;
        } else if (event.target == spectrum_string_text) {
	    color_string = spectrum_string_text.getText();
	    cmap = new Colormap(color_string,range);
	    this.refresh();
	    return true;
	} 
        // Otherwise, let the superclass handle it.
        else {
		this.refresh();
		return super.action(event, arg);
	}
    }

}
