// DisplayJavaColours: browser for the pre-defined Java colour names
// Copyright (C) Lemma 1 Ltd. 1997
// Author: Rob Arthan; rda@lemma-one.com

// This program is free software; you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free Software
// Foundation; either version 2 of the License, or (at your option) any later
// version.

// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU General Public License for more details.
// The GNU General Public License can be obtained at URL:
// http://www.lemma-one.com/scrapbook/gpl.html
// or by writing to the Free Software Foundation, Inc., 59 Temple Place,
// Suite 330, Boston, MA 02111-1307 USA.
import java.awt.*;

class DisplayJavaColoursCanvas extends Canvas {
// size parameters
	private int width;
	private int height;
// display class
	private Color colour;
//
	public void paint(Graphics gfx) {
		Graphics g = getGraphics();
		setBackground(Color.lightGray);
		g.setColor(colour);
		g.clearRect(0, 0, width, height);
		g.fillRect(5, 5, width-12, height-12);
	}
//
// constructor function:
//
	DisplayJavaColoursCanvas (int w, int h, Color clr) {

		width = w;
		height = h;
		colour = clr;
	}
//
// preferredSize method for layout managers
//
	public Dimension preferredSize() {
		return new Dimension(width, height);
	}
//
// minimumSize method for layout managers
//
	public Dimension minimumSize() {
		return new Dimension(20, 20);
	}
}

class DisplayJavaColoursFrame extends Frame {
//
// the colour name
//
	String colour_name;
//
// method to display an RGB value (in hex) in the label
//
	private Label create_rgb_label(Color clr) {
		int red = clr.getRed();
		int green = clr.getGreen();
		int blue = clr.getBlue();
		String msg = 	": R = 0x" +
				Integer.toString(red, 16) +
				"; G = 0x" +
				Integer.toString(green, 16) +
				"; B = 0x" +
				Integer.toString(blue, 16);
		return new Label(msg, Label.CENTER);
	}
//
// build_frame: build a frame and populate it with a font display canvas
// and the label for the RGB value display
//
	DisplayJavaColoursFrame (int w, int h, String clr_name, Color clr) {
		super();
		colour_name = clr_name;
		String title = "Java Colour: " + colour_name;
		setTitle(title);

		GridBagLayout layout = new GridBagLayout();

		setLayout(layout);

		DisplayJavaColoursCanvas c =
			new DisplayJavaColoursCanvas(w, h, clr);

		GridBagConstraints c_con = new GridBagConstraints();
		c_con.gridx = 0; c_con.gridy = 0;
		layout.setConstraints(c, c_con);

		add(c);

		Label rgb_label = create_rgb_label(clr);

		GridBagConstraints rgbl_con = new GridBagConstraints();
		rgbl_con.gridx = 0; c_con.gridy = 1;
		layout.setConstraints(rgb_label, rgbl_con);

		add(rgb_label);

		pack();
		show();
	}

	public boolean handleEvent(Event evt) {
		if(evt.id == Event.WINDOW_DESTROY) {
			dispose();
			return true;
		}
		return false;
	}
}


class DisplayJavaColoursControlPanel extends Panel {

	private String[] colour_names = {
		"black",
		"blue",
		"cyan",
		"darkGray",
		"gray",
		"green",
		"lightGray",
		"magenta",
		"orange",
		"pink",
		"red",
		"white",
		"yellow"
	};

	private Color[] colours = {
		Color.black,
		Color.blue,
		Color.cyan,
		Color.darkGray,
		Color.gray,
		Color.green,
		Color.lightGray,
		Color.magenta,
		Color.orange,
		Color.pink,
		Color.red,
		Color.white,
		Color.yellow
	};

	private Choice colour_choice;

	private Button display_button;


	private GridBagConstraints
		 place(GridBagLayout gbl, Component comp,
			int x, int y, int width, int anchor) {
		
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.gridx = x;
		gbc.gridy = y;
		gbc.gridwidth = width;
		gbc.anchor = anchor;
		gbc.fill = GridBagConstraints.BOTH;
		gbl.setConstraints(comp, gbc);
		return gbc;
	}
		
	DisplayJavaColoursControlPanel() {

		GridBagLayout gbl = new GridBagLayout();
		setLayout(gbl);

		Label colour_choice_label = new Label("Colour:", Label.LEFT);
		add(colour_choice_label);

		colour_choice = new Choice();
		for(int i = 0; i < colour_names.length; ++i) {
			colour_choice.addItem(colour_names[i]);
		}
		add(colour_choice);

		display_button = new Button("Display");
		add(display_button);

		place(gbl, colour_choice_label,     0, 0, 1, GridBagConstraints.WEST);
		place(gbl, colour_choice,    1, 0, 1, GridBagConstraints.EAST);
		place(gbl, display_button, 0, 3, 2, GridBagConstraints.WEST);
		show();
	}

	public boolean handleEvent(Event evt) {
		if(evt.target == display_button
		&& evt.id == Event.ACTION_EVENT) {
			String clr_name = colour_choice.getSelectedItem();
			Color clr = colours[colour_choice.getSelectedIndex()];
			new DisplayJavaColoursFrame(200, 100, clr_name, clr);
			return true;
		}
		return false;
	}
}

public class DisplayJavaColours extends java.applet.Applet {
	public void init () {
		DisplayJavaColoursControlPanel pan = new DisplayJavaColoursControlPanel();
		add(pan);
		resize(pan.size());
	}
}
