home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 25 / IOPROG_25.ISO / SOFT / JavaS / javastar-eval.exe / data1.cab / Program_Files / examples / namedb / gjt / DrawnRectangle.java < prev    next >
Encoding:
Java Source  |  1999-02-11  |  4.1 KB  |  137 lines

  1. package gjt;
  2.  
  3. import java.awt.*;
  4.  
  5. /**
  6.  * A Rectangle which draws itself inside of a Component.<p>
  7.  * 
  8.  * DrawnRectangles may have their thickness and line color set, 
  9.  * and are capable of reporting their inner bounds (the area 
  10.  * inside the lines).<p>
  11.  *
  12.  * Default thickness is 2.<p>
  13.  *
  14.  * If not set explicitly, the line color used is three shades 
  15.  * darker than the background color of the Component being 
  16.  * drawn into.<p>
  17.  * 
  18.  * DrawnRectangles may be clear()ed, which clears both the 
  19.  * exterior (the lines) and the interior (the area inside of 
  20.  * the lines) of the DrawnRectangle.<p>
  21.  *
  22.  * DrawnRectangles may also be fill()ed with a specified color 
  23.  * by calling fill(Color), or by calling setFillColor(Color) 
  24.  * followed by fill().<p>
  25.  *
  26.  * By default, the fill Color is the background color of the 
  27.  * Component drawn into.<p>
  28.  *
  29.  * @version 1.0, Apr 1 1996
  30.  * @author  David Geary
  31.  * @see     ThreeDRectangle
  32.  * @see     EtchedRectangle
  33.  * @see     Border
  34.  * @see     EtchedBorder
  35.  * @see     ThreeDBorder
  36.  * @see     gjt.test.DrawnRectangleTest
  37.  */
  38. public class DrawnRectangle extends Rectangle {
  39.     protected static int    _defaultThickness = 2;
  40.  
  41.     protected Component drawInto;
  42.     private   int       thick;
  43.     private   Color     lineColor, fillColor;
  44.  
  45.     public DrawnRectangle(Component drawInto) {
  46.         this(drawInto, _defaultThickness, 0, 0, 0, 0);
  47.     }
  48.     public DrawnRectangle(Component drawInto, int thick) {
  49.         this(drawInto, thick, 0, 0, 0, 0);
  50.     }
  51.     public DrawnRectangle(Component drawInto, int x, int y, 
  52.                                               int w, int h) {
  53.         this(drawInto, _defaultThickness, x, y, w, h);
  54.     }
  55.     public DrawnRectangle(Component drawInto, int thick,
  56.                           int x, int y, int w, int h) {
  57.         Assert.notNull(drawInto);
  58.         Assert.notFalse(thick > 0);
  59.  
  60.         this.drawInto = drawInto;
  61.         this.thick    = thick;
  62.         reshape(x,y,w,h);
  63.     }
  64.     public Component component()          {return drawInto;    }
  65.     public int  getThickness  ()          {return thick;       }
  66.     public void setThickness  (int thick) {this.thick = thick; }
  67.  
  68.     public void setLineColor(Color lineColor) {
  69.         this.lineColor = lineColor;
  70.     }
  71.     public void setFillColor(Color fillColor) {
  72.         this.fillColor = fillColor;
  73.     }
  74.     public void fill() {
  75.         fill(getFillColor());      
  76.     }
  77.     public Color getLineColor() {
  78.         if(lineColor == null)
  79.             lineColor = 
  80.             drawInto.getBackground().darker().darker().darker();
  81.         return lineColor;
  82.     }
  83.     public Color getFillColor() {
  84.         if(fillColor == null)
  85.             fillColor = drawInto.getBackground();
  86.         return fillColor;
  87.     }
  88.     public Rectangle getInnerBounds() {
  89.         return new Rectangle(x+thick, y+thick, 
  90.                              width-(thick*2), height-(thick*2));
  91.     }
  92.     public void paint() {
  93.         Graphics g = drawInto.getGraphics();
  94.         paintFlat(g, getLineColor()); 
  95.     }
  96.     private void paintFlat(Graphics g, Color color) {
  97.         if(g != null) {
  98.             g.setColor(color);
  99.             for(int i=0; i < thick; ++i)
  100.                 g.drawRect(x+i, y+i, 
  101.                            width-(i*2)-1, height-(i*2)-1);
  102.         }
  103.     }
  104.     public void clearInterior() {
  105.         fill(drawInto.getBackground());
  106.     }
  107.     public void clearExterior() {
  108.         paintFlat(drawInto.getGraphics(), 
  109.                   drawInto.getBackground());
  110.     }
  111.     public void clear() { 
  112.         clearExterior(); 
  113.         clearInterior(); 
  114.     }
  115.     public void fill(Color color) {
  116.         Graphics g = drawInto.getGraphics();
  117.  
  118.         if(g != null) {
  119.             Rectangle r = getInnerBounds();
  120.             g.setColor(color);
  121.             g.fillRect(r.x, r.y, r.width, r.height);
  122.             setFillColor(color);
  123.         }
  124.     }
  125.     public String toString() {
  126.         return super.toString() + "[" + paramString() + "]";
  127.     }
  128.     public String paramString() {
  129.         return "color=" + getLineColor() + ",thickness=" + 
  130.                 thick + ",fillColor=" + getFillColor();
  131.     }
  132.     protected Color brighter() {
  133.         return 
  134.     getLineColor().brighter().brighter().brighter().brighter();
  135.     }
  136. }
  137.