home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / pc / java / unuy2wen / cybcerone / utils / keypad.java < prev    next >
Encoding:
Java Source  |  1996-08-14  |  3.2 KB  |  130 lines

  1. // Keypad.java
  2. // 28.02.96
  3. //
  4. // your basic keypad
  5.  
  6. package cybcerone.utils;
  7.  
  8. import java.awt.Graphics;
  9. import java.awt.Event;
  10. import java.awt.Rectangle;
  11. import java.awt.Image;
  12. import java.awt.image.ImageFilter;
  13. import java.awt.image.CropImageFilter;
  14. import java.awt.image.FilteredImageSource;
  15.  
  16.  
  17. /**
  18.  * A panel that automatically takes the right side of the screen and
  19.  * contains a bunch of buttons that the user can click on.
  20.  */
  21. public class Keypad extends IdPanel {
  22.   private static final Rectangle placement = new Rectangle (734, 0, 290, 668);
  23.  
  24.   /**
  25.    * The standard background image should be the keypad with the buttons
  26.    * unpressed.  When one is pressed, the correct portion of this image
  27.    * will be drawn over top of it.  This image should therefore contain
  28.    * all buttons pressed at the same time.
  29.    */
  30.   protected Image activeBg;
  31.  
  32.   /**
  33.    * This is the actual data structure that holds the buttons.
  34.    */
  35.   protected KeypadButtonGroup buttons;
  36.   
  37.   /** Make me. */
  38.   public Keypad (String id, String statusText, Appletlike app) {
  39.     super (id, statusText, app);
  40.     reshape (placement);
  41.  
  42.     buttons = new KeypadButtonGroup ();
  43.   }
  44.  
  45.   /**
  46.    * Set this single image showing all the buttons pushed.  Regions of this
  47.    * are clipped to show individual buttons pushed.
  48.    */
  49.   protected void setActiveBackground (Image activeBg) {
  50.     this.activeBg = activeBg;
  51.   }
  52.   
  53.   /**
  54.    * Same as above, but a filename and priority.
  55.    */
  56.   protected void setActiveBackground (String activeBgFile, int priority) {
  57.     this.activeBg = app.getImage (activeBgFile, priority);
  58.   }
  59.  
  60.   /**
  61.    * Add a logical button to this keypad.
  62.    */
  63.   protected void addButton (Object value, int x, int y,
  64.                 int width, int height) {
  65.     addButton (value, x, y, width, height, buttons);
  66.   }
  67.  
  68.   protected void addButton (Object value, int x, int y, int width, int height,
  69.                 KeypadButtonGroup theButtonGroup) {
  70.     KeypadButton theButton = 
  71.       new KeypadButton (value, scale (x), scale (y), 
  72.             scale (width), scale (height));
  73.     
  74.     theButtonGroup.add (theButton);
  75.  
  76.   }
  77.  
  78.  
  79.   /**
  80.    * Which button is at (x, y)?
  81.    */
  82.   protected KeypadButton whichButton (int x, int y) {
  83.     return buttons.whichButton (x, y);
  84.   }
  85.  
  86.   /** 
  87.    * Which button has this value?
  88.    */
  89.   protected KeypadButton whichButton (Object value) {
  90.     return buttons.whichButton (value);
  91.   }
  92.  
  93.   public void paint (Graphics g) {
  94.     super.paint (g);
  95.     paintActive (g);
  96.   }
  97.  
  98.   /**
  99.    * Paint the active button.
  100.    */
  101.   protected void paintActive (Graphics g) {
  102.     KeypadButton activeButton = buttons.getHighlighted ();
  103.  
  104.     if (activeBg != null && activeButton != null) {
  105.       Rectangle placement = activeButton.getPlacement ();
  106.       g.clipRect (placement.x, placement.y, placement.width, placement.height);
  107.       g.drawImage (activeBg, 0, 0, this);
  108.     }
  109.   }
  110.  
  111.   public void update (KeypadButton theButton) {
  112.     if (theButton != null) {
  113.       buttons.setHighlighted (theButton);
  114.       app.update (theButton.getValue ());
  115.       repaint ();
  116.     }
  117.   }      
  118.  
  119.   /**
  120.    * If the user clicked on a button, make it the active, send it's
  121.    * value to the parent, and repaint.
  122.    */
  123.   public boolean mouseDown (Event evt, int x, int y) {
  124.     KeypadButton theButton = whichButton (x, y);
  125.     update (theButton);
  126.     return true;
  127.   }
  128.  
  129. }
  130.