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

  1. // IdPanel.java
  2. // 27.02.96
  3. //
  4. // your basic panel
  5.  
  6. package cybcerone.utils;
  7.  
  8. import java.awt.Panel;
  9. import java.awt.Image;
  10. import java.awt.Graphics;
  11. import java.awt.Event;
  12. import java.awt.Rectangle;
  13. import java.awt.Point;
  14. import java.awt.Dimension;
  15. import java.awt.Color;
  16.  
  17. /**
  18.  * The IdPanel is just a regular panel with a few features tacked on.  It
  19.  * has an id, displays text when the mouse passes over it, and can be set
  20.  * so that, if clicked on, it updates its parent with a string (used in
  21.  * this case to jump to another page).  Also, an image can be set to be
  22.  * used as the background.
  23.  */
  24. public class IdPanel extends Panel {
  25.   private String id;
  26.   private String statusText;
  27.  
  28.   /**
  29.    * When the user clicks on an IdPanel, if this value is not null it is
  30.    * used as the ID of a panel to switch to.  If it's a Mapable object, 
  31.    * the mapping module will be activated and this object will be displayed.
  32.    */
  33.   protected Object next;
  34.  
  35.   /**
  36.    * The parent of this IdPanel or, if it's at the top level, a link to
  37.    * the Appletlike object that's running it.
  38.    */
  39.   protected Appletlike app;
  40.  
  41.   /** 
  42.    * The background image.
  43.    */
  44.   private Image background;
  45.  
  46.   /**
  47.    * @parameter id The id for this panel.
  48.    * @parameter statusText The text to display on the status bar.
  49.    * @parameter app The parent of this IdPanel.
  50.    */
  51.   public IdPanel (String id, String statusText, Appletlike app) {
  52.     setLayout (null);
  53.     this.id = id;
  54.     this.statusText = statusText;
  55.     this.app = app;
  56.     setBackground (Color.black);
  57.   }
  58.  
  59.   /** What was that id again? */
  60.   public String getId () { return id; }
  61.  
  62.   /** The text for the status bar. */
  63.   public String getStatusText () { return statusText; }
  64.  
  65.   /**
  66.    * Change the text to display on the status bar.
  67.    */
  68.   public void setStatusText (String statusText) {
  69.     this.statusText = statusText;
  70.   }
  71.  
  72.   /** The id of the panel to switch to. */
  73.   public Object getNext () { return next; }
  74.   
  75.   /** Set the background image to this image */
  76.   public void setBackground (Image background) {
  77.     this.background = background;
  78.   }
  79.  
  80.   /** 
  81.    * Set the background image to the image with this filename, and
  82.    * assign it this priority while loading.
  83.    */
  84.   public void setBackground (String bgFile, int priority) {
  85.     setBackground (app.getImage (bgFile, priority));
  86.   }
  87.  
  88.   /** If I'm clicked on, where do I go? */
  89.   public void setNext (Object next) {
  90.     this.next = next;
  91.   }
  92.   
  93.   /** Just draws the background. */
  94.   public void paint (Graphics g) {
  95.     if (background != null) 
  96.       g.drawImage (background, 0, 0, Color.black, this);
  97.   }
  98.   
  99.   /** Overridden to prevent flickering. */
  100.   public void update (Graphics g) {
  101.     paint (g);
  102.   }
  103.   
  104.   /** Display this panel's status bar text. */
  105.   public boolean mouseEnter (Event evt, int x, int y) {
  106.     app.showStatus (statusText);
  107.     return true;
  108.   }
  109.  
  110.   /** Clear the status bar. */
  111.   public boolean mouseExit (Event evt, int x, int y) {
  112.     app.showStatus ("");
  113.     return true;
  114.   }
  115.  
  116.   /**
  117.    * When the user clicks, update app with the id of the panel to go to.
  118.    */
  119.   public boolean mouseDown (Event evt, int x, int y) {
  120.     if (next != null)
  121.       app.update (next);
  122.     return true;
  123.   }
  124.  
  125.   /**
  126.    * Put yourself in the right place, according to this Rectangle.
  127.    * Scaling is done automatically.
  128.    */
  129.   public void reshape (Rectangle placement) {
  130.     reshape (scale (placement.x), scale (placement.y),
  131.          scale (placement.width), scale (placement.height));
  132.   }
  133.  
  134.   /**
  135.    * Put yourself in the right place, starting at the Point and with
  136.    * the given Dimension.  Scaling is done automatically.
  137.    */
  138.   public void reshape (Point place, Dimension dimensions) {
  139.     reshape (scale (place.x), scale (place.y),
  140.          scale (dimensions.width), scale (dimensions.height));
  141.   }
  142.  
  143.   /** 
  144.    * Scale this number using the current scaling factor.  This method 
  145.    * makes it easier for panels to scale things.
  146.    */
  147.   public static final int scale (int num) {
  148.     return Scaler.scale (num);
  149.   }
  150.  
  151.   /**
  152.    * Same as above, but make a scaled rectangle out of these parameters.
  153.    */
  154.   public static final Rectangle scale (int x, int y, int width, int height) {
  155.     return Scaler.scale (x, y, width, height);
  156.   }
  157.  
  158.   /**
  159.    * Return a scaled version of this rectangle.
  160.    */
  161.   public static final Rectangle scale (Rectangle rec) {
  162.     return Scaler.scale (rec);
  163.   }
  164.  
  165.   /** Start any subpanels. */
  166.   public void start () {
  167.     for (int i = 0; i < countComponents (); i++) {
  168.       if (getComponent (i) instanceof IdPanel) {
  169.     ((IdPanel)getComponent(i)).start ();
  170.       }
  171.     }
  172.   }
  173.   
  174.   /** Stop any subpanels. */
  175.   public void stop () {
  176.     for (int i = 0; i < countComponents (); i++)
  177.       if (getComponent (i) instanceof IdPanel)
  178.     ((IdPanel)getComponent(i)).stop ();
  179.   }
  180. }
  181.