1 /*
   2  * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package javax.swing.border;
  26 
  27 import java.awt.Graphics;
  28 import java.awt.Insets;
  29 import java.awt.Color;
  30 import java.awt.Component;
  31 import java.beans.ConstructorProperties;
  32 
  33 /**
  34  * A class which implements a simple two-line bevel border.
  35  * <p>
  36  * <strong>Warning:</strong>
  37  * Serialized objects of this class will not be compatible with
  38  * future Swing releases. The current serialization support is
  39  * appropriate for short term storage or RMI between applications running
  40  * the same version of Swing.  As of 1.4, support for long term storage
  41  * of all JavaBeans&trade;
  42  * has been added to the <code>java.beans</code> package.
  43  * Please see {@link java.beans.XMLEncoder}.
  44  *
  45  * @author David Kloba
  46  */
  47 @SuppressWarnings("serial") // Same-version serialization only
  48 public class BevelBorder extends AbstractBorder
  49 {
  50     /** Raised bevel type. */
  51     public static final int RAISED  = 0;
  52     /** Lowered bevel type. */
  53     public static final int LOWERED = 1;
  54 
  55     protected int bevelType;
  56     protected Color highlightOuter;
  57     protected Color highlightInner;
  58     protected Color shadowInner;
  59     protected Color shadowOuter;
  60 
  61     /**
  62      * Creates a bevel border with the specified type and whose
  63      * colors will be derived from the background color of the
  64      * component passed into the paintBorder method.
  65      * @param bevelType the type of bevel for the border
  66      */
  67     public BevelBorder(int bevelType) {
  68         this.bevelType = bevelType;
  69     }
  70 
  71     /**
  72      * Creates a bevel border with the specified type, highlight and
  73      * shadow colors.
  74      * @param bevelType the type of bevel for the border
  75      * @param highlight the color to use for the bevel highlight
  76      * @param shadow the color to use for the bevel shadow
  77      */
  78     public BevelBorder(int bevelType, Color highlight, Color shadow) {
  79         this(bevelType, highlight.brighter(), highlight, shadow, shadow.brighter());
  80     }
  81 
  82     /**
  83      * Creates a bevel border with the specified type, highlight and
  84      * shadow colors.
  85      *
  86      * @param bevelType the type of bevel for the border
  87      * @param highlightOuterColor the color to use for the bevel outer highlight
  88      * @param highlightInnerColor the color to use for the bevel inner highlight
  89      * @param shadowOuterColor the color to use for the bevel outer shadow
  90      * @param shadowInnerColor the color to use for the bevel inner shadow
  91      */
  92     @ConstructorProperties({"bevelType", "highlightOuterColor", "highlightInnerColor", "shadowOuterColor", "shadowInnerColor"})
  93     public BevelBorder(int bevelType, Color highlightOuterColor,
  94                        Color highlightInnerColor, Color shadowOuterColor,
  95                        Color shadowInnerColor) {
  96         this(bevelType);
  97         this.highlightOuter = highlightOuterColor;
  98         this.highlightInner = highlightInnerColor;
  99         this.shadowOuter = shadowOuterColor;
 100         this.shadowInner = shadowInnerColor;
 101     }
 102 
 103     /**
 104      * Paints the border for the specified component with the specified
 105      * position and size.
 106      * @param c the component for which this border is being painted
 107      * @param g the paint graphics
 108      * @param x the x position of the painted border
 109      * @param y the y position of the painted border
 110      * @param width the width of the painted border
 111      * @param height the height of the painted border
 112      */
 113     public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
 114         if (bevelType == RAISED) {
 115              paintRaisedBevel(c, g, x, y, width, height);
 116 
 117         } else if (bevelType == LOWERED) {
 118              paintLoweredBevel(c, g, x, y, width, height);
 119         }
 120     }
 121 
 122     /**
 123      * Reinitialize the insets parameter with this Border's current Insets.
 124      * @param c the component for which this border insets value applies
 125      * @param insets the object to be reinitialized
 126      */
 127     public Insets getBorderInsets(Component c, Insets insets) {
 128         insets.set(2, 2, 2, 2);
 129         return insets;
 130     }
 131 
 132     /**
 133      * Returns the outer highlight color of the bevel border
 134      * when rendered on the specified component.  If no highlight
 135      * color was specified at instantiation, the highlight color
 136      * is derived from the specified component's background color.
 137      *
 138      * @param c the component for which the highlight may be derived
 139      * @return the outer highlight {@code Color}
 140      * @since 1.3
 141      */
 142     public Color getHighlightOuterColor(Component c)   {
 143         Color highlight = getHighlightOuterColor();
 144         return highlight != null? highlight :
 145                                        c.getBackground().brighter().brighter();
 146     }
 147 
 148     /**
 149      * Returns the inner highlight color of the bevel border
 150      * when rendered on the specified component.  If no highlight
 151      * color was specified at instantiation, the highlight color
 152      * is derived from the specified component's background color.
 153      *
 154      * @param c the component for which the highlight may be derived
 155      * @return the inner highlight {@code Color}
 156      * @since 1.3
 157      */
 158     public Color getHighlightInnerColor(Component c)   {
 159         Color highlight = getHighlightInnerColor();
 160         return highlight != null? highlight :
 161                                        c.getBackground().brighter();
 162     }
 163 
 164     /**
 165      * Returns the inner shadow color of the bevel border
 166      * when rendered on the specified component.  If no shadow
 167      * color was specified at instantiation, the shadow color
 168      * is derived from the specified component's background color.
 169      *
 170      * @param c the component for which the shadow may be derived
 171      * @return the inner shadow {@code Color}
 172      * @since 1.3
 173      */
 174     public Color getShadowInnerColor(Component c)      {
 175         Color shadow = getShadowInnerColor();
 176         return shadow != null? shadow :
 177                                     c.getBackground().darker();
 178     }
 179 
 180     /**
 181      * Returns the outer shadow color of the bevel border
 182      * when rendered on the specified component.  If no shadow
 183      * color was specified at instantiation, the shadow color
 184      * is derived from the specified component's background color.
 185      *
 186      * @param c the component for which the shadow may be derived
 187      * @return the outer shadow {@code Color}
 188      * @since 1.3
 189      */
 190     public Color getShadowOuterColor(Component c)      {
 191         Color shadow = getShadowOuterColor();
 192         return shadow != null? shadow :
 193                                     c.getBackground().darker().darker();
 194     }
 195 
 196     /**
 197      * Returns the outer highlight color of the bevel border.
 198      * Will return null if no highlight color was specified
 199      * at instantiation.
 200      *
 201      * @return the outer highlight {@code Color} or {@code null} if no highlight
 202      *         color was specified
 203      * @since 1.3
 204      */
 205     public Color getHighlightOuterColor()   {
 206         return highlightOuter;
 207     }
 208 
 209     /**
 210      * Returns the inner highlight color of the bevel border.
 211      * Will return null if no highlight color was specified
 212      * at instantiation.
 213      *
 214      * @return the inner highlight {@code Color} or {@code null} if no highlight
 215      *         color was specified
 216      * @since 1.3
 217      */
 218     public Color getHighlightInnerColor()   {
 219         return highlightInner;
 220     }
 221 
 222     /**
 223      * Returns the inner shadow color of the bevel border.
 224      * Will return null if no shadow color was specified
 225      * at instantiation.
 226      *
 227      * @return the inner shadow {@code Color} or {@code null} if no shadow color
 228      *         was specified
 229      * @since 1.3
 230      */
 231     public Color getShadowInnerColor()      {
 232         return shadowInner;
 233     }
 234 
 235     /**
 236      * Returns the outer shadow color of the bevel border.
 237      * Will return null if no shadow color was specified
 238      * at instantiation.
 239      *
 240      * @return the outer shadow {@code Color} or {@code null} if no shadow color
 241      *         was specified
 242      * @since 1.3
 243      */
 244     public Color getShadowOuterColor()      {
 245         return shadowOuter;
 246     }
 247 
 248     /**
 249      * Returns the type of the bevel border.
 250      *
 251      * @return the bevel border type, either {@code RAISED} or {@code LOWERED}
 252      */
 253     public int getBevelType()       {
 254         return bevelType;
 255     }
 256 
 257     /**
 258      * Returns whether or not the border is opaque. This implementation
 259      * returns {@code true}.
 260      *
 261      * @return true
 262      */
 263     public boolean isBorderOpaque() { return true; }
 264 
 265     protected void paintRaisedBevel(Component c, Graphics g, int x, int y,
 266                                     int width, int height)  {
 267         Color oldColor = g.getColor();
 268         int h = height;
 269         int w = width;
 270 
 271         g.translate(x, y);
 272 
 273         g.setColor(getHighlightOuterColor(c));
 274         g.drawLine(0, 0, 0, h-2);
 275         g.drawLine(1, 0, w-2, 0);
 276 
 277         g.setColor(getHighlightInnerColor(c));
 278         g.drawLine(1, 1, 1, h-3);
 279         g.drawLine(2, 1, w-3, 1);
 280 
 281         g.setColor(getShadowOuterColor(c));
 282         g.drawLine(0, h-1, w-1, h-1);
 283         g.drawLine(w-1, 0, w-1, h-2);
 284 
 285         g.setColor(getShadowInnerColor(c));
 286         g.drawLine(1, h-2, w-2, h-2);
 287         g.drawLine(w-2, 1, w-2, h-3);
 288 
 289         g.translate(-x, -y);
 290         g.setColor(oldColor);
 291 
 292     }
 293 
 294     protected void paintLoweredBevel(Component c, Graphics g, int x, int y,
 295                                         int width, int height)  {
 296         Color oldColor = g.getColor();
 297         int h = height;
 298         int w = width;
 299 
 300         g.translate(x, y);
 301 
 302         g.setColor(getShadowInnerColor(c));
 303         g.drawLine(0, 0, 0, h-1);
 304         g.drawLine(1, 0, w-1, 0);
 305 
 306         g.setColor(getShadowOuterColor(c));
 307         g.drawLine(1, 1, 1, h-2);
 308         g.drawLine(2, 1, w-2, 1);
 309 
 310         g.setColor(getHighlightOuterColor(c));
 311         g.drawLine(1, h-1, w-1, h-1);
 312         g.drawLine(w-1, 1, w-1, h-2);
 313 
 314         g.setColor(getHighlightInnerColor(c));
 315         g.drawLine(2, h-2, w-2, h-2);
 316         g.drawLine(w-2, 2, w-2, h-3);
 317 
 318         g.translate(-x, -y);
 319         g.setColor(oldColor);
 320 
 321     }
 322 
 323 }