home *** CD-ROM | disk | FTP | other *** search
Java Source | 1996-09-20 | 11.6 KB | 444 lines |
- import java.applet.*;
- import java.awt.*;
- import java.util.*;
- import SetColorControls;
-
- public class EX16D 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;
- protected FontData fData = new FontData();
- protected SetFontDialog SetFontDlg;
-
- public void init()
- {
- // set up the set color dialog frame
- frame = new Frame();
- frame.resize(1, 1);
-
- // let the applet observe the color and the font
- cData.addObserver(this);
- fData.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.setFont(fData.GetFont());
-
- 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;
- }
- else if ("Set Font".equals(obj)) {
- SetFontDlg = new SetFontDialog(frame, fData);
- SetFontDlg.show();
-
- result = true;
- }
-
- return result;
- }
-
- public void update(Observable o, Object arg)
- {
- // since the paint method all ready looks at cData and fData,
- // simply force a repaint of the dialog to pick up the new
- // color or font
- 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, 12));
- setBackground(Color.lightGray);
-
- // 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();
- }
- }
-
- class FontData extends Observable
- {
- protected Font fnt = new Font("Dialog", Font.PLAIN, 12);
-
- public Font GetFont()
- {
- return fnt;
- }
-
- public void SetFont(Font fnt)
- {
- this.fnt = fnt; // save the font
-
- // flag the change an notify the on-lookers
- setChanged();
- notifyObservers();
- }
- }
-
- class SetFontDialog extends Dialog
- {
- protected Font selectedFont;
- protected FontData fData;
- protected Choice NameChoice = new Choice();
- protected Choice SizeChoice = new Choice();
- protected Checkbox BoldCheckbox = new Checkbox("Bold");
- protected Checkbox ItalicCheckbox = new Checkbox("Italic");
- protected Label SampleLabel = new Label("AaBbCc", Label.CENTER);
-
- public SetFontDialog(Frame parent, FontData fData)
- {
- super(parent, "Set Font", true);
-
- setFont(new Font("Dialog", Font.PLAIN, 12));
- setBackground(Color.lightGray);
-
- // don't let the user change the size
- setResizable(false);
-
- resize(300, 150);
-
- setBackground(Color.lightGray);
-
- // create the controls for the dialog
- Panel fontSelectionPanel = new Panel();
- fontSelectionPanel.add(NameChoice);
- fontSelectionPanel.add(SizeChoice);
- fontSelectionPanel.add(BoldCheckbox);
- fontSelectionPanel.add(ItalicCheckbox);
- add("North", fontSelectionPanel);
-
- Panel fontSamplePanel = new Panel();
- fontSamplePanel.add(new Label("Sample"));
- fontSamplePanel.add(SampleLabel);
- add("Center", fontSamplePanel);
-
- Panel dialogClosePanel = new Panel();
- dialogClosePanel.add(new Button("OK"));
- dialogClosePanel.add(new Button("Cancel"));
- add("South", dialogClosePanel);
-
- // fill in the choice lists
- FillChoiceLists();
-
- // save the data and start off with the current font
- this.fData = fData;
- NameChoice.select(fData.GetFont().getName());
- SizeChoice.select(Integer.toString(fData.GetFont().getSize()));
-
- BoldCheckbox.setState(
- (fData.GetFont().getStyle() & Font.BOLD) == Font.BOLD);
- ItalicCheckbox.setState(
- (fData.GetFont().getStyle() & Font.ITALIC) == Font.ITALIC);
-
- GetSelectedFont(false);
-
- SampleLabel.setFont(selectedFont);
- }
-
- public boolean action(Event evt, Object arg)
- {
- boolean result = false; // asume no action
-
- if ("OK".equals(evt.arg)) {
- fData.SetFont(selectedFont);
- 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 == NameChoice) ||
- (evt.target == SizeChoice) ||
- (evt.target == BoldCheckbox) ||
- (evt.target == ItalicCheckbox)) {
- GetSelectedFont(true);
- result = true;
- }
- }
-
- return result;
- }
-
-
- protected void FillChoiceLists()
- {
- String fonts[] = Toolkit.getDefaultToolkit().getFontList();
-
- for (int index = 0; index < fonts.length; index++)
- NameChoice.addItem(fonts[index]);
-
- // there is no way to get the font sizes that are available
- // on the host, so just add some to the listbox
- for (int index = 8; index <= 36; index += 4)
- SizeChoice.addItem(Integer.toString(index));
- }
-
- protected void GetSelectedFont(boolean redisplay)
- {
- int style = Font.PLAIN;
-
- if (BoldCheckbox.getState())
- style |= Font.BOLD;
-
- if (ItalicCheckbox.getState())
- style |= Font.ITALIC;
-
- selectedFont = new Font(NameChoice.getSelectedItem(), style,
- Integer.parseInt(SizeChoice.getSelectedItem()));
-
- if (redisplay) {
- // apply the new font to the sample and make sure
- // it fits in the dialog
- SampleLabel.setFont(selectedFont);
- SampleLabel.resize(SampleLabel.preferredSize());
-
- // tell the layout manager to do it's job again
- validate();
- }
- }
- }