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 extends BevelBorder
  59: {
  60:   /**
  61:    * Determined using the <code>serialver</code> tool
  62:    * of Sun JDK 1.4.1_01 on GNU/Linux 2.4.20. Interestingly,
  63:    * the Apple/Sun JDK 1.3.1 on MacOS X 10.1.5 gives a different
  64:    * value, namely -6658357140774549493L.
  65:    */
  66:   static final long serialVersionUID = 5248789787305979975L;
  67: 
  68: 
  69:   /**
  70:    * Constructs a SoftBevelBorder whose colors will be derived from the
  71:    * background of the enclosed component. The background color is
  72:    * retrieved each time the border is painted, so a SoftBevelBorder
  73:    * constructed by this method will automatically reflect a change
  74:    * to the component&#x2019;s background color.
  75:    *
  76:    * <p><img src="doc-files/SoftBevelBorder-1.png" width="500" height="200"
  77:    * alt="[An illustration showing raised and lowered SoftBevelBorders]" />
  78:    *
  79:    * @param bevelType the desired appearance of the border. The value
  80:    *        must be either {@link BevelBorder#RAISED}
  81:    *        or {@link BevelBorder#LOWERED}.
  82:    *
  83:    * @throws IllegalArgumentException if <code>bevelType</code> has
  84:    *         an unsupported value.
  85:    */
  86:   public SoftBevelBorder(int bevelType)
  87:   {
  88:     super(bevelType);
  89:   }
  90: 
  91: 
  92:   /**
  93:    * Constructs a SoftBevelBorder given its appearance type and two
  94:    * colors for its highlight and shadow.
  95:    *
  96:    * <p><img src="doc-files/SoftBevelBorder-2.png" width="500" height="150"
  97:    * alt="[An illustration showing SoftBevelBorders that were
  98:    * constructed with this method]" />
  99:    *
 100:    * @param bevelType the desired appearance of the border. The value
 101:    *        must be either {@link BevelBorder#RAISED} or {@link
 102:    *        BevelBorder#LOWERED}.
 103:    *
 104:    * @param highlight the color that will be used for the inner side
 105:    *        of the highlighted edges (top and left if if
 106:    *        <code>bevelType</code> is {@link BevelBorder#RAISED};
 107:    *        bottom and right otherwise). The color for the outer side
 108:    *        is a brightened version of this color.
 109:    *
 110:    * @param shadow the color that will be used for the outer side of
 111:    *        the shadowed edges (bottom and right if
 112:    *        <code>bevelType</code> is {@link BevelBorder#RAISED}; top
 113:    *        and left otherwise). The color for the inner side is a
 114:    *        brightened version of this color.
 115:    *
 116:    * @throws IllegalArgumentException if <code>bevelType</code> has an
 117:    *        unsupported value.
 118:    *
 119:    * @throws NullPointerException if <code>highlight</code> or
 120:    *         <code>shadow</code> is <code>null</code>.
 121:    *
 122:    * @see java.awt.Color#brighter()
 123:    */
 124:   public SoftBevelBorder(int bevelType, Color highlight, Color shadow)
 125:   {
 126:     this(bevelType,
 127:          /* highlightOuter */ highlight.brighter(),
 128:          /* highlightInner */ highlight,
 129:          /* shadowOuter */    shadow,
 130:          /* shadowInner */    shadow.brighter());
 131:   }
 132: 
 133: 
 134:   /**
 135:    * Constructs a SoftBevelBorder given its appearance type and all
 136:    * colors.
 137:    *
 138:    * <p><img src="doc-files/SoftBevelBorder-3.png" width="500" height="150"
 139:    * alt="[An illustration showing SoftBevelBorders that were
 140:    * constructed with this method]" />
 141:    *
 142:    * @param bevelType the desired appearance of the border. The value
 143:    *        must be either {@link BevelBorder#RAISED} or {@link
 144:    *        BevelBorder#LOWERED}.
 145:    *
 146:    * @param highlightOuter the color that will be used for the outer
 147:    *        side of the highlighted edges (top and left if
 148:    *        <code>bevelType</code> is {@link BevelBorder#RAISED};
 149:    *        bottom and right otherwise).
 150:    *
 151:    * @param highlightInner the color that will be used for the inner
 152:    *        side of the highlighted edges.
 153:    *
 154:    * @param shadowOuter the color that will be used for the outer side
 155:    *        of the shadowed edges (bottom and right if
 156:    *        <code>bevelType</code> is {@link BevelBorder#RAISED}; top
 157:    *        and left otherwise).
 158:    *
 159:    * @param shadowInner the color that will be used for the inner
 160:    *        side of the shadowed edges.
 161:    *
 162:    * @throws IllegalArgumentException if <code>bevelType</code> has
 163:    *         an unsupported value.
 164:    *
 165:    * @throws NullPointerException if one of the passed colors
 166:    *         is <code>null</code>.
 167:    */
 168:   public SoftBevelBorder(int bevelType,
 169:                          Color highlightOuter, Color highlightInner,
 170:                          Color shadowOuter, Color shadowInner)
 171:   {
 172:     super(bevelType,
 173:           highlightOuter, highlightInner,
 174:           shadowOuter, shadowInner);
 175:   }
 176: 
 177: 
 178:   /**
 179:    * Paints the border for a given component.
 180:    *
 181:    * @param c the component whose border is to be painted.
 182:    * @param g the graphics for painting.
 183:    * @param x the horizontal position for painting the border.
 184:    * @param y the vertical position for painting the border.
 185:    * @param width the width of the available area for painting the border.
 186:    * @param height the height of the available area for painting the border.
 187:    */
 188:   public void paintBorder(Component c, Graphics  g,
 189:                           int x, int y, int width, int height)
 190:   {
 191:     switch (bevelType)
 192:     {
 193:     case RAISED:
 194:       paintSoftBevel(g, x, y, width, height,
 195:                      getHighlightOuterColor(c), getHighlightInnerColor(c),
 196:                      getShadowInnerColor(c), getShadowOuterColor(c));
 197:       break;
 198: 
 199:     case LOWERED:
 200:       paintSoftBevel(g, x, y, width, height,
 201:                      getShadowOuterColor(c), getShadowInnerColor(c),
 202:                      getHighlightInnerColor(c), getHighlightOuterColor(c));
 203:       break;
 204:     }
 205:   }
 206: 
 207: 
 208:   /**
 209:    * Measures the width of this border.
 210:    *
 211:    * @param c the component whose border is to be measured.
 212:    *
 213:    * @return an Insets object whose <code>left</code>, <code>right</code>,
 214:    *         <code>top</code> and <code>bottom</code> fields indicate the
 215:    *         width of the border at the respective edge.
 216:    *
 217:    * @see #getBorderInsets(java.awt.Component, java.awt.Insets)
 218:    */
 219:   public Insets getBorderInsets(Component c)
 220:   {
 221:     return new Insets(3, 3, 3, 3);
 222:   }
 223: 
 224: 
 225:   /**
 226:    * Measures the width of this border, storing the results into a
 227:    * pre-existing Insets object.
 228:    *
 229:    * @param insets an Insets object for holding the result values.
 230:    *        After invoking this method, the <code>left</code>,
 231:    *        <code>right</code>, <code>top</code> and
 232:    *        <code>bottom</code> fields indicate the width of the
 233:    *        border at the respective edge.
 234:    *
 235:    * @return the same object that was passed for <code>insets</code>.
 236:    *
 237:    * @see #getBorderInsets(Component)
 238:    */
 239:   public Insets getBorderInsets(Component c, Insets insets)
 240:   {
 241:     insets.left = insets.right = insets.top = insets.bottom = 3;
 242:     return insets;
 243:   }
 244: 
 245: 
 246:   /**
 247:    * Determines whether this border fills every pixel in its area
 248:    * when painting.
 249:    *
 250:    * <p>The enlarged view (see documentation for constructors) shows
 251:    * that a SoftBevelBorder does not paint all pixels. Therefore,
 252:    * this method always returns <code>false</code>.
 253:    *
 254:    * @return <code>false</code>.
 255:    */
 256:   public boolean isBorderOpaque()
 257:   {
 258:     return false;
 259:   }
 260: 
 261: 
 262:   /**
 263:    * Paints a soft bevel in four colors.
 264:    *
 265:    * <pre>
 266:    * +++++++++++.
 267:    * ++.........#    + = color a
 268:    * +..        #    . = color b
 269:    * +.         #    X = color c
 270:    * ..        X#    # = color d
 271:    * . ##########</pre>
 272:    *
 273:    * @param g the graphics for painting.
 274:    * @param x the horizontal position for painting the border.
 275:    * @param y the vertical position for painting the border.
 276:    * @param width the width of the available area for painting the border.
 277:    * @param height the height of the available area for painting the border.
 278:    * @param a the color for the outer side of the top and left edges.
 279:    * @param b the color for the inner side of the top and left edges.
 280:    * @param c the color for the inner side of the bottom and right edges.
 281:    * @param d the color for the outer side of the bottom and right edges.
 282:    */
 283:   private static void paintSoftBevel(Graphics g,
 284:                                      int x, int y, int width, int height,
 285:                                      Color a, Color b, Color c, Color d)
 286:   {
 287:     Color oldColor;
 288: 
 289:     oldColor = g.getColor();
 290:     g.translate(x, y);
 291:     width = width - 1;
 292:     height = height - 1;
 293: 
 294:     try
 295:     {
 296:       /* To understand this code, it might be helpful to look at the
 297:        * images that are included with the JavaDoc, especially
 298:        * SoftBevelBorder-3.png. They are located in the "doc-files"
 299:        * subdirectory.
 300:        */
 301:       g.setColor(a);
 302:       g.drawLine(0, 0, width - 1, 0);                   // a, horizontal
 303:       g.drawLine(0, 1, 2, 1);                           // a, horizontal
 304:       g.drawLine(0, 2, 0, height - 1);                  // a, vertical
 305: 
 306:       g.setColor(b);
 307:       g.drawLine(width, 0, width, 0);                   // b, horizontal
 308:       g.drawLine(2, 1, width - 1, 1);                   // b, horizontal
 309:       g.drawLine(1, 2, 2, 2);                           // b, horizontal
 310:       g.drawLine(1, 3, 1, height - 1);                  // b, vertical
 311:       g.drawLine(0, height - 1, 0, height);             // b, vertical
 312: 
 313:       g.setColor(c);
 314:       g.drawLine(width - 1, height - 1,                 // c, one pixel
 315:                  width - 1, height - 1);
 316: 
 317:       g.setColor(d);
 318:       g.drawLine(2, height, width, height);             // d, horizontal
 319:       g.drawLine(width, 2, width, height - 1);          // d, vertical
 320:     }
 321:     finally
 322:     {
 323:       g.translate(-x, -y);
 324:       g.setColor(oldColor);
 325:     }
 326:   }
 327: }