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.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™ 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 @SuppressWarnings("serial") // Same-version serialization only 55 public class EtchedBorder extends AbstractBorder 56 { 57 /** Raised etched type. */ 58 public static final int RAISED = 0; 59 /** Lowered etched type. */ 60 public static final int LOWERED = 1; 61 62 /** 63 * The type of etch to be drawn by the border. 64 */ 65 protected int etchType; 66 /** 67 * The color to use for the etched highlight. 68 */ 69 protected Color highlight; 70 /** 71 * The color to use for the etched shadow. 72 */ 73 protected Color shadow; 74 75 /** 76 * Creates a lowered etched border whose colors will be derived 77 * from the background color of the component passed into 78 * the paintBorder method. 79 */ 80 public EtchedBorder() { 81 this(LOWERED); 82 } 83 84 /** 85 * Creates an etched border with the specified etch-type 86 * whose colors will be derived 87 * from the background color of the component passed into 88 * the paintBorder method. 89 * 90 * @param etchType the type of etch to be drawn by the border 91 */ 92 public EtchedBorder(int etchType) { 93 this(etchType, null, null); 94 } 95 96 /** 97 * Creates a lowered etched border with the specified highlight and 98 * shadow colors. 99 * 100 * @param highlight the color to use for the etched highlight 101 * @param shadow the color to use for the etched shadow 102 */ 103 public EtchedBorder(Color highlight, Color shadow) { 104 this(LOWERED, highlight, shadow); 105 } 106 107 /** 108 * Creates an etched border with the specified etch-type, 109 * highlight and shadow colors. 110 * 111 * @param etchType the type of etch to be drawn by the border 112 * @param highlight the color to use for the etched highlight 113 * @param shadow the color to use for the etched shadow 114 */ 115 @ConstructorProperties({"etchType", "highlightColor", "shadowColor"}) 116 public EtchedBorder(int etchType, Color highlight, Color shadow) { 117 this.etchType = etchType; 118 this.highlight = highlight; 119 this.shadow = shadow; 120 } 121 122 /** 123 * Paints the border for the specified component with the 124 * specified position and size. 125 * 126 * @param c the component for which this border is being painted 127 * @param g the paint graphics 128 * @param x the x position of the painted border 129 * @param y the y position of the painted border 130 * @param width the width of the painted border 131 * @param height the height of the painted border 132 */ 133 public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) { 134 int w = width; 135 int h = height; 136 137 g.translate(x, y); 138 139 g.setColor(etchType == LOWERED? getShadowColor(c) : getHighlightColor(c)); 140 g.drawRect(0, 0, w-2, h-2); 141 142 g.setColor(etchType == LOWERED? getHighlightColor(c) : getShadowColor(c)); 143 g.drawLine(1, h-3, 1, 1); 144 g.drawLine(1, 1, w-3, 1); 145 146 g.drawLine(0, h-1, w-1, h-1); 147 g.drawLine(w-1, h-1, w-1, 0); 148 149 g.translate(-x, -y); 150 } 151 152 /** 153 * Reinitialize the insets parameter with this Border's current Insets. 154 * 155 * @param c the component for which this border insets value applies 156 * @param insets the object to be reinitialized 157 */ 158 public Insets getBorderInsets(Component c, Insets insets) { 159 insets.set(2, 2, 2, 2); 160 return insets; 161 } 162 163 /** 164 * Returns whether or not the border is opaque. 165 * This implementation returns true. 166 * 167 * @return true 168 */ 169 public boolean isBorderOpaque() { return true; } 170 171 /** 172 * Returns which etch-type is set on the etched border. 173 * 174 * @return the etched border type, either {@code RAISED} or {@code LOWERED} 175 */ 176 public int getEtchType() { 177 return etchType; 178 } 179 180 /** 181 * Returns the highlight color of the etched border 182 * when rendered on the specified component. If no highlight 183 * color was specified at instantiation, the highlight color 184 * is derived from the specified component's background color. 185 * 186 * @param c the component for which the highlight may be derived 187 * @return the highlight {@code Color} of this {@code EtchedBorder} 188 * @since 1.3 189 */ 190 public Color getHighlightColor(Component c) { 191 return highlight != null? highlight : 192 c.getBackground().brighter(); 193 } 194 195 /** 196 * Returns the highlight color of the etched border. 197 * Will return null if no highlight color was specified 198 * at instantiation. 199 * 200 * @return the highlight {@code Color} of this {@code EtchedBorder} or null 201 * if none was specified 202 * @since 1.3 203 */ 204 public Color getHighlightColor() { 205 return highlight; 206 } 207 208 /** 209 * Returns the shadow color of the etched border 210 * when rendered on the specified component. If no shadow 211 * color was specified at instantiation, the shadow color 212 * is derived from the specified component's background color. 213 * 214 * @param c the component for which the shadow may be derived 215 * @return the shadow {@code Color} of this {@code EtchedBorder} 216 * @since 1.3 217 */ 218 public Color getShadowColor(Component c) { 219 return shadow != null? shadow : c.getBackground().darker(); 220 } 221 222 /** 223 * Returns the shadow color of the etched border. 224 * Will return null if no shadow color was specified 225 * at instantiation. 226 * 227 * @return the shadow {@code Color} of this {@code EtchedBorder} or null 228 * if none was specified 229 * @since 1.3 230 */ 231 public Color getShadowColor() { 232 return shadow; 233 } 234 235 }