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.Rectangle;
  30 import java.awt.Color;
  31 import java.awt.Component;
  32 import java.beans.ConstructorProperties;
  33 
  34 /**
  35  * A class which implements a raised or lowered bevel with
  36  * softened corners.
  37  * <p>
  38  * <strong>Warning:</strong>
  39  * Serialized objects of this class will not be compatible with
  40  * future Swing releases. The current serialization support is
  41  * appropriate for short term storage or RMI between applications running
  42  * the same version of Swing.  As of 1.4, support for long term storage
  43  * of all JavaBeans
  44  * has been added to the <code>java.beans</code> package.
  45  * Please see {@link java.beans.XMLEncoder}.
  46  *
  47  * @author Amy Fowler
  48  * @author Chester Rose
  49  */
  50 @SuppressWarnings("serial") // Same-version serialization only
  51 public class SoftBevelBorder extends BevelBorder
  52 {
  53 
  54     /**
  55      * Creates a bevel border with the specified type and whose
  56      * colors will be derived from the background color of the
  57      * component passed into the paintBorder method.
  58      * @param bevelType the type of bevel for the border
  59      */
  60     public SoftBevelBorder(int bevelType) {
  61         super(bevelType);
  62     }
  63 
  64     /**
  65      * Creates a bevel border with the specified type, highlight and
  66      * shadow colors.
  67      * @param bevelType the type of bevel for the border
  68      * @param highlight the color to use for the bevel highlight
  69      * @param shadow the color to use for the bevel shadow
  70      */
  71     public SoftBevelBorder(int bevelType, Color highlight, Color shadow) {
  72         super(bevelType, highlight, shadow);
  73     }
  74 
  75     /**
  76      * Creates a bevel border with the specified type, highlight
  77      * shadow colors.
  78      * @param bevelType the type of bevel for the border
  79      * @param highlightOuterColor the color to use for the bevel outer highlight
  80      * @param highlightInnerColor the color to use for the bevel inner highlight
  81      * @param shadowOuterColor the color to use for the bevel outer shadow
  82      * @param shadowInnerColor the color to use for the bevel inner shadow
  83      */
  84     @ConstructorProperties({"bevelType", "highlightOuterColor", "highlightInnerColor", "shadowOuterColor", "shadowInnerColor"})
  85     public SoftBevelBorder(int bevelType, Color highlightOuterColor,
  86                         Color highlightInnerColor, Color shadowOuterColor,
  87                         Color shadowInnerColor) {
  88         super(bevelType, highlightOuterColor, highlightInnerColor,
  89               shadowOuterColor, shadowInnerColor);
  90     }
  91 
  92     /**
  93      * Paints the border for the specified component with the specified
  94      * position and size.
  95      * @param c the component for which this border is being painted
  96      * @param g the paint graphics
  97      * @param x the x position of the painted border
  98      * @param y the y position of the painted border
  99      * @param width the width of the painted border
 100      * @param height the height of the painted border
 101      */
 102     public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
 103         Color oldColor = g.getColor();
 104         g.translate(x, y);
 105 
 106         if (bevelType == RAISED) {
 107             g.setColor(getHighlightOuterColor(c));
 108             g.drawLine(0, 0, width-2, 0);
 109             g.drawLine(0, 0, 0, height-2);
 110             g.drawLine(1, 1, 1, 1);
 111 
 112             g.setColor(getHighlightInnerColor(c));
 113             g.drawLine(2, 1, width-2, 1);
 114             g.drawLine(1, 2, 1, height-2);
 115             g.drawLine(2, 2, 2, 2);
 116             g.drawLine(0, height-1, 0, height-2);
 117             g.drawLine(width-1, 0, width-1, 0);
 118 
 119             g.setColor(getShadowOuterColor(c));
 120             g.drawLine(2, height-1, width-1, height-1);
 121             g.drawLine(width-1, 2, width-1, height-1);
 122 
 123             g.setColor(getShadowInnerColor(c));
 124             g.drawLine(width-2, height-2, width-2, height-2);
 125 
 126 
 127         } else if (bevelType == LOWERED) {
 128             g.setColor(getShadowOuterColor(c));
 129             g.drawLine(0, 0, width-2, 0);
 130             g.drawLine(0, 0, 0, height-2);
 131             g.drawLine(1, 1, 1, 1);
 132 
 133             g.setColor(getShadowInnerColor(c));
 134             g.drawLine(2, 1, width-2, 1);
 135             g.drawLine(1, 2, 1, height-2);
 136             g.drawLine(2, 2, 2, 2);
 137             g.drawLine(0, height-1, 0, height-2);
 138             g.drawLine(width-1, 0, width-1, 0);
 139 
 140             g.setColor(getHighlightOuterColor(c));
 141             g.drawLine(2, height-1, width-1, height-1);
 142             g.drawLine(width-1, 2, width-1, height-1);
 143 
 144             g.setColor(getHighlightInnerColor(c));
 145             g.drawLine(width-2, height-2, width-2, height-2);
 146         }
 147         g.translate(-x, -y);
 148         g.setColor(oldColor);
 149     }
 150 
 151     /**
 152      * Reinitialize the insets parameter with this Border's current Insets.
 153      * @param c the component for which this border insets value applies
 154      * @param insets the object to be reinitialized
 155      */
 156     public Insets getBorderInsets(Component c, Insets insets)       {
 157         insets.set(3, 3, 3, 3);
 158         return insets;
 159     }
 160 
 161     /**
 162      * Returns whether or not the border is opaque.
 163      */
 164     public boolean isBorderOpaque() { return false; }
 165 
 166 }