import java.awt.*;


public class OptFrame extends Frame
{
    static final String colValues[] = { "00", "33", "66", "99", "CC", "FF" };
    static final String textSample = "The Quick Brown Dog Jumped Over\nthe Lazy Fox!";
    ImageSampler applet;
    TextArea text;
    Choice   size;
    Choice   redAmount;
    Choice   greenAmount;
    Choice   blueAmount;
    Label    colorSample;
    Button dismissButton;
    Button applyButton;

    Color  col = null;
    int    siz = 0;

    public OptFrame(ImageSampler sampler, Color bgColor) {
	super("Image Sampler Options");
	setBackground(bgColor);
	applet = sampler;

	Label textCaption = new Label("Sample text:");
	add("North", textCaption);
	text = new TextArea(5,40);
	text.setText(textSample);
	text.setBackground(Color.white);
	text.setForeground(Color.black);
	add("Center", text);

	Panel bpane = new Panel();
	bpane.setBackground(bgColor);
	bpane.setLayout(new BorderLayout());
	Panel tpane = new Panel();
	tpane.setBackground(bgColor);
	int cx;
	tpane.add(new Label("Size: "));
	size = new Choice();
	size.addItem("10"); size.addItem("12"); size.addItem("14");
	size.addItem("18"); size.addItem("24"); size.addItem("28");
	size.addItem("36");
	tpane.add(size);
	tpane.add(new Label("   Red:"));
	redAmount = new Choice();
	for(cx = 0; cx < colValues.length; cx++)
	    redAmount.addItem(colValues[cx]);
	tpane.add(redAmount);
	tpane.add(new Label("  Green:"));
	greenAmount = new Choice();
	for(cx = 0; cx < colValues.length; cx++)
	    greenAmount.addItem(colValues[cx]);
	tpane.add(greenAmount);
	tpane.add(new Label("  Blue:"));
	blueAmount = new Choice();
	for(cx = 0; cx < colValues.length; cx++)
	    blueAmount.addItem(colValues[cx]);
	tpane.add(blueAmount);
	bpane.add("North", tpane);

	Panel apane = new Panel();
	applyButton = new Button("Apply");
	apane.add(applyButton);
	dismissButton = new Button("Dismiss");
	apane.add(dismissButton);
	bpane.add("South", apane);
	add("South", bpane);
	collect();
	apply();
	pack();
    }


    public boolean action(Event evt, Object arg) {
	if (evt.target == dismissButton) {
	    hide();
	}
	else if (evt.target == applyButton) {
	    collect();
	    apply();
	}
	else if (evt.target instanceof Choice) {
	    boolean textIsBrite = collect();
	    text.setForeground(col);
	    text.setBackground((textIsBrite)?(Color.black):(Color.white));
	    String tmp = text.getText();
	    text.setText("");
	    text.setText(tmp);
	}

	return super.action(evt,arg);
    }

    void apply() {
	String txt = text.getText();
	applet.canvas.setTextOptions(txt, siz, col);
    }

    boolean collect() {
	siz = Integer.parseInt(size.getSelectedItem());
	int rc = Integer.parseInt(redAmount.getSelectedItem(),16);
	int gc = Integer.parseInt(greenAmount.getSelectedItem(),16);
	int bc = Integer.parseInt(blueAmount.getSelectedItem(),16);
	if (col == null ||
	    col.getRed() != rc || col.getGreen() != gc || col.getBlue() != bc)
	    {
		col = new Color(rc,gc,bc);
	    }
	return ((rc + gc + bc) > 290);
    }
	

}
