
/*
    An applet for demonstrating the use of several GUI components
    (Canvas, Checkbox, Choice, Panel, Scrollbar, TextField) and their
    associated events.
    
    This file defines two classes: EventDemo and ColorCanvas.
    
    David Eck, September 2, 1996
*/


import java.awt.*;
import java.applet.*;

public class EventDemo extends Applet {

      // An applet that displays a shape and some text.  Color of
      // shape and text is controlled by a vertical scroll bar.
      // Color of background is controlled by a horizontal scroll bar.
      // Text to be displayed can be entered in a TextField.
      // Shape to be displayed can be selected from a Choice componnet.
      // Bright or dim colors can be selected using a Chackbox.
      // The display area is implemented as a ColorCanvas; the 
      // ColorCanvas class is defined later in this file.
      
   ColorCanvas display;  // display area
   Choice shapeChoice;   // for selecting which shape to display
   Checkbox brightColors;// for selecting bright or dim colors
   TextField text;       // for entering the text to be displayed
   Scrollbar hScroll;    // horizontal scroll bar
   Scrollbar vScroll;    // vertical scroll bar
      
   public void init() {  // set up contents of applet
             
       Panel topPanel = new Panel(); // to hold display and scroll bars
       topPanel.setLayout(new BorderLayout());
       display = new ColorCanvas();
       topPanel.add("Center", display);
       hScroll = new Scrollbar(Scrollbar.HORIZONTAL,0,1,0,100);
       topPanel.add("South", hScroll);
       vScroll = new Scrollbar(Scrollbar.VERTICAL,50,1,0,100);
       topPanel.add("East", vScroll);
             
       Panel bottomPanel = new Panel();  // for controls
       bottomPanel.setLayout(new GridLayout(1,3,5,5));
       shapeChoice = new Choice();
       shapeChoice.addItem("Rectangle");
       shapeChoice.addItem("Oval");
       shapeChoice.addItem("RoundRect");
       bottomPanel.add(shapeChoice);
       brightColors = new Checkbox("Bright Colors");
       bottomPanel.add(brightColors);
       text = new TextField("Hello World");
       bottomPanel.add(text);
             
       setLayout(new BorderLayout(5,5));  // applies to applet itself
       add("Center", topPanel);
       add("South", bottomPanel);
             
       setBackground(Color.darkGray);   // background for applet
       setDisplayColors();  // defined below
             
   } // end of init()
         
   public Insets insets() {  // leave border around edge of applet
      return new Insets(5,5,5,5);
   }
         
   public boolean action(Event evt, Object arg) {
      if (evt.target == shapeChoice) {
         // user has selected a shape; set the shape
         // variable in the display, and ask the system
         // to redraw the display 
         switch (shapeChoice.getSelectedIndex()) {
            case 0:
               display.shape = ColorCanvas.RECT;
               break;
            case 1:
               display.shape = ColorCanvas.OVAL;
               break;
            case 2:
               display.shape = ColorCanvas.ROUNDED;
               break;
         }
         display.repaint();
      }
      else if (evt.target == brightColors) {
         // user has changed the state of the checkbox;
         // reset the colors for the display,
         // and ask the system to redraw the display
         setDisplayColors();
         display.repaint();
      }
      else if (evt.target == text) {
         // user has entered new text in the text field
         // and has pressed return; set the corresponding
         // variable in the display, and ask system to redraw it
         display.text = text.getText();
         display.repaint();
      }
      return true;
   } // end of action()
         
   public boolean handleEvent(Event evt) {
      if ( evt.id == Event.SCROLL_LINE_UP ||
           evt.id == Event.SCROLL_LINE_DOWN ||
           evt.id == Event.SCROLL_PAGE_UP ||
           evt.id == Event.SCROLL_PAGE_DOWN ||
           evt.id == Event.SCROLL_ABSOLUTE ) {
         // user had changed the value of one of the
         // scroll bars;  adjust the colors in the display
         // and repaint it.  (I don't have to check
         // which scroll bar it is, because setDisplayColors()
         // always checks the values of both scroll bars.)
         setDisplayColors();
         Graphics g = display.getGraphics();
         display.update(g); // call update() for immediate
         g.dispose(); // repainting, while the user is scrolling
         return true;
      }               
      else
         return super.handleEvent(evt);
   } // end of handleEvent()
               
   void setDisplayColors() {
        // set foreground and background colors of display,
        // depending on values of scroll bars and
        // on state of the checkbox.  (Colors are made
        // using Color.getHSBColor(float,float,float),
        // which creates a color given a hue, a satuation,
        // and a brightness.  The parametes must be between
        // 0.0 and 1.0.)
      float backgroundHue = hScroll.getValue() / 100.0F;
      float foregroundHue = vScroll.getValue() / 100.0F;
      float saturation = 1.0F;
      float brightness;
      if (brightColors.getState())
         brightness = 1.0F;
      else 
         brightness = 0.6F;
      Color backgroundColor = 
             Color.getHSBColor(backgroundHue,saturation,brightness);
      Color foregroundColor = 
             Color.getHSBColor(foregroundHue,saturation,brightness);
      display.setBackground(backgroundColor);
      display.setForeground(foregroundColor);
   } // end of setDisplayColors()
         
} // end of class Event Demo



class ColorCanvas extends Canvas {
       
      // Display a shape and some text.
      // The canvas's setForeground() and setBackground()
      // methods should be called to set the colors to
      // be used for drawing.
       
   public String text; // text to be displayed
   public int shape;   // code for shape to be displayed;
       
   public final static int RECT = 0;  // shape code for a rectangle
   public final static int OVAL = 1;  // shape code for an oval
   public final static int ROUNDED = 2; // shape code for an round rect

   public ColorCanvas() {
       text = "Hello World";  // default text
       shape = RECT;  // default shape
   }
       
   public void paint(Graphics g) {
       int width = size().width;   // get size of canvas
       int height = size().height;
       int shape_left = width / 9;  // compute position and size of shape
       int shape_top = height / 3;
       int shape_width = (7*width / 9);
       int shape_height = (5*height / 9);
       switch (shape) {   // draw the shape
          case RECT:
             g.fillRect(shape_left,shape_top,shape_width,shape_height);
             break;
          case OVAL:
             g.fillOval(shape_left,shape_top,shape_width,shape_height);
             break;
          case ROUNDED:
             g.fillRoundRect(shape_left,shape_top,shape_width,shape_height,16,16);
             break;
       }
       g.drawString(text,width/9,2*height/9);  // draw the text
   }
       
 }  // end of class ColorCanvas

