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 / ex16d.java < prev    next >
Encoding:
Java Source  |  1996-09-20  |  11.6 KB  |  444 lines

  1. import java.applet.*;
  2. import java.awt.*;
  3. import java.util.*;
  4. import SetColorControls;
  5.  
  6. public class EX16D extends Applet implements Observer
  7. {
  8.     protected Button SetColorButton = new Button("Set Color");
  9.     protected Button SetFontButton = new Button("Set Font");
  10.     protected String MyText = new String("This is the text being modified.");
  11.     protected Frame frame;
  12.     protected ColorData cData = new ColorData();
  13.     protected SetColorDialog SetColorDlg;
  14.     protected FontData fData = new FontData();
  15.     protected SetFontDialog SetFontDlg;
  16.  
  17.     public void init()
  18.     {
  19.          // set up the set color dialog frame
  20.         frame = new Frame();
  21.         frame.resize(1, 1);
  22.  
  23.         // let the applet observe the color and the font
  24.         cData.addObserver(this);
  25.         fData.addObserver(this);
  26.  
  27.         // add the controls to the applet
  28.         add(SetColorButton);
  29.         add(SetFontButton);
  30.     }
  31.  
  32.     public void paint(Graphics g)
  33.     {
  34.         // use the color data to set the text color
  35.         g.setColor(cData.GetColor());
  36.         g.setFont(fData.GetFont());
  37.  
  38.         g.drawString(MyText, 20, 100);
  39.     }
  40.  
  41.     public boolean action(Event evt, Object obj) 
  42.     {
  43.         boolean result = false;            // asume no action
  44.  
  45.         if ("Set Color".equals(obj)) {
  46.             SetColorDlg = new SetColorDialog(frame, cData);
  47.             SetColorDlg.show();
  48.  
  49.             result = true;
  50.         }
  51.         else if ("Set Font".equals(obj)) {
  52.             SetFontDlg = new SetFontDialog(frame, fData);
  53.             SetFontDlg.show();
  54.  
  55.             result = true;
  56.         }
  57.  
  58.         return result;
  59.     }
  60.  
  61.     public void update(Observable o, Object arg)
  62.     {
  63.         // since the paint method all ready looks at cData and fData,
  64.         //   simply  force a repaint of the dialog to pick up the new 
  65.         //   color or font
  66.         repaint();
  67.     }
  68. }
  69.  
  70. class ColorData extends Observable
  71. {
  72.     protected Color clr = new Color(Color.black.getRGB());
  73.  
  74.     public Color GetColor()
  75.     {
  76.         return clr;
  77.     }
  78.  
  79.     public void SetColor(Color clr)
  80.     {
  81.         this.clr = clr;                    // save the color
  82.  
  83.         // flag the change an notify the on-lookers
  84.         setChanged();
  85.         notifyObservers();
  86.     }
  87. }
  88.  
  89. class SetColorDialog extends Dialog
  90. {
  91.     protected Color selectedColor;
  92.     protected ColorData cData;
  93.     protected SetColorControls ctrls;
  94.     protected Rectangle sampleRect;
  95.  
  96.     public SetColorDialog(Frame parent, ColorData cData)
  97.     {
  98.         super(parent, "Set Color", true);
  99.  
  100.         setFont(new Font("Dialog", Font.PLAIN, 12));
  101.         setBackground(Color.lightGray);
  102.  
  103.         // don't let the user change the size
  104.         setResizable(false);
  105.  
  106.         resize(220, 116);
  107.  
  108.         // create the controls for the dialog
  109.         ctrls = new SetColorControls(this);
  110.         ctrls.CreateControls();
  111.  
  112.         FillSetColorTo();
  113.         SetupScrollbars();
  114.  
  115.         // save the data and start off with the current color
  116.         this.cData = cData;
  117.         selectedColor = new Color(cData.GetColor().getRGB());
  118.     }
  119.  
  120.     public boolean action(Event evt, Object arg) 
  121.     {
  122.         boolean result = false;            // asume no action
  123.  
  124.         if ("OK".equals(evt.arg)) {
  125.             cData.SetColor(selectedColor);
  126.             dispose();                    // close the dialog
  127.         }
  128.         if ("Cancel".equals(evt.arg))
  129.             dispose();                    // close the dialog
  130.  
  131.         return result;
  132.     }
  133.  
  134.     public boolean handleEvent(Event evt)
  135.     {
  136.         boolean result = false;            // assume no action
  137.  
  138.         // call the superclass for normal processing
  139.         result = super.handleEvent(evt);
  140.  
  141.         // process the event here if not handled yet
  142.         if (!result) {
  143.             if (evt.target == ctrls.RedScrollbar) {
  144.                 handleScrollbarEvent(ctrls.RedScrollbar, ctrls.RedValue);
  145.                 result = true;
  146.             }
  147.             else if (evt.target == ctrls.GreenScrollbar) {
  148.                 handleScrollbarEvent(ctrls.GreenScrollbar, ctrls.GreenValue);
  149.                 result = true;
  150.             }
  151.             else if (evt.target == ctrls.BlueScrollbar) {
  152.                 handleScrollbarEvent(ctrls.BlueScrollbar, ctrls.BlueValue);
  153.                 result = true;
  154.             }
  155.             else if (evt.target == ctrls.StandardColorChoice) {
  156.                 handleChoiceEvent();
  157.                 result = true;
  158.             }
  159.         }
  160.  
  161.         return result;
  162.     }
  163.  
  164.     public void paint(Graphics g)
  165.     {
  166.         super.paint(g);                    // let normal processing happen
  167.  
  168.         if (selectedColor != null) {
  169.             if (sampleRect == null) {
  170.                 // determine where we can put the sample color
  171.                 sampleRect = ctrls.StandardColorChoice.bounds();
  172.                 sampleRect.x += sampleRect.width + 10;
  173.                 sampleRect.width = 20;
  174.                 sampleRect.height = 20;
  175.             }
  176.  
  177.             g.setColor(selectedColor);
  178.  
  179.             g.drawRect(sampleRect.x, sampleRect.y, 
  180.                         sampleRect.width, sampleRect.height);
  181.             g.fillRect(sampleRect.x, sampleRect.y, 
  182.                 sampleRect.width, sampleRect.height);
  183.         }
  184.     }
  185.  
  186.     protected void FillSetColorTo() 
  187.     {
  188.         ctrls.StandardColorChoice.addItem("black");
  189.         ctrls.StandardColorChoice.addItem("blue");
  190.         ctrls.StandardColorChoice.addItem("cyan");
  191.         ctrls.StandardColorChoice.addItem("darkGray");
  192.         ctrls.StandardColorChoice.addItem("gray");
  193.         ctrls.StandardColorChoice.addItem("green");
  194.         ctrls.StandardColorChoice.addItem("lightGray");
  195.         ctrls.StandardColorChoice.addItem("magenta");
  196.         ctrls.StandardColorChoice.addItem("orange");
  197.         ctrls.StandardColorChoice.addItem("pink");
  198.         ctrls.StandardColorChoice.addItem("red");
  199.         ctrls.StandardColorChoice.addItem("white");
  200.         ctrls.StandardColorChoice.addItem("yellow");
  201.         ctrls.StandardColorChoice.addItem("custom");
  202.     }
  203.  
  204.     protected void SetupScrollbars() 
  205.     {
  206.         ctrls.RedScrollbar.setValues(0, 10, 0, 255);
  207.         ctrls.GreenScrollbar.setValues(0, 10, 0, 255);
  208.         ctrls.BlueScrollbar.setValues(0, 10, 0, 255);
  209.     }
  210.  
  211.     protected void handleScrollbarEvent(Scrollbar sBar, Label sBarValue)
  212.     {
  213.         int value = sBar.getValue();
  214.  
  215.         // set the text value accroding to the scroll bar
  216.         sBarValue.setText(String.valueOf(value));
  217.  
  218.         // reset the actual sample
  219.         SetSampleColor();
  220.  
  221.         // changing of the scroll bar means it is custom
  222.         ctrls.StandardColorChoice.select("custom");
  223.     }
  224.  
  225.     protected void handleChoiceEvent()
  226.     {
  227.         Color standardColor = null;        // holds found color
  228.         String name = ctrls.StandardColorChoice.getSelectedItem();
  229.  
  230.         // check for each of the standard colors
  231.         if (name == "black")
  232.             standardColor = new Color(Color.black.getRGB());
  233.         else if (name == "blue")
  234.             standardColor = new Color(Color.blue.getRGB());
  235.         else if (name == "cyan")
  236.             standardColor = new Color(Color.cyan.getRGB());
  237.         else if (name == "darkGray")
  238.             standardColor = new Color(Color.darkGray.getRGB());
  239.         else if (name == "gray")
  240.             standardColor = new Color(Color.gray.getRGB());
  241.         else if (name == "green")
  242.             standardColor = new Color(Color.green.getRGB());
  243.         else if (name == "lightGray")
  244.             standardColor = new Color(Color.lightGray.getRGB());
  245.         else if (name == "magenta")
  246.             standardColor = new Color(Color.magenta.getRGB());
  247.         else if (name == "orange")
  248.             standardColor = new Color(Color.orange.getRGB());
  249.         else if (name == "pink")
  250.             standardColor = new Color(Color.pink.getRGB());
  251.         else if (name == "red")
  252.             standardColor = new Color(Color.red.getRGB());
  253.         else if (name == "white")
  254.             standardColor = new Color(Color.white.getRGB());
  255.         else if (name == "yellow")
  256.             standardColor = new Color(Color.yellow.getRGB());
  257.  
  258.         // set the scrollbar if the color is not custom
  259.         if (standardColor != null) {
  260.             SetScrollbarValue(ctrls.RedScrollbar, 
  261.                     ctrls.RedValue, standardColor.getRed());
  262.             SetScrollbarValue(ctrls.GreenScrollbar, 
  263.                     ctrls.GreenValue, standardColor.getGreen());
  264.             SetScrollbarValue(ctrls.BlueScrollbar, 
  265.                     ctrls.BlueValue, standardColor.getBlue());
  266.  
  267.             // reset the actual sample
  268.             SetSampleColor();
  269.         }
  270.     }
  271.  
  272.     protected void SetScrollbarValue(Scrollbar sBar, 
  273.             Label sBarValue, int value)
  274.     {
  275.         sBar.setValue(value);            // set the value
  276.  
  277.         // set the text value accroding to the new value
  278.         sBarValue.setText(String.valueOf(value));
  279.     }
  280.  
  281.  
  282.     protected void SetSampleColor()
  283.     {
  284.         int red = ctrls.RedScrollbar.getValue();
  285.         int green = ctrls.GreenScrollbar.getValue();
  286.         int blue = ctrls.BlueScrollbar.getValue();
  287.  
  288.         selectedColor = new Color(red, green, blue);
  289.         repaint();
  290.     }
  291. }
  292.  
  293. class FontData extends Observable
  294. {
  295.     protected Font fnt = new Font("Dialog", Font.PLAIN, 12);
  296.  
  297.     public Font GetFont()
  298.     {
  299.         return fnt;
  300.     }
  301.  
  302.     public void SetFont(Font fnt)
  303.     {
  304.         this.fnt = fnt;                    // save the font
  305.  
  306.         // flag the change an notify the on-lookers
  307.         setChanged();
  308.         notifyObservers();
  309.     }
  310. }
  311.  
  312. class SetFontDialog extends Dialog
  313. {
  314.     protected Font selectedFont;
  315.     protected FontData fData;
  316.     protected Choice NameChoice = new Choice();
  317.     protected Choice SizeChoice = new Choice();
  318.     protected Checkbox BoldCheckbox = new Checkbox("Bold");
  319.     protected Checkbox ItalicCheckbox = new Checkbox("Italic");
  320.     protected Label SampleLabel = new Label("AaBbCc", Label.CENTER);
  321.  
  322.     public SetFontDialog(Frame parent, FontData fData)
  323.     {
  324.         super(parent, "Set Font", true);
  325.  
  326.         setFont(new Font("Dialog", Font.PLAIN, 12));
  327.         setBackground(Color.lightGray);
  328.  
  329.         // don't let the user change the size
  330.         setResizable(false);
  331.  
  332.         resize(300, 150);
  333.  
  334.         setBackground(Color.lightGray);
  335.  
  336.         // create the controls for the dialog
  337.         Panel fontSelectionPanel = new Panel();
  338.         fontSelectionPanel.add(NameChoice);
  339.         fontSelectionPanel.add(SizeChoice);
  340.         fontSelectionPanel.add(BoldCheckbox);
  341.         fontSelectionPanel.add(ItalicCheckbox);
  342.         add("North", fontSelectionPanel);
  343.  
  344.         Panel fontSamplePanel = new Panel();
  345.         fontSamplePanel.add(new Label("Sample"));
  346.         fontSamplePanel.add(SampleLabel);
  347.         add("Center", fontSamplePanel);
  348.  
  349.         Panel dialogClosePanel = new Panel();
  350.         dialogClosePanel.add(new Button("OK"));
  351.         dialogClosePanel.add(new Button("Cancel"));
  352.         add("South", dialogClosePanel);
  353.  
  354.         // fill in the choice lists
  355.         FillChoiceLists();
  356.  
  357.         // save the data and start off with the current font
  358.         this.fData = fData;
  359.         NameChoice.select(fData.GetFont().getName());
  360.         SizeChoice.select(Integer.toString(fData.GetFont().getSize()));
  361.  
  362.         BoldCheckbox.setState(
  363.                 (fData.GetFont().getStyle() & Font.BOLD) == Font.BOLD);
  364.         ItalicCheckbox.setState(
  365.                 (fData.GetFont().getStyle() & Font.ITALIC) == Font.ITALIC);
  366.  
  367.         GetSelectedFont(false);
  368.  
  369.         SampleLabel.setFont(selectedFont);
  370.     }
  371.  
  372.     public boolean action(Event evt, Object arg) 
  373.     {
  374.         boolean result = false;            // asume no action
  375.  
  376.         if ("OK".equals(evt.arg)) {
  377.             fData.SetFont(selectedFont);
  378.             dispose();                    // close the dialog
  379.         }
  380.         if ("Cancel".equals(evt.arg))
  381.             dispose();                    // close the dialog
  382.  
  383.         return result;
  384.     }
  385.  
  386.     public boolean handleEvent(Event evt)
  387.     {
  388.         boolean result = false;            // assume no action
  389.  
  390.         // call the superclass for normal processing
  391.         result = super.handleEvent(evt);
  392.  
  393.         // process the event here if not handled yet
  394.         if (!result) {
  395.             if ((evt.target == NameChoice) ||
  396.                     (evt.target == SizeChoice) ||
  397.                     (evt.target == BoldCheckbox) ||
  398.                     (evt.target == ItalicCheckbox)) {
  399.                 GetSelectedFont(true);
  400.                 result = true;
  401.             }
  402.         }
  403.  
  404.         return result;
  405.     }
  406.  
  407.  
  408.     protected void FillChoiceLists()
  409.     {
  410.         String fonts[] = Toolkit.getDefaultToolkit().getFontList();
  411.  
  412.         for (int index = 0; index < fonts.length; index++)
  413.             NameChoice.addItem(fonts[index]);
  414.  
  415.         // there is no way to get the font sizes that are available
  416.         //   on the host, so just add some to the listbox
  417.         for (int index = 8; index <= 36; index += 4)
  418.             SizeChoice.addItem(Integer.toString(index));
  419.     }
  420.  
  421.     protected void GetSelectedFont(boolean redisplay)
  422.     {
  423.         int style = Font.PLAIN;
  424.  
  425.         if (BoldCheckbox.getState())
  426.             style |= Font.BOLD;
  427.  
  428.         if (ItalicCheckbox.getState())
  429.             style |= Font.ITALIC;
  430.  
  431.         selectedFont = new Font(NameChoice.getSelectedItem(), style,
  432.                 Integer.parseInt(SizeChoice.getSelectedItem()));
  433.  
  434.         if (redisplay) {
  435.             // apply the new font to the sample and make sure
  436.             //   it fits in the dialog
  437.             SampleLabel.setFont(selectedFont);
  438.             SampleLabel.resize(SampleLabel.preferredSize());
  439.  
  440.             // tell the layout manager to do it's job again
  441.             validate();
  442.         }
  443.     }
  444. }