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 two-line bevel border. |
| 36 | * <p> |
| 37 | * <strong>Warning:</strong> |
| 38 | * Serialized objects of this class will not be compatible with |
| 39 | * future Swing releases. The current serialization support is |
| 40 | * appropriate for short term storage or RMI between applications running |
| 41 | * the same version of Swing. As of 1.4, support for long term storage |
| 42 | * of all JavaBeans<sup><font size="-2">TM</font></sup> |
| 43 | * has been added to the <code>java.beans</code> package. |
| 44 | * Please see {@link java.beans.XMLEncoder}. |
| 45 | * |
| 46 | * @author David Kloba |
| 47 | */ |
| 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 | * <p> |
| 86 | * Note: The shadow inner and outer colors are |
| 87 | * switched for a lowered bevel border. |
| 88 | * |
| 89 | * @param bevelType the type of bevel for the border |
| 90 | * @param highlightOuterColor the color to use for the bevel outer highlight |
| 91 | * @param highlightInnerColor the color to use for the bevel inner highlight |
| 92 | * @param shadowOuterColor the color to use for the bevel outer shadow |
| 93 | * @param shadowInnerColor the color to use for the bevel inner shadow |
| 94 | */ |
| 95 | @ConstructorProperties({"bevelType", "highlightOuterColor", "highlightInnerColor", "shadowOuterColor", "shadowInnerColor"}) |
| 96 | public BevelBorder(int bevelType, Color highlightOuterColor, |
| 97 | Color highlightInnerColor, Color shadowOuterColor, |
| 98 | Color shadowInnerColor) { |
| 99 | this(bevelType); |
| 100 | this.highlightOuter = highlightOuterColor; |
| 101 | this.highlightInner = highlightInnerColor; |
| 102 | this.shadowOuter = shadowOuterColor; |
| 103 | this.shadowInner = shadowInnerColor; |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Paints the border for the specified component with the specified |
| 108 | * position and size. |
| 109 | * @param c the component for which this border is being painted |
| 110 | * @param g the paint graphics |
| 111 | * @param x the x position of the painted border |
| 112 | * @param y the y position of the painted border |
| 113 | * @param width the width of the painted border |
| 114 | * @param height the height of the painted border |
| 115 | */ |
| 116 | public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) { |
| 117 | if (bevelType == RAISED) { |
| 118 | paintRaisedBevel(c, g, x, y, width, height); |
| 119 | |
| 120 | } else if (bevelType == LOWERED) { |
| 121 | paintLoweredBevel(c, g, x, y, width, height); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Reinitialize the insets parameter with this Border's current Insets. |
| 127 | * @param c the component for which this border insets value applies |
| 128 | * @param insets the object to be reinitialized |
| 129 | */ |
| 130 | public Insets getBorderInsets(Component c, Insets insets) { |
| 131 | insets.set(2, 2, 2, 2); |
| 132 | return insets; |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Returns the outer highlight color of the bevel border |
| 137 | * when rendered on the specified component. If no highlight |
| 138 | * color was specified at instantiation, the highlight color |
| 139 | * is derived from the specified component's background color. |
| 140 | * @param c the component for which the highlight may be derived |
| 141 | * @since 1.3 |
| 142 | */ |
| 143 | public Color getHighlightOuterColor(Component c) { |
| 144 | Color highlight = getHighlightOuterColor(); |
| 145 | return highlight != null? highlight : |
| 146 | c.getBackground().brighter().brighter(); |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Returns the inner highlight color of the bevel border |
| 151 | * when rendered on the specified component. If no highlight |
| 152 | * color was specified at instantiation, the highlight color |
| 153 | * is derived from the specified component's background color. |
| 154 | * @param c the component for which the highlight may be derived |
| 155 | * @since 1.3 |
| 156 | */ |
| 157 | public Color getHighlightInnerColor(Component c) { |
| 158 | Color highlight = getHighlightInnerColor(); |
| 159 | return highlight != null? highlight : |
| 160 | c.getBackground().brighter(); |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Returns the inner shadow color of the bevel border |
| 165 | * when rendered on the specified component. If no shadow |
| 166 | * color was specified at instantiation, the shadow color |
| 167 | * is derived from the specified component's background color. |
| 168 | * @param c the component for which the shadow may be derived |
| 169 | * @since 1.3 |
| 170 | */ |
| 171 | public Color getShadowInnerColor(Component c) { |
| 172 | Color shadow = getShadowInnerColor(); |
| 173 | return shadow != null? shadow : |
| 174 | c.getBackground().darker(); |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Returns the outer shadow color of the bevel border |
| 179 | * when rendered on the specified component. If no shadow |
| 180 | * color was specified at instantiation, the shadow color |
| 181 | * is derived from the specified component's background color. |
| 182 | * @param c the component for which the shadow may be derived |
| 183 | * @since 1.3 |
| 184 | */ |
| 185 | public Color getShadowOuterColor(Component c) { |
| 186 | Color shadow = getShadowOuterColor(); |
| 187 | return shadow != null? shadow : |
| 188 | c.getBackground().darker().darker(); |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * Returns the outer highlight color of the bevel border. |
| 193 | * Will return null if no highlight color was specified |
| 194 | * at instantiation. |
| 195 | * @since 1.3 |
| 196 | */ |
| 197 | public Color getHighlightOuterColor() { |
| 198 | return highlightOuter; |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * Returns the inner highlight color of the bevel border. |
| 203 | * Will return null if no highlight color was specified |
| 204 | * at instantiation. |
| 205 | * @since 1.3 |
| 206 | */ |
| 207 | public Color getHighlightInnerColor() { |
| 208 | return highlightInner; |
| 209 | } |
| 210 | |
| 211 | /** |
| 212 | * Returns the inner shadow color of the bevel border. |
| 213 | * Will return null if no shadow color was specified |
| 214 | * at instantiation. |
| 215 | * @since 1.3 |
| 216 | */ |
| 217 | public Color getShadowInnerColor() { |
| 218 | return shadowInner; |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * Returns the outer shadow color of the bevel border. |
| 223 | * Will return null if no shadow color was specified |
| 224 | * at instantiation. |
| 225 | * @since 1.3 |
| 226 | */ |
| 227 | public Color getShadowOuterColor() { |
| 228 | return shadowOuter; |
| 229 | } |
| 230 | |
| 231 | /** |
| 232 | * Returns the type of the bevel border. |
| 233 | */ |
| 234 | public int getBevelType() { |
| 235 | return bevelType; |
| 236 | } |
| 237 | |
| 238 | /** |
| 239 | * Returns whether or not the border is opaque. |
| 240 | */ |
| 241 | public boolean isBorderOpaque() { return true; } |
| 242 | |
| 243 | protected void paintRaisedBevel(Component c, Graphics g, int x, int y, |
| 244 | int width, int height) { |
| 245 | Color oldColor = g.getColor(); |
| 246 | int h = height; |
| 247 | int w = width; |
| 248 | |
| 249 | g.translate(x, y); |
| 250 | |
| 251 | g.setColor(getHighlightOuterColor(c)); |
| 252 | g.drawLine(0, 0, 0, h-2); |
| 253 | g.drawLine(1, 0, w-2, 0); |
| 254 | |
| 255 | g.setColor(getHighlightInnerColor(c)); |
| 256 | g.drawLine(1, 1, 1, h-3); |
| 257 | g.drawLine(2, 1, w-3, 1); |
| 258 | |
| 259 | g.setColor(getShadowOuterColor(c)); |
| 260 | g.drawLine(0, h-1, w-1, h-1); |
| 261 | g.drawLine(w-1, 0, w-1, h-2); |
| 262 | |
| 263 | g.setColor(getShadowInnerColor(c)); |
| 264 | g.drawLine(1, h-2, w-2, h-2); |
| 265 | g.drawLine(w-2, 1, w-2, h-3); |
| 266 | |
| 267 | g.translate(-x, -y); |
| 268 | g.setColor(oldColor); |
| 269 | |
| 270 | } |
| 271 | |
| 272 | protected void paintLoweredBevel(Component c, Graphics g, int x, int y, |
| 273 | int width, int height) { |
| 274 | Color oldColor = g.getColor(); |
| 275 | int h = height; |
| 276 | int w = width; |
| 277 | |
| 278 | g.translate(x, y); |
| 279 | |
| 280 | g.setColor(getShadowInnerColor(c)); |
| 281 | g.drawLine(0, 0, 0, h-1); |
| 282 | g.drawLine(1, 0, w-1, 0); |
| 283 | |
| 284 | g.setColor(getShadowOuterColor(c)); |
| 285 | g.drawLine(1, 1, 1, h-2); |
| 286 | g.drawLine(2, 1, w-2, 1); |
| 287 | |
| 288 | g.setColor(getHighlightOuterColor(c)); |
| 289 | g.drawLine(1, h-1, w-1, h-1); |
| 290 | g.drawLine(w-1, 1, w-1, h-2); |
| 291 | |
| 292 | g.setColor(getHighlightInnerColor(c)); |
| 293 | g.drawLine(2, h-2, w-2, h-2); |
| 294 | g.drawLine(w-2, 2, w-2, h-3); |
| 295 | |
| 296 | g.translate(-x, -y); |
| 297 | g.setColor(oldColor); |
| 298 | |
| 299 | } |
| 300 | |
| 301 | } |