home *** CD-ROM | disk | FTP | other *** search
Java Source | 1996-08-14 | 4.0 KB | 171 lines |
- // EntryKeypad.java
- // 28.02.96
- //
- // A keypad with a display
-
- package cybcerone.utils;
-
- import java.awt.Event;
- import java.awt.Graphics;
- import java.awt.Rectangle;
- import java.awt.Color;
-
- /**
- * This is a Keypad with a box for input. Can be used to enter
- * letters and numbers.
- */
- public class EntryKeypad extends Keypad {
- private Rectangle entryBox = scale (54, 212, 182, 29);
-
- private static final String trailer =
- " . . . . . . . . . . . . . . . . . .";
-
- /** This erases the current entry. */
- protected static final Character eraseChar = new Character ('|');
-
- /** This is the backspace character. */
- protected static final Character backspaceChar = new Character ((char)8);
-
- private StringBuffer entry;
-
-
- /**
- * One EntryKeypad to go.
- */
- public EntryKeypad (String id, String statusText, Appletlike app) {
- super (id, statusText, app);
- setFont (Fonts.bigFont);
- reset ();
- }
-
- protected void setEntryBox (Rectangle box) {
- entryBox = scale (box);
- }
-
- /**
- * Add a logical button to this keypad. Note that the value must be
- * of type Character (no way to enforce this since it'll use the parent
- * method if you pass a different type).
- */
- protected void addButton (Character value, int x, int y,
- int width, int height) {
- super.addButton (value, x, y, width, height);
- }
-
- /**
- * Draw the background, the text in the entry box, and then the selected
- * button.
- */
- public void paint (Graphics g) {
- super.paint (g);
- paintEntryBox (g);
- }
-
- protected void paintEntryBox (Graphics g) {
- Graphics box = g.create (entryBox.x, entryBox.y,
- entryBox.width, entryBox.height);
- box.setColor (Color.white);
- box.fillRect (0, 0, entryBox.width, entryBox.height);
- box.setColor (Color.black);
- box.drawString (entry + trailer, scale (4), scale (23));
- }
-
- protected void paintActive (Graphics g) {
- KeypadButton activeButton = buttons.getHighlighted ();
-
- if (activeBg != null && activeButton != null) {
- Rectangle placement = activeButton.getPlacement ();
- Graphics active = g.create ();
- active.clipRect (placement.x, placement.y,
- placement.width,placement.height);
- active.drawImage (activeBg, 0, 0, this);
- }
- }
-
- /** Clear the entry. */
- public void reset () {
- entry = new StringBuffer ();
- update ();
- }
-
- /** Add this letter to the entry. */
- private void addLetter (Character letter) {
- entry.append (letter);
- }
-
- /** Delete the last letter of the entry. */
- private void deleteLetter () {
- int length = entry.length ();
-
- if (length > 0) {
- entry.setLength (length - 1);
- }
- }
-
- /**
- * This button was clicked. Get it's value. If it's the erase
- * character, reset. If it's the backspace character, delete
- * a letter. Otherwise, add the letter to the entry.
- */
- public void update (KeypadButton theButton) {
- Character theValue;
-
- if (theButton != null) {
- theValue = (Character)theButton.getValue ();
-
- buttons.setHighlighted (theButton);
-
- if (theValue.equals (eraseChar))
- reset ();
- else if (theValue.equals (backspaceChar))
- deleteLetter ();
- else
- addLetter (theValue);
-
- update ();
- }
- }
-
- /** update the parent */
- private void update () {
- repaint ();
- app.update (entry.toString ());
- }
-
- /**
- * The user clicked somewhere, take appropriate action.
- */
- public boolean mouseDown (Event evt, int x, int y) {
- update (whichButton (x, y));
- return true;
- }
-
- /**
- * Unpress the button.
- */
- public boolean mouseUp (Event evt, int x, int y) {
- buttons.clearHighlighted ();
- repaint ();
- return true;
- }
-
- /**
- * The user pressed a key. Make it look like he pressed the button on
- * the keypad.
- */
- public boolean keyDown (Event evt, int key) {
- update (whichButton (new Character (Character.toUpperCase ((char)key))));
- return true;
- }
-
- /**
- * The key's up, so unpress it.
- */
- public boolean keyUp (Event evt, int key) {
- buttons.clearHighlighted ();
- repaint ();
- return true;
- }
-
- }
-