1 /* 2 * Copyright (c) 1997, 2015, 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™ 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 /** 56 * The bevel type. 57 */ 58 protected int bevelType; 59 /** 60 * The color to use for the bevel outer highlight. 61 */ 62 protected Color highlightOuter; 63 /** 64 * The color to use for the bevel inner highlight. 65 */ 66 protected Color highlightInner; 67 /** 68 * The color to use for the bevel inner shadow. 69 */ 70 protected Color shadowInner; 71 /** 72 * the color to use for the bevel outer shadow 73 */ 74 protected Color shadowOuter; 75 76 /** 77 * Creates a bevel border with the specified type and whose 78 * colors will be derived from the background color of the 79 * component passed into the paintBorder method. 80 * @param bevelType the type of bevel for the border 81 */ 82 public BevelBorder(int bevelType) { 83 this.bevelType = bevelType; 84 } 85 86 /** 87 * Creates a bevel border with the specified type, highlight and 88 * shadow colors. 89 * @param bevelType the type of bevel for the border 90 * @param highlight the color to use for the bevel highlight 91 * @param shadow the color to use for the bevel shadow 92 */ 93 public BevelBorder(int bevelType, Color highlight, Color shadow) { 94 this(bevelType, highlight.brighter(), highlight, shadow, shadow.brighter()); 95 } 96 97 /** 98 * Creates a bevel border with the specified type, highlight and 99 * shadow colors. 100 * 101 * @param bevelType the type of bevel for the border 102 * @param highlightOuterColor the color to use for the bevel outer highlight 103 * @param highlightInnerColor the color to use for the bevel inner highlight 104 * @param shadowOuterColor the color to use for the bevel outer shadow 105 * @param shadowInnerColor the color to use for the bevel inner shadow 106 */ 107 @ConstructorProperties({"bevelType", "highlightOuterColor", "highlightInnerColor", "shadowOuterColor", "shadowInnerColor"}) 108 public BevelBorder(int bevelType, Color highlightOuterColor, 109 Color highlightInnerColor, Color shadowOuterColor, 110 Color shadowInnerColor) { 111 this(bevelType); 112 this.highlightOuter = highlightOuterColor; 113 this.highlightInner = highlightInnerColor; 114 this.shadowOuter = shadowOuterColor; 115 this.shadowInner = shadowInnerColor; 116 } 117 118 /** 119 * Paints the border for the specified component with the specified 120 * position and size. 121 * @param c the component for which this border is being painted 122 * @param g the paint graphics 123 * @param x the x position of the painted border 124 * @param y the y position of the painted border 125 * @param width the width of the painted border 126 * @param height the height of the painted border 127 */ 128 public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) { 129 if (bevelType == RAISED) { 130 paintRaisedBevel(c, g, x, y, width, height); 131 132 } else if (bevelType == LOWERED) { 133 paintLoweredBevel(c, g, x, y, width, height); 134 } 135 } 136 137 /** 138 * Reinitialize the insets parameter with this Border's current Insets. 139 * @param c the component for which this border insets value applies 140 * @param insets the object to be reinitialized 141 */ 142 public Insets getBorderInsets(Component c, Insets insets) { 143 insets.set(2, 2, 2, 2); 144 return insets; 145 } 146 147 /** 148 * Returns the outer highlight color of the bevel border 149 * when rendered on the specified component. If no highlight 150 * color was specified at instantiation, the highlight color 151 * is derived from the specified component's background color. 152 * 153 * @param c the component for which the highlight may be derived 154 * @return the outer highlight {@code Color} 155 * @since 1.3 156 */ 157 public Color getHighlightOuterColor(Component c) { 158 Color highlight = getHighlightOuterColor(); 159 return highlight != null? highlight : 160 c.getBackground().brighter().brighter(); 161 } 162 163 /** 164 * Returns the inner highlight color of the bevel border 165 * when rendered on the specified component. If no highlight 166 * color was specified at instantiation, the highlight color 167 * is derived from the specified component's background color. 168 * 169 * @param c the component for which the highlight may be derived 170 * @return the inner highlight {@code Color} 171 * @since 1.3 172 */ 173 public Color getHighlightInnerColor(Component c) { 174 Color highlight = getHighlightInnerColor(); 175 return highlight != null? highlight : 176 c.getBackground().brighter(); 177 } 178 179 /** 180 * Returns the inner shadow color of the bevel border 181 * when rendered on the specified component. If no shadow 182 * color was specified at instantiation, the shadow color 183 * is derived from the specified component's background color. 184 * 185 * @param c the component for which the shadow may be derived 186 * @return the inner shadow {@code Color} 187 * @since 1.3 188 */ 189 public Color getShadowInnerColor(Component c) { 190 Color shadow = getShadowInnerColor(); 191 return shadow != null? shadow : 192 c.getBackground().darker(); 193 } 194 195 /** 196 * Returns the outer shadow color of the bevel border 197 * when rendered on the specified component. If no shadow 198 * color was specified at instantiation, the shadow color 199 * is derived from the specified component's background color. 200 * 201 * @param c the component for which the shadow may be derived 202 * @return the outer shadow {@code Color} 203 * @since 1.3 204 */ 205 public Color getShadowOuterColor(Component c) { 206 Color shadow = getShadowOuterColor(); 207 return shadow != null? shadow : 208 c.getBackground().darker().darker(); 209 } 210 211 /** 212 * Returns the outer highlight color of the bevel border. 213 * Will return null if no highlight color was specified 214 * at instantiation. 215 * 216 * @return the outer highlight {@code Color} or {@code null} if no highlight 217 * color was specified 218 * @since 1.3 219 */ 220 public Color getHighlightOuterColor() { 221 return highlightOuter; 222 } 223 224 /** 225 * Returns the inner highlight color of the bevel border. 226 * Will return null if no highlight color was specified 227 * at instantiation. 228 * 229 * @return the inner highlight {@code Color} or {@code null} if no highlight 230 * color was specified 231 * @since 1.3 232 */ 233 public Color getHighlightInnerColor() { 234 return highlightInner; 235 } 236 237 /** 238 * Returns the inner shadow color of the bevel border. 239 * Will return null if no shadow color was specified 240 * at instantiation. 241 * 242 * @return the inner shadow {@code Color} or {@code null} if no shadow color 243 * was specified 244 * @since 1.3 245 */ 246 public Color getShadowInnerColor() { 247 return shadowInner; 248 } 249 250 /** 251 * Returns the outer shadow color of the bevel border. 252 * Will return null if no shadow color was specified 253 * at instantiation. 254 * 255 * @return the outer shadow {@code Color} or {@code null} if no shadow color 256 * was specified 257 * @since 1.3 258 */ 259 public Color getShadowOuterColor() { 260 return shadowOuter; 261 } 262 263 /** 264 * Returns the type of the bevel border. 265 * 266 * @return the bevel border type, either {@code RAISED} or {@code LOWERED} 267 */ 268 public int getBevelType() { 269 return bevelType; 270 } 271 272 /** 273 * Returns whether or not the border is opaque. This implementation 274 * returns {@code true}. 275 * 276 * @return true 277 */ 278 public boolean isBorderOpaque() { return true; } 279 280 /** 281 * Paints a raised bevel for the specified component with the specified 282 * position and size. 283 * 284 * @param c the component for which the raised bevel is being painted 285 * @param g the paint graphics 286 * @param x the x position of the raised bevel 287 * @param y the y position of the raised bevel 288 * @param width the width of the raised bevel 289 * @param height the height of the raised bevel 290 */ 291 protected void paintRaisedBevel(Component c, Graphics g, int x, int y, 292 int width, int height) { 293 Color oldColor = g.getColor(); 294 int h = height; 295 int w = width; 296 297 g.translate(x, y); 298 299 g.setColor(getHighlightOuterColor(c)); 300 g.drawLine(0, 0, 0, h-2); 301 g.drawLine(1, 0, w-2, 0); 302 303 g.setColor(getHighlightInnerColor(c)); 304 g.drawLine(1, 1, 1, h-3); 305 g.drawLine(2, 1, w-3, 1); 306 307 g.setColor(getShadowOuterColor(c)); 308 g.drawLine(0, h-1, w-1, h-1); 309 g.drawLine(w-1, 0, w-1, h-2); 310 311 g.setColor(getShadowInnerColor(c)); 312 g.drawLine(1, h-2, w-2, h-2); 313 g.drawLine(w-2, 1, w-2, h-3); 314 315 g.translate(-x, -y); 316 g.setColor(oldColor); 317 318 } 319 320 /** 321 * Paints a lowered bevel for the specified component with the specified 322 * position and size. 323 * 324 * @param c the component for which the lowered bevel is being painted 325 * @param g the paint graphics 326 * @param x the x position of the lowered bevel 327 * @param y the y position of the lowered bevel 328 * @param width the width of the lowered bevel 329 * @param height the height of the lowered bevel 330 */ 331 protected void paintLoweredBevel(Component c, Graphics g, int x, int y, 332 int width, int height) { 333 Color oldColor = g.getColor(); 334 int h = height; 335 int w = width; 336 337 g.translate(x, y); 338 339 g.setColor(getShadowInnerColor(c)); 340 g.drawLine(0, 0, 0, h-1); 341 g.drawLine(1, 0, w-1, 0); 342 343 g.setColor(getShadowOuterColor(c)); 344 g.drawLine(1, 1, 1, h-2); 345 g.drawLine(2, 1, w-2, 1); 346 347 g.setColor(getHighlightOuterColor(c)); 348 g.drawLine(1, h-1, w-1, h-1); 349 g.drawLine(w-1, 1, w-1, h-2); 350 351 g.setColor(getHighlightInnerColor(c)); 352 g.drawLine(2, h-2, w-2, h-2); 353 g.drawLine(w-2, 2, w-2, h-3); 354 355 g.translate(-x, -y); 356 g.setColor(oldColor); 357 358 } 359 360 }