J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 1997-2007 Sun Microsystems, Inc. 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. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any 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 simple etched border which can |
| 36 | * either be etched-in or etched-out. If no highlight/shadow |
| 37 | * colors are initialized when the border is created, then |
| 38 | * these colors will be dynamically derived from the background |
| 39 | * color of the component argument passed into the paintBorder() |
| 40 | * method. |
| 41 | * <p> |
| 42 | * <strong>Warning:</strong> |
| 43 | * Serialized objects of this class will not be compatible with |
| 44 | * future Swing releases. The current serialization support is |
| 45 | * appropriate for short term storage or RMI between applications running |
| 46 | * the same version of Swing. As of 1.4, support for long term storage |
| 47 | * of all JavaBeans<sup><font size="-2">TM</font></sup> |
| 48 | * has been added to the <code>java.beans</code> package. |
| 49 | * Please see {@link java.beans.XMLEncoder}. |
| 50 | * |
| 51 | * @author David Kloba |
| 52 | * @author Amy Fowler |
| 53 | */ |
| 54 | public class EtchedBorder extends AbstractBorder |
| 55 | { |
| 56 | /** Raised etched type. */ |
| 57 | public static final int RAISED = 0; |
| 58 | /** Lowered etched type. */ |
| 59 | public static final int LOWERED = 1; |
| 60 | |
| 61 | protected int etchType; |
| 62 | protected Color highlight; |
| 63 | protected Color shadow; |
| 64 | |
| 65 | /** |
| 66 | * Creates a lowered etched border whose colors will be derived |
| 67 | * from the background color of the component passed into |
| 68 | * the paintBorder method. |
| 69 | */ |
| 70 | public EtchedBorder() { |
| 71 | this(LOWERED); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Creates an etched border with the specified etch-type |
| 76 | * whose colors will be derived |
| 77 | * from the background color of the component passed into |
| 78 | * the paintBorder method. |
| 79 | * @param etchType the type of etch to be drawn by the border |
| 80 | */ |
| 81 | public EtchedBorder(int etchType) { |
| 82 | this(etchType, null, null); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Creates a lowered etched border with the specified highlight and |
| 87 | * shadow colors. |
| 88 | * @param highlight the color to use for the etched highlight |
| 89 | * @param shadow the color to use for the etched shadow |
| 90 | */ |
| 91 | public EtchedBorder(Color highlight, Color shadow) { |
| 92 | this(LOWERED, highlight, shadow); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Creates an etched border with the specified etch-type, |
| 97 | * highlight and shadow colors. |
| 98 | * @param etchType the type of etch to be drawn by the border |
| 99 | * @param highlight the color to use for the etched highlight |
| 100 | * @param shadow the color to use for the etched shadow |
| 101 | */ |
| 102 | @ConstructorProperties({"etchType", "highlightColor", "shadowColor"}) |
| 103 | public EtchedBorder(int etchType, Color highlight, Color shadow) { |
| 104 | this.etchType = etchType; |
| 105 | this.highlight = highlight; |
| 106 | this.shadow = shadow; |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Paints the border for the specified component with the |
| 111 | * specified position and size. |
| 112 | * @param c the component for which this border is being painted |
| 113 | * @param g the paint graphics |
| 114 | * @param x the x position of the painted border |
| 115 | * @param y the y position of the painted border |
| 116 | * @param width the width of the painted border |
| 117 | * @param height the height of the painted border |
| 118 | */ |
| 119 | public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) { |
| 120 | int w = width; |
| 121 | int h = height; |
| 122 | |
| 123 | g.translate(x, y); |
| 124 | |
| 125 | g.setColor(etchType == LOWERED? getShadowColor(c) : getHighlightColor(c)); |
| 126 | g.drawRect(0, 0, w-2, h-2); |
| 127 | |
| 128 | g.setColor(etchType == LOWERED? getHighlightColor(c) : getShadowColor(c)); |
| 129 | g.drawLine(1, h-3, 1, 1); |
| 130 | g.drawLine(1, 1, w-3, 1); |
| 131 | |
| 132 | g.drawLine(0, h-1, w-1, h-1); |
| 133 | g.drawLine(w-1, h-1, w-1, 0); |
| 134 | |
| 135 | g.translate(-x, -y); |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Reinitialize the insets parameter with this Border's current Insets. |
| 140 | * @param c the component for which this border insets value applies |
| 141 | * @param insets the object to be reinitialized |
| 142 | */ |
| 143 | public Insets getBorderInsets(Component c, Insets insets) { |
| 144 | insets.set(2, 2, 2, 2); |
| 145 | return insets; |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Returns whether or not the border is opaque. |
| 150 | */ |
| 151 | public boolean isBorderOpaque() { return true; } |
| 152 | |
| 153 | /** |
| 154 | * Returns which etch-type is set on the etched border. |
| 155 | */ |
| 156 | public int getEtchType() { |
| 157 | return etchType; |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Returns the highlight color of the etched border |
| 162 | * when rendered on the specified component. If no highlight |
| 163 | * color was specified at instantiation, the highlight color |
| 164 | * is derived from the specified component's background color. |
| 165 | * @param c the component for which the highlight may be derived |
| 166 | * @since 1.3 |
| 167 | */ |
| 168 | public Color getHighlightColor(Component c) { |
| 169 | return highlight != null? highlight : |
| 170 | c.getBackground().brighter(); |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Returns the highlight color of the etched border. |
| 175 | * Will return null if no highlight color was specified |
| 176 | * at instantiation. |
| 177 | * @since 1.3 |
| 178 | */ |
| 179 | public Color getHighlightColor() { |
| 180 | return highlight; |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * Returns the shadow color of the etched border |
| 185 | * when rendered on the specified component. If no shadow |
| 186 | * color was specified at instantiation, the shadow color |
| 187 | * is derived from the specified component's background color. |
| 188 | * @param c the component for which the shadow may be derived |
| 189 | * @since 1.3 |
| 190 | */ |
| 191 | public Color getShadowColor(Component c) { |
| 192 | return shadow != null? shadow : c.getBackground().darker(); |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * Returns the shadow color of the etched border. |
| 197 | * Will return null if no shadow color was specified |
| 198 | * at instantiation. |
| 199 | * @since 1.3 |
| 200 | */ |
| 201 | public Color getShadowColor() { |
| 202 | return shadow; |
| 203 | } |
| 204 | |
| 205 | } |