home *** CD-ROM | disk | FTP | other *** search
/ BURKS 2 / BURKS_AUG97.ISO / BURKS / LANGUAGE / JAVA / NOTES / SOURCE / evntdemo.jav < prev    next >
Text File  |  1996-12-20  |  7KB  |  195 lines

  1.  
  2. /*
  3.     An applet for demonstrating the use of several GUI components
  4.     (Canvas, Checkbox, Choice, Panel, Scrollbar, TextField) and their
  5.     associated events.
  6.     
  7.     This file defines two classes: EventDemo and ColorCanvas.
  8.     
  9.     David Eck, September 2, 1996
  10. */
  11.  
  12.  
  13. import java.awt.*;
  14. import java.applet.*;
  15.  
  16. public class EventDemo extends Applet {
  17.  
  18.       // An applet that displays a shape and some text.  Color of
  19.       // shape and text is controlled by a vertical scroll bar.
  20.       // Color of background is controlled by a horizontal scroll bar.
  21.       // Text to be displayed can be entered in a TextField.
  22.       // Shape to be displayed can be selected from a Choice componnet.
  23.       // Bright or dim colors can be selected using a Chackbox.
  24.       // The display area is implemented as a ColorCanvas; the 
  25.       // ColorCanvas class is defined later in this file.
  26.       
  27.    ColorCanvas display;  // display area
  28.    Choice shapeChoice;   // for selecting which shape to display
  29.    Checkbox brightColors;// for selecting bright or dim colors
  30.    TextField text;       // for entering the text to be displayed
  31.    Scrollbar hScroll;    // horizontal scroll bar
  32.    Scrollbar vScroll;    // vertical scroll bar
  33.       
  34.    public void init() {  // set up contents of applet
  35.              
  36.        Panel topPanel = new Panel(); // to hold display and scroll bars
  37.        topPanel.setLayout(new BorderLayout());
  38.        display = new ColorCanvas();
  39.        topPanel.add("Center", display);
  40.        hScroll = new Scrollbar(Scrollbar.HORIZONTAL,0,1,0,100);
  41.        topPanel.add("South", hScroll);
  42.        vScroll = new Scrollbar(Scrollbar.VERTICAL,50,1,0,100);
  43.        topPanel.add("East", vScroll);
  44.              
  45.        Panel bottomPanel = new Panel();  // for controls
  46.        bottomPanel.setLayout(new GridLayout(1,3,5,5));
  47.        shapeChoice = new Choice();
  48.        shapeChoice.addItem("Rectangle");
  49.        shapeChoice.addItem("Oval");
  50.        shapeChoice.addItem("RoundRect");
  51.        bottomPanel.add(shapeChoice);
  52.        brightColors = new Checkbox("Bright Colors");
  53.        bottomPanel.add(brightColors);
  54.        text = new TextField("Hello World");
  55.        bottomPanel.add(text);
  56.              
  57.        setLayout(new BorderLayout(5,5));  // applies to applet itself
  58.        add("Center", topPanel);
  59.        add("South", bottomPanel);
  60.              
  61.        setBackground(Color.darkGray);   // background for applet
  62.        setDisplayColors();  // defined below
  63.              
  64.    } // end of init()
  65.          
  66.    public Insets insets() {  // leave border around edge of applet
  67.       return new Insets(5,5,5,5);
  68.    }
  69.          
  70.    public boolean action(Event evt, Object arg) {
  71.       if (evt.target == shapeChoice) {
  72.          // user has selected a shape; set the shape
  73.          // variable in the display, and ask the system
  74.          // to redraw the display 
  75.          switch (shapeChoice.getSelectedIndex()) {
  76.             case 0:
  77.                display.shape = ColorCanvas.RECT;
  78.                break;
  79.             case 1:
  80.                display.shape = ColorCanvas.OVAL;
  81.                break;
  82.             case 2:
  83.                display.shape = ColorCanvas.ROUNDED;
  84.                break;
  85.          }
  86.          display.repaint();
  87.       }
  88.       else if (evt.target == brightColors) {
  89.          // user has changed the state of the checkbox;
  90.          // reset the colors for the display,
  91.          // and ask the system to redraw the display
  92.          setDisplayColors();
  93.          display.repaint();
  94.       }
  95.       else if (evt.target == text) {
  96.          // user has entered new text in the text field
  97.          // and has pressed return; set the corresponding
  98.          // variable in the display, and ask system to redraw it
  99.          display.text = text.getText();
  100.          display.repaint();
  101.       }
  102.       return true;
  103.    } // end of action()
  104.          
  105.    public boolean handleEvent(Event evt) {
  106.       if ( evt.id == Event.SCROLL_LINE_UP ||
  107.            evt.id == Event.SCROLL_LINE_DOWN ||
  108.            evt.id == Event.SCROLL_PAGE_UP ||
  109.            evt.id == Event.SCROLL_PAGE_DOWN ||
  110.            evt.id == Event.SCROLL_ABSOLUTE ) {
  111.          // user had changed the value of one of the
  112.          // scroll bars;  adjust the colors in the display
  113.          // and repaint it.  (I don't have to check
  114.          // which scroll bar it is, because setDisplayColors()
  115.          // always checks the values of both scroll bars.)
  116.          setDisplayColors();
  117.          Graphics g = display.getGraphics();
  118.          display.update(g); // call update() for immediate
  119.          g.dispose(); // repainting, while the user is scrolling
  120.          return true;
  121.       }               
  122.       else
  123.          return super.handleEvent(evt);
  124.    } // end of handleEvent()
  125.                
  126.    void setDisplayColors() {
  127.         // set foreground and background colors of display,
  128.         // depending on values of scroll bars and
  129.         // on state of the checkbox.  (Colors are made
  130.         // using Color.getHSBColor(float,float,float),
  131.         // which creates a color given a hue, a satuation,
  132.         // and a brightness.  The parametes must be between
  133.         // 0.0 and 1.0.)
  134.       float backgroundHue = hScroll.getValue() / 100.0F;
  135.       float foregroundHue = vScroll.getValue() / 100.0F;
  136.       float saturation = 1.0F;
  137.       float brightness;
  138.       if (brightColors.getState())
  139.          brightness = 1.0F;
  140.       else 
  141.          brightness = 0.6F;
  142.       Color backgroundColor = 
  143.              Color.getHSBColor(backgroundHue,saturation,brightness);
  144.       Color foregroundColor = 
  145.              Color.getHSBColor(foregroundHue,saturation,brightness);
  146.       display.setBackground(backgroundColor);
  147.       display.setForeground(foregroundColor);
  148.    } // end of setDisplayColors()
  149.          
  150. } // end of class Event Demo
  151.  
  152.  
  153.  
  154. class ColorCanvas extends Canvas {
  155.        
  156.       // Display a shape and some text.
  157.       // The canvas's setForeground() and setBackground()
  158.       // methods should be called to set the colors to
  159.       // be used for drawing.
  160.        
  161.    public String text; // text to be displayed
  162.    public int shape;   // code for shape to be displayed;
  163.        
  164.    public final static int RECT = 0;  // shape code for a rectangle
  165.    public final static int OVAL = 1;  // shape code for an oval
  166.    public final static int ROUNDED = 2; // shape code for an round rect
  167.  
  168.    public ColorCanvas() {
  169.        text = "Hello World";  // default text
  170.        shape = RECT;  // default shape
  171.    }
  172.        
  173.    public void paint(Graphics g) {
  174.        int width = size().width;   // get size of canvas
  175.        int height = size().height;
  176.        int shape_left = width / 9;  // compute position and size of shape
  177.        int shape_top = height / 3;
  178.        int shape_width = (7*width / 9);
  179.        int shape_height = (5*height / 9);
  180.        switch (shape) {   // draw the shape
  181.           case RECT:
  182.              g.fillRect(shape_left,shape_top,shape_width,shape_height);
  183.              break;
  184.           case OVAL:
  185.              g.fillOval(shape_left,shape_top,shape_width,shape_height);
  186.              break;
  187.           case ROUNDED:
  188.              g.fillRoundRect(shape_left,shape_top,shape_width,shape_height,16,16);
  189.              break;
  190.        }
  191.        g.drawString(text,width/9,2*height/9);  // draw the text
  192.    }
  193.        
  194.  }  // end of class ColorCanvas
  195.