Source for javax.swing.border.SoftBevelBorder

   1: /* SoftBevelBorder.java -- 
   2:    Copyright (C) 2003 Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
  38: package javax.swing.border;
  39: 
  40: import java.awt.Color;
  41: import java.awt.Component;
  42: import java.awt.Graphics;
  43: import java.awt.Insets;
  44: 
  45: 
  46: /**
  47:  * A rectangular, three pixel thick border that looks like a BevelBorder
  48:  * with slightly softened corners.
  49:  *
  50:  * <p>Like BevelBorder, SoftBevelBorder has a highlight and a shadow
  51:  * color. In the raised variant, the highlight color is used for the
  52:  * top and left edges, and the shadow color is used for the bottom and
  53:  * right edge.  In the lowered variant, color usage is reversed.  For
  54:  * an image, see the documentation of the individual constructors.
  55:  *
  56:  * @author Sascha Brawer (brawer@dandelis.ch)
  57:  */
  58: public class SoftBevelBorder
  59:   extends BevelBorder
  60: {
  61:   /**
  62:    * Determined using the <code>serialver</code> tool
  63:    * of Sun JDK 1.4.1_01 on GNU/Linux 2.4.20. Interestingly,
  64:    * the Apple/Sun JDK 1.3.1 on MacOS X 10.1.5 gives a different
  65:    * value, namely -6658357140774549493L.
  66:    */
  67:   static final long serialVersionUID = 5248789787305979975L;
  68: 
  69: 
  70:   /**
  71:    * Constructs a SoftBevelBorder whose colors will be derived from the
  72:    * background of the enclosed component. The background color is
  73:    * retrieved each time the border is painted, so a SoftBevelBorder
  74:    * constructed by this method will automatically reflect a change
  75:    * to the component&#x2019;s background color.
  76:    *
  77:    * <p><img src="doc-files/SoftBevelBorder-1.png" width="500" height="200"
  78:    * alt="[An illustration showing raised and lowered SoftBevelBorders]" />
  79:    *
  80:    * @param bevelType the desired appearance of the border. The value
  81:    *        must be either {@link BevelBorder#RAISED}
  82:    *        or {@link BevelBorder#LOWERED}.
  83:    *
  84:    * @throws IllegalArgumentException if <code>bevelType</code> has
  85:    *         an unsupported value.
  86:    */
  87:   public SoftBevelBorder(int bevelType)
  88:   {
  89:     super(bevelType);
  90:   }
  91: 
  92: 
  93:   /**
  94:    * Constructs a SoftBevelBorder given its appearance type and two
  95:    * colors for its highlight and shadow.
  96:    *
  97:    * <p><img src="doc-files/SoftBevelBorder-2.png" width="500" height="150"
  98:    * alt="[An illustration showing SoftBevelBorders that were
  99:    * constructed with this method]" />
 100:    *
 101:    * @param bevelType the desired appearance of the border. The value
 102:    *        must be either {@link BevelBorder#RAISED} or {@link
 103:    *        BevelBorder#LOWERED}.
 104:    *
 105:    * @param highlight the color that will be used for the inner side
 106:    *        of the highlighted edges (top and left if if
 107:    *        <code>bevelType</code> is {@link BevelBorder#RAISED};
 108:    *        bottom and right otherwise). The color for the outer side
 109:    *        is a brightened version of this color.
 110:    *
 111:    * @param shadow the color that will be used for the outer side of
 112:    *        the shadowed edges (bottom and right if
 113:    *        <code>bevelType</code> is {@link BevelBorder#RAISED}; top
 114:    *        and left otherwise). The color for the inner side is a
 115:    *        brightened version of this color.
 116:    *
 117:    * @throws IllegalArgumentException if <code>bevelType</code> has an
 118:    *        unsupported value.
 119:    *
 120:    * @throws NullPointerException if <code>highlight</code> or
 121:    *         <code>shadow</code> is <code>null</code>.
 122:    *
 123:    * @see java.awt.Color#brighter()
 124:    */
 125:   public SoftBevelBorder(int bevelType, Color highlight, Color shadow)
 126:   {
 127:     this(bevelType,
 128:          /* highlightOuter */ highlight.brighter(),
 129:          /* highlightInner */ highlight,
 130:          /* shadowOuter */    shadow,
 131:          /* shadowInner */    shadow.brighter());
 132:   }
 133: 
 134: 
 135:   /**
 136:    * Constructs a SoftBevelBorder given its appearance type and all
 137:    * colors.
 138:    *
 139:    * <p><img src="doc-files/SoftBevelBorder-3.png" width="500" height="150"
 140:    * alt="[An illustration showing SoftBevelBorders that were
 141:    * constructed with this method]" />
 142:    *
 143:    * @param bevelType the desired appearance of the border. The value
 144:    *        must be either {@link BevelBorder#RAISED} or {@link
 145:    *        BevelBorder#LOWERED}.
 146:    *
 147:    * @param highlightOuter the color that will be used for the outer
 148:    *        side of the highlighted edges (top and left if
 149:    *        <code>bevelType</code> is {@link BevelBorder#RAISED};
 150:    *        bottom and right otherwise).
 151:    *
 152:    * @param highlightInner the color that will be used for the inner
 153:    *        side of the highlighted edges.
 154:    *
 155:    * @param shadowOuter the color that will be used for the outer side
 156:    *        of the shadowed edges (bottom and right if
 157:    *        <code>bevelType</code> is {@link BevelBorder#RAISED}; top
 158:    *        and left otherwise).
 159:    *
 160:    * @param shadowInner the color that will be used for the inner
 161:    *        side of the shadowed edges.
 162:    *
 163:    * @throws IllegalArgumentException if <code>bevelType</code> has
 164:    *         an unsupported value.
 165:    *
 166:    * @throws NullPointerException if one of the passed colors
 167:    *         is <code>null</code>.
 168:    */
 169:   public SoftBevelBorder(int bevelType,
 170:                          Color highlightOuter, Color highlightInner,
 171:                          Color shadowOuter, Color shadowInner)
 172:   {
 173:     super(bevelType,
 174:           highlightOuter, highlightInner,
 175:           shadowOuter, shadowInner);
 176:   }
 177: 
 178: 
 179:   /**
 180:    * Paints the border for a given component.
 181:    *
 182:    * @param c the component whose border is to be painted.
 183:    * @param g the graphics for painting.
 184:    * @param x the horizontal position for painting the border.
 185:    * @param y the vertical position for painting the border.
 186:    * @param width the width of the available area for painting the border.
 187:    * @param height the height of the available area for painting the border.
 188:    */
 189:   public void paintBorder(Component c, Graphics  g,
 190:                           int x, int y, int width, int height)
 191:   {
 192:     switch (bevelType)
 193:     {
 194:     case RAISED:
 195:       paintSoftBevel(g, x, y, width, height,
 196:                      getHighlightOuterColor(c), getHighlightInnerColor(c),
 197:                      getShadowInnerColor(c), getShadowOuterColor(c));
 198:       break;
 199: 
 200:     case LOWERED:
 201:       paintSoftBevel(g, x, y, width, height,
 202:                      getShadowOuterColor(c), getShadowInnerColor(c),
 203:                      getHighlightInnerColor(c), getHighlightOuterColor(c));
 204:       break;
 205:     }
 206:   }
 207: 
 208: 
 209:   /**
 210:    * Measures the width of this border.
 211:    *
 212:    * @param c the component whose border is to be measured.
 213:    *
 214:    * @return an Insets object whose <code>left</code>, <code>right</code>,
 215:    *         <code>top</code> and <code>bottom</code> fields indicate the
 216:    *         width of the border at the respective edge.
 217:    *
 218:    * @see #getBorderInsets(java.awt.Component, java.awt.Insets)
 219:    */
 220:   public Insets getBorderInsets(Component c)
 221:   {
 222:     return new Insets(3, 3, 3, 3);
 223:   }
 224: 
 225: 
 226:   /**
 227:    * Measures the width of this border, storing the results into a
 228:    * pre-existing Insets object.
 229:    *
 230:    * @param insets an Insets object for holding the result values.
 231:    *        After invoking this method, the <code>left</code>,
 232:    *        <code>right</code>, <code>top</code> and
 233:    *        <code>bottom</code> fields indicate the width of the
 234:    *        border at the respective edge.
 235:    *
 236:    * @return the same object that was passed for <code>insets</code>.
 237:    *
 238:    * @see #getBorderInsets(Component)
 239:    */
 240:   public Insets getBorderInsets(Component c, Insets insets)
 241:   {
 242:     insets.left = insets.right = insets.top = insets.bottom = 3;
 243:     return insets;
 244:   }
 245: 
 246:   
 247:   /**
 248:    * Determines whether this border fills every pixel in its area
 249:    * when painting.
 250:    *
 251:    * <p>The enlarged view (see documentation for constructors) shows
 252:    * that a SoftBevelBorder does not paint all pixels. Therefore,
 253:    * this method always returns <code>false</code>.
 254:    *
 255:    * @return <code>false</code>.
 256:    */
 257:   public boolean isBorderOpaque()
 258:   {
 259:     return false;
 260:   }
 261: 
 262: 
 263:   /**
 264:    * Paints a soft bevel in four colors.
 265:    * 
 266:    * <pre>
 267:    * @@@@@@@@@@@.
 268:    * @@.........#    @ = color a
 269:    * @..        #    . = color b
 270:    * @.         #    X = color c
 271:    * ..        X#    # = color d
 272:    * . ##########</pre>
 273:    *
 274:    * @param g the graphics for painting.
 275:    * @param x the horizontal position for painting the border.
 276:    * @param y the vertical position for painting the border.
 277:    * @param width the width of the available area for painting the border.
 278:    * @param height the height of the available area for painting the border.
 279:    * @param a the color for the outer side of the top and left edges.
 280:    * @param b the color for the inner side of the top and left edges.
 281:    * @param c the color for the inner side of the bottom and right edges.
 282:    * @param d the color for the outer side of the bottom and right edges.
 283:    */
 284:   private static void paintSoftBevel(Graphics g,
 285:                                      int x, int y, int width, int height,
 286:                                      Color a, Color b, Color c, Color d)
 287:   {
 288:     Color oldColor;
 289: 
 290:     oldColor = g.getColor();
 291:     g.translate(x, y);
 292:     width = width - 1;
 293:     height = height - 1;
 294: 
 295:     try
 296:     {
 297:       /* To understand this code, it might be helpful to look at the
 298:        * images that are included with the JavaDoc, especially
 299:        * SoftBevelBorder-3.png. They are located in the "doc-files"
 300:        * subdirectory.
 301:        */
 302:       g.setColor(a);
 303:       g.drawLine(0, 0, width - 1, 0);                   // a, horizontal
 304:       g.drawLine(0, 1, 2, 1);                           // a, horizontal
 305:       g.drawLine(0, 2, 0, height - 1);                  // a, vertical
 306: 
 307:       g.setColor(b);
 308:       g.drawLine(width, 0, width, 0);                   // b, horizontal
 309:       g.drawLine(2, 1, width - 1, 1);                   // b, horizontal
 310:       g.drawLine(1, 2, 2, 2);                           // b, horizontal
 311:       g.drawLine(1, 3, 1, height - 1);                  // b, vertical
 312:       g.drawLine(0, height - 1, 0, height);             // b, vertical
 313: 
 314:       g.setColor(c);
 315:       g.drawLine(width - 1, height - 1,                 // c, one pixel
 316:                  width - 1, height - 1);
 317: 
 318:       g.setColor(d);
 319:       g.drawLine(2, height, width, height);             // d, horizontal
 320:       g.drawLine(width, 2, width, height - 1);          // d, vertical
 321:     }
 322:     finally
 323:     {
 324:       g.translate(-x, -y);
 325:       g.setColor(oldColor);
 326:     }
 327:   }
 328: }