home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Web Programming with Visual J++
/
Web_Programming_with_Visual_J_SAMS.NET_Version_1.0_1997.iso
/
source
/
chap16
/
ex16b.java
< prev
next >
Wrap
Text File
|
1996-09-16
|
8KB
|
279 lines
import java.applet.*;
import java.awt.*;
import java.util.*;
import SetColorControls;
public class EX16B extends Applet implements Observer
{
protected Button SetColorButton = new Button("Set Color");
protected Button SetFontButton = new Button("Set Font");
protected String MyText = new String("This is the text being modified.");
protected Frame frame;
protected ColorData cData = new ColorData();
protected SetColorDialog SetColorDlg;
public void init()
{
// set up the set color dialog frame
frame = new Frame();
frame.resize(1, 1);
// let the applet observe the color
cData.addObserver(this);
// add the controls to the applet
add(SetColorButton);
add(SetFontButton);
}
public void paint(Graphics g)
{
// use the color data to set the text color
g.setColor(cData.GetColor());
g.drawString(MyText, 20, 100);
}
public boolean action(Event evt, Object obj)
{
boolean result = false; // asume no action
if ("Set Color".equals(obj)) {
SetColorDlg = new SetColorDialog(frame, cData);
SetColorDlg.show();
result = true;
}
return result;
}
public void update(Observable o, Object arg)
{
// since the paint method all ready looks at cData, simply
// force a repaint of the dialog to pic up the new color
repaint();
}
}
class ColorData extends Observable
{
protected Color clr = new Color(Color.black.getRGB());
public Color GetColor()
{
return clr;
}
public void SetColor(Color clr)
{
this.clr = clr; // save the color
// flag the change an notify the on-lookers
setChanged();
notifyObservers();
}
}
class SetColorDialog extends Dialog
{
protected Color selectedColor;
protected ColorData cData;
protected SetColorControls ctrls;
protected Rectangle sampleRect;
public SetColorDialog(Frame parent, ColorData cData)
{
super(parent, "Set Color", true);
setFont(new Font("Dialog", Font.PLAIN, 16));
// don't let the user change the size
setResizable(false);
resize(220, 116);
// create the controls for the dialog
ctrls = new SetColorControls(this);
ctrls.CreateControls();
FillSetColorTo();
SetupScrollbars();
// save the data and start off with the current color
this.cData = cData;
selectedColor = new Color(cData.GetColor().getRGB());
}
public boolean action(Event evt, Object arg)
{
boolean result = false; // asume no action
if ("OK".equals(evt.arg)) {
cData.SetColor(selectedColor);
dispose(); // close the dialog
}
if ("Cancel".equals(evt.arg))
dispose(); // close the dialog
return result;
}
public boolean handleEvent(Event evt)
{
boolean result = false; // assume no action
// call the superclass for normal processing
result = super.handleEvent(evt);
// process the event here if not handled yet
if (!result) {
if (evt.target == ctrls.RedScrollbar) {
handleScrollbarEvent(ctrls.RedScrollbar, ctrls.RedValue);
result = true;
}
else if (evt.target == ctrls.GreenScrollbar) {
handleScrollbarEvent(ctrls.GreenScrollbar, ctrls.GreenValue);
result = true;
}
else if (evt.target == ctrls.BlueScrollbar) {
handleScrollbarEvent(ctrls.BlueScrollbar, ctrls.BlueValue);
result = true;
}
else if (evt.target == ctrls.StandardColorChoice) {
handleChoiceEvent();
result = true;
}
}
return result;
}
public void paint(Graphics g)
{
super.paint(g); // let normal processing happen
if (selectedColor != null) {
if (sampleRect == null) {
// determine where we can put the sample color
sampleRect = ctrls.StandardColorChoice.bounds();
sampleRect.x += sampleRect.width + 10;
sampleRect.width = 20;
sampleRect.height = 20;
}
g.setColor(selectedColor);
g.drawRect(sampleRect.x, sampleRect.y,
sampleRect.width, sampleRect.height);
g.fillRect(sampleRect.x, sampleRect.y,
sampleRect.width, sampleRect.height);
}
}
protected void FillSetColorTo()
{
ctrls.StandardColorChoice.addItem("black");
ctrls.StandardColorChoice.addItem("blue");
ctrls.StandardColorChoice.addItem("cyan");
ctrls.StandardColorChoice.addItem("darkGray");
ctrls.StandardColorChoice.addItem("gray");
ctrls.StandardColorChoice.addItem("green");
ctrls.StandardColorChoice.addItem("lightGray");
ctrls.StandardColorChoice.addItem("magenta");
ctrls.StandardColorChoice.addItem("orange");
ctrls.StandardColorChoice.addItem("pink");
ctrls.StandardColorChoice.addItem("red");
ctrls.StandardColorChoice.addItem("white");
ctrls.StandardColorChoice.addItem("yellow");
ctrls.StandardColorChoice.addItem("custom");
}
protected void SetupScrollbars()
{
ctrls.RedScrollbar.setValues(0, 10, 0, 255);
ctrls.GreenScrollbar.setValues(0, 10, 0, 255);
ctrls.BlueScrollbar.setValues(0, 10, 0, 255);
}
protected void handleScrollbarEvent(Scrollbar sBar, Label sBarValue)
{
int value = sBar.getValue();
// set the text value accroding to the scroll bar
sBarValue.setText(String.valueOf(value));
// reset the actual sample
SetSampleColor();
// changing of the scroll bar means it is custom
ctrls.StandardColorChoice.select("custom");
}
protected void handleChoiceEvent()
{
Color standardColor = null; // holds found color
String name = ctrls.StandardColorChoice.getSelectedItem();
// check for each of the standard colors
if (name == "black")
standardColor = new Color(Color.black.getRGB());
else if (name == "blue")
standardColor = new Color(Color.blue.getRGB());
else if (name == "cyan")
standardColor = new Color(Color.cyan.getRGB());
else if (name == "darkGray")
standardColor = new Color(Color.darkGray.getRGB());
else if (name == "gray")
standardColor = new Color(Color.gray.getRGB());
else if (name == "green")
standardColor = new Color(Color.green.getRGB());
else if (name == "lightGray")
standardColor = new Color(Color.lightGray.getRGB());
else if (name == "magenta")
standardColor = new Color(Color.magenta.getRGB());
else if (name == "orange")
standardColor = new Color(Color.orange.getRGB());
else if (name == "pink")
standardColor = new Color(Color.pink.getRGB());
else if (name == "red")
standardColor = new Color(Color.red.getRGB());
else if (name == "white")
standardColor = new Color(Color.white.getRGB());
else if (name == "yellow")
standardColor = new Color(Color.yellow.getRGB());
// set the scrollbar if the color is not custom
if (standardColor != null) {
SetScrollbarValue(ctrls.RedScrollbar,
ctrls.RedValue, standardColor.getRed());
SetScrollbarValue(ctrls.GreenScrollbar,
ctrls.GreenValue, standardColor.getGreen());
SetScrollbarValue(ctrls.BlueScrollbar,
ctrls.BlueValue, standardColor.getBlue());
// reset the actual sample
SetSampleColor();
}
}
protected void SetScrollbarValue(Scrollbar sBar,
Label sBarValue, int value)
{
sBar.setValue(value); // set the value
// set the text value accroding to the new value
sBarValue.setText(String.valueOf(value));
}
protected void SetSampleColor()
{
int red = ctrls.RedScrollbar.getValue();
int green = ctrls.GreenScrollbar.getValue();
int blue = ctrls.BlueScrollbar.getValue();
selectedColor = new Color(red, green, blue);
repaint();
}
}