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 / EtchedRectangle.java < prev    next >
Encoding:
Java Source  |  1999-02-11  |  3.2 KB  |  98 lines

  1. package gjt;
  2.  
  3. import java.awt.*;
  4.  
  5. /**
  6.  * A DrawnRectangle that draws an etched border.<p>
  7.  * 
  8.  * Drawn etched in by default, drawing style used by paint() is 
  9.  * controlled by etchedIn() and etchedOut().  Note that 
  10.  * etchedIn() and etchedOut() do not result in anything being 
  11.  * painted, but only set the state for the next call to paint(). 
  12.  * To set the state and paint in one operation, use 
  13.  * paintEtchedIn() and paintEtchedOut().<p>
  14.  * 
  15.  * Although it is permissible to set the thickness of 
  16.  * EtchedRectangles, they tend to loose the etching effect 
  17.  * if thickness is greater than 4.<p>
  18.  *
  19.  * The current state of the rectangle may be obtained by 
  20.  * calling isEtchedIn().
  21.  *
  22.  * @version 1.0, Apr 1 1996
  23.  * @author  David Geary
  24.  * @see     DrawnRectangle
  25.  * @see     ThreeDRectangle
  26.  * @see     gjt.test.DrawnRectangleTest
  27.  */
  28. public class EtchedRectangle extends DrawnRectangle {
  29.     protected static Etching _defaultEtching = Etching.IN;
  30.     private Etching etching;
  31.  
  32.     public EtchedRectangle(Component drawInto) {
  33.         this(drawInto, _defaultEtching, 
  34.              _defaultThickness, 0, 0, 0, 0);
  35.     }
  36.     public EtchedRectangle(Component drawInto, int thickness) {
  37.         this(drawInto, _defaultEtching, thickness, 0, 0, 0, 0);
  38.     }
  39.     public EtchedRectangle(Component drawInto, int x, int y, 
  40.                                                int w, int h) {
  41.         this(drawInto, _defaultEtching, 
  42.              _defaultThickness, x, y, w, h);
  43.     }
  44.     public EtchedRectangle(Component drawInto, int thickness,
  45.                                                int x, int y, 
  46.                                                int w, int h) {
  47.         this(drawInto, _defaultEtching, thickness, x, y, w, h);
  48.     }
  49.     public EtchedRectangle(Component drawInto, Etching etching, 
  50.                            int thickness, int x, int y, 
  51.                            int w, int h) {
  52.         super(drawInto, thickness, x, y, w, h);
  53.         this.etching = etching;
  54.     }
  55.     public void    etchedIn  () { etching = Etching.IN;        }
  56.     public void    etchedOut () { etching = Etching.OUT;       }
  57.     public boolean isEtchedIn() { return etching == Etching.IN;}
  58.  
  59.     public void paint() {
  60.         if(etching == Etching.IN) paintEtchedIn();
  61.         else                      paintEtchedOut();
  62.     }
  63.     public void paintEtchedIn() {
  64.         Graphics g = drawInto.getGraphics();
  65.         if(g != null)
  66.             paintEtched(g, getLineColor(), brighter());
  67.  
  68.         etchedIn();
  69.     }
  70.     public void paintEtchedOut() {
  71.         Graphics g = drawInto.getGraphics();
  72.         if(g != null)
  73.             paintEtched(g, brighter(), getLineColor());
  74.  
  75.         etchedOut();
  76.     }
  77.     public String paramString() { 
  78.         return super.paramString() + "," + etching;
  79.     }
  80.     private void paintEtched(Graphics g, 
  81.                              Color topLeft, 
  82.                              Color bottomRight) {
  83.         int  thickness = getThickness();
  84.         int  w = width  - thickness;
  85.         int  h = height - thickness;
  86.  
  87.         g.setColor(topLeft);
  88.         for(int i=0; i < thickness/2; ++i) 
  89.             g.drawRect(x+i, y+i, w, h);
  90.  
  91.         g.setColor(bottomRight);
  92.  
  93.         for(int i=0; i < thickness/2; ++i) 
  94.             g.drawRect(x+(thickness/2)+i, 
  95.                        y+(thickness/2)+i, w, h);
  96.     }
  97. }
  98.