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

  1. // DualKeypad.java
  2. // 01.03.96
  3. //
  4. // A keypad with two regions of pressable buttons
  5. // i.e. two buttons can be pressed at the same time, one in each region
  6.  
  7. package cybcerone.utils;
  8.  
  9. import java.awt.Event;
  10. import java.awt.Graphics;
  11. import java.awt.Rectangle;
  12.  
  13. public class DualKeypad extends Keypad {
  14.  
  15.   /** this is the second group of buttons */
  16.   protected KeypadButtonGroup moreButtons;
  17.  
  18.   public DualKeypad (String id, String statusText, Appletlike app) {
  19.     super (id, statusText, app);
  20.     
  21.     moreButtons = new KeypadButtonGroup ();
  22.   }
  23.  
  24.   /** 
  25.    * Add a logical button to this DualKeypad 
  26.    *
  27.    * @param whichGroup all true buttons are in 1 group, false in the other
  28.    **/
  29.   protected void addButton (Object value, int x, int y,
  30.                 int width, int height, boolean whichGroup) {
  31.     if (whichGroup) {
  32.       addButton (value, x, y, width, height, buttons);
  33.     } else {
  34.       addButton (value, x, y, width, height, moreButtons);
  35.     }
  36.   }
  37.  
  38.   protected KeypadButton whichButton (int x, int y) {
  39.     KeypadButton theButton = super.whichButton (x, y);
  40.     
  41.     if (theButton == null)
  42.       theButton = moreButtons.whichButton (x, y);
  43.  
  44.     return theButton;
  45.   }
  46.  
  47.   protected KeypadButton whichButton (Object value) {
  48.     KeypadButton theButton = super.whichButton (value);
  49.  
  50.     if (theButton == null)
  51.       theButton = moreButtons.whichButton (value);
  52.  
  53.     return theButton;
  54.   }
  55.  
  56.   /**
  57.    * Paint the active buttons.
  58.    */
  59.   protected void paintActive (Graphics g) {
  60.     paintActive (g, buttons.getHighlighted ());
  61.     paintActive (g, moreButtons.getHighlighted ());
  62.   }
  63.  
  64.   protected void paintActive (Graphics g, KeypadButton activeButton) {
  65.     if (activeBg != null && activeButton != null) {
  66.       Rectangle placement = activeButton.getPlacement ();
  67.       Graphics active = g.create ();
  68.       active.clipRect (placement.x, placement.y,
  69.                placement.width, placement.height);
  70.       active.drawImage (activeBg, 0, 0, this);
  71.     }
  72.   }
  73.     
  74.  
  75.   /**
  76.    * If the user clicked on a button, make it the active button within
  77.    * its group, send its value to the parent, and repaint.
  78.    */
  79.   public boolean mouseDown (Event evt, int x, int y) {
  80.     KeypadButton theButton = whichButton (x, y);
  81.  
  82.     if (theButton != null) {
  83.       if (buttons.contains (theButton)) {
  84.     buttons.setHighlighted (theButton);
  85.       } else {
  86.     moreButtons.setHighlighted (theButton);
  87.       }
  88.       app.update (theButton.getValue ());
  89.       repaint ();
  90.     }
  91.     return true;
  92.   }
  93.  
  94.   public void update (KeypadButton theButton) {
  95.     if (theButton != null) {
  96.       if (buttons.contains (theButton))
  97.     buttons.setHighlighted (theButton);
  98.       else
  99.     moreButtons.setHighlighted (theButton);
  100.       app.update (theButton.getValue ());
  101.       repaint ();
  102.     }
  103.   }
  104. }
  105.