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

  1. // TextPanel.java
  2. // 25.03.96
  3. //
  4. // a panel for text
  5.  
  6. package cybcerone.utils;
  7.  
  8. import java.awt.Color;
  9. import java.awt.Font;
  10. import java.awt.Graphics;
  11. import java.awt.FontMetrics;
  12. import java.awt.Event;
  13.  
  14. /**
  15.  * A panel to put some text in.
  16.  */
  17. public class TextPanel extends IdPanel {
  18.   private Font textFont;
  19.   private Color textColor = Color.white;
  20.   private Color highlightColor = Color.red;
  21.  
  22.   protected String text;
  23.   protected int y;
  24.   protected FontMetrics metrics;
  25.  
  26.   public TextPanel (String id, String statusText, Appletlike app) {
  27.     super (id, statusText, app);
  28.     setForeground (textColor);
  29.  
  30.     textFont = Fonts.bigFont;
  31.   }
  32.   
  33.   public void setFont (Font textFont) {
  34.     this.textFont = textFont;
  35.     setMetrics ();
  36.   }
  37.  
  38.   protected void setMetrics () {
  39.     metrics = getFontMetrics (textFont);
  40.     y = size().height - metrics.getMaxDescent ();
  41.   }    
  42.   
  43.   public void setColor (Color textColor) {
  44.     this.textColor = textColor;
  45.     setForeground (textColor);
  46.   }
  47.   
  48.   public void setHighlight (Color highlightColor) {
  49.     this.highlightColor = highlightColor;
  50.   }
  51.   
  52.   public void setText (String text) {
  53.     this.text = text;
  54.     if (metrics == null)
  55.       setMetrics ();
  56.     
  57.     repaint ();
  58.   }
  59.  
  60.   public String getText () { return text; }
  61.   
  62.   public boolean mouseEnter (Event evt, int x, int y) {
  63.     if (getNext () != null) {
  64.       setForeground (highlightColor);
  65.       repaint ();
  66.     }
  67.     return super.mouseEnter (evt, x, y);
  68.   }
  69.   
  70.   public boolean mouseExit (Event evt, int x, int y) {
  71.     if (getNext () != null) {
  72.       setForeground (textColor);
  73.       repaint ();
  74.     }
  75.     return super.mouseExit (evt, x, y);
  76.   }
  77.  
  78.   public void paint (Graphics g) {
  79.     g.setColor (getBackground ());
  80.     g.fillRect (0, 0, size().width, size().height);
  81.     
  82.     super.paint (g);
  83.  
  84.     g.setFont (textFont);
  85.     g.setColor (getForeground ());
  86.     paintText (g);
  87.   }
  88.   
  89.   protected void paintText (Graphics g) {
  90.     if (text != null) {
  91.       g.drawString (text, 0, y);
  92.     }
  93.   }
  94. }
  95.