001 /*
002 * Copyright 1997-2007 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025 package javax.swing.border;
026
027 import java.awt.Graphics;
028 import java.awt.Insets;
029 import java.awt.Rectangle;
030 import java.awt.Color;
031 import java.awt.Component;
032 import java.beans.ConstructorProperties;
033
034 /**
035 * A class which implements a simple two-line bevel border.
036 * <p>
037 * <strong>Warning:</strong>
038 * Serialized objects of this class will not be compatible with
039 * future Swing releases. The current serialization support is
040 * appropriate for short term storage or RMI between applications running
041 * the same version of Swing. As of 1.4, support for long term storage
042 * of all JavaBeans<sup><font size="-2">TM</font></sup>
043 * has been added to the <code>java.beans</code> package.
044 * Please see {@link java.beans.XMLEncoder}.
045 *
046 * @version 1.27 05/05/07
047 * @author David Kloba
048 */
049 public class BevelBorder extends AbstractBorder {
050 /** Raised bevel type. */
051 public static final int RAISED = 0;
052 /** Lowered bevel type. */
053 public static final int LOWERED = 1;
054
055 protected int bevelType;
056 protected Color highlightOuter;
057 protected Color highlightInner;
058 protected Color shadowInner;
059 protected Color shadowOuter;
060
061 /**
062 * Creates a bevel border with the specified type and whose
063 * colors will be derived from the background color of the
064 * component passed into the paintBorder method.
065 * @param bevelType the type of bevel for the border
066 */
067 public BevelBorder(int bevelType) {
068 this .bevelType = bevelType;
069 }
070
071 /**
072 * Creates a bevel border with the specified type, highlight and
073 * shadow colors.
074 * @param bevelType the type of bevel for the border
075 * @param highlight the color to use for the bevel highlight
076 * @param shadow the color to use for the bevel shadow
077 */
078 public BevelBorder(int bevelType, Color highlight, Color shadow) {
079 this (bevelType, highlight.brighter(), highlight, shadow, shadow
080 .brighter());
081 }
082
083 /**
084 * Creates a bevel border with the specified type, highlight and
085 * shadow colors.
086 * <p>
087 * Note: The shadow inner and outer colors are
088 * switched for a lowered bevel border.
089 *
090 * @param bevelType the type of bevel for the border
091 * @param highlightOuterColor the color to use for the bevel outer highlight
092 * @param highlightInnerColor the color to use for the bevel inner highlight
093 * @param shadowOuterColor the color to use for the bevel outer shadow
094 * @param shadowInnerColor the color to use for the bevel inner shadow
095 */
096 @ConstructorProperties({"bevelType","highlightOuterColor","highlightInnerColor","shadowOuterColor","shadowInnerColor"})
097 public BevelBorder(int bevelType, Color highlightOuterColor,
098 Color highlightInnerColor, Color shadowOuterColor,
099 Color shadowInnerColor) {
100 this (bevelType);
101 this .highlightOuter = highlightOuterColor;
102 this .highlightInner = highlightInnerColor;
103 this .shadowOuter = shadowOuterColor;
104 this .shadowInner = shadowInnerColor;
105 }
106
107 /**
108 * Paints the border for the specified component with the specified
109 * position and size.
110 * @param c the component for which this border is being painted
111 * @param g the paint graphics
112 * @param x the x position of the painted border
113 * @param y the y position of the painted border
114 * @param width the width of the painted border
115 * @param height the height of the painted border
116 */
117 public void paintBorder(Component c, Graphics g, int x, int y,
118 int width, int height) {
119 if (bevelType == RAISED) {
120 paintRaisedBevel(c, g, x, y, width, height);
121
122 } else if (bevelType == LOWERED) {
123 paintLoweredBevel(c, g, x, y, width, height);
124 }
125 }
126
127 /**
128 * Returns the insets of the border.
129 * @param c the component for which this border insets value applies
130 */
131 public Insets getBorderInsets(Component c) {
132 return new Insets(2, 2, 2, 2);
133 }
134
135 /**
136 * Reinitialize the insets parameter with this Border's current Insets.
137 * @param c the component for which this border insets value applies
138 * @param insets the object to be reinitialized
139 */
140 public Insets getBorderInsets(Component c, Insets insets) {
141 insets.left = insets.top = insets.right = insets.bottom = 2;
142 return insets;
143 }
144
145 /**
146 * Returns the outer highlight color of the bevel border
147 * when rendered on the specified component. If no highlight
148 * color was specified at instantiation, the highlight color
149 * is derived from the specified component's background color.
150 * @param c the component for which the highlight may be derived
151 * @since 1.3
152 */
153 public Color getHighlightOuterColor(Component c) {
154 Color highlight = getHighlightOuterColor();
155 return highlight != null ? highlight : c.getBackground()
156 .brighter().brighter();
157 }
158
159 /**
160 * Returns the inner highlight color of the bevel border
161 * when rendered on the specified component. If no highlight
162 * color was specified at instantiation, the highlight color
163 * is derived from the specified component's background color.
164 * @param c the component for which the highlight may be derived
165 * @since 1.3
166 */
167 public Color getHighlightInnerColor(Component c) {
168 Color highlight = getHighlightInnerColor();
169 return highlight != null ? highlight : c.getBackground()
170 .brighter();
171 }
172
173 /**
174 * Returns the inner shadow color of the bevel border
175 * when rendered on the specified component. If no shadow
176 * color was specified at instantiation, the shadow color
177 * is derived from the specified component's background color.
178 * @param c the component for which the shadow may be derived
179 * @since 1.3
180 */
181 public Color getShadowInnerColor(Component c) {
182 Color shadow = getShadowInnerColor();
183 return shadow != null ? shadow : c.getBackground().darker();
184 }
185
186 /**
187 * Returns the outer shadow color of the bevel border
188 * when rendered on the specified component. If no shadow
189 * color was specified at instantiation, the shadow color
190 * is derived from the specified component's background color.
191 * @param c the component for which the shadow may be derived
192 * @since 1.3
193 */
194 public Color getShadowOuterColor(Component c) {
195 Color shadow = getShadowOuterColor();
196 return shadow != null ? shadow : c.getBackground().darker()
197 .darker();
198 }
199
200 /**
201 * Returns the outer highlight color of the bevel border.
202 * Will return null if no highlight color was specified
203 * at instantiation.
204 * @since 1.3
205 */
206 public Color getHighlightOuterColor() {
207 return highlightOuter;
208 }
209
210 /**
211 * Returns the inner highlight color of the bevel border.
212 * Will return null if no highlight color was specified
213 * at instantiation.
214 * @since 1.3
215 */
216 public Color getHighlightInnerColor() {
217 return highlightInner;
218 }
219
220 /**
221 * Returns the inner shadow color of the bevel border.
222 * Will return null if no shadow color was specified
223 * at instantiation.
224 * @since 1.3
225 */
226 public Color getShadowInnerColor() {
227 return shadowInner;
228 }
229
230 /**
231 * Returns the outer shadow color of the bevel border.
232 * Will return null if no shadow color was specified
233 * at instantiation.
234 * @since 1.3
235 */
236 public Color getShadowOuterColor() {
237 return shadowOuter;
238 }
239
240 /**
241 * Returns the type of the bevel border.
242 */
243 public int getBevelType() {
244 return bevelType;
245 }
246
247 /**
248 * Returns whether or not the border is opaque.
249 */
250 public boolean isBorderOpaque() {
251 return true;
252 }
253
254 protected void paintRaisedBevel(Component c, Graphics g, int x,
255 int y, int width, int height) {
256 Color oldColor = g.getColor();
257 int h = height;
258 int w = width;
259
260 g.translate(x, y);
261
262 g.setColor(getHighlightOuterColor(c));
263 g.drawLine(0, 0, 0, h - 2);
264 g.drawLine(1, 0, w - 2, 0);
265
266 g.setColor(getHighlightInnerColor(c));
267 g.drawLine(1, 1, 1, h - 3);
268 g.drawLine(2, 1, w - 3, 1);
269
270 g.setColor(getShadowOuterColor(c));
271 g.drawLine(0, h - 1, w - 1, h - 1);
272 g.drawLine(w - 1, 0, w - 1, h - 2);
273
274 g.setColor(getShadowInnerColor(c));
275 g.drawLine(1, h - 2, w - 2, h - 2);
276 g.drawLine(w - 2, 1, w - 2, h - 3);
277
278 g.translate(-x, -y);
279 g.setColor(oldColor);
280
281 }
282
283 protected void paintLoweredBevel(Component c, Graphics g, int x,
284 int y, int width, int height) {
285 Color oldColor = g.getColor();
286 int h = height;
287 int w = width;
288
289 g.translate(x, y);
290
291 g.setColor(getShadowInnerColor(c));
292 g.drawLine(0, 0, 0, h - 1);
293 g.drawLine(1, 0, w - 1, 0);
294
295 g.setColor(getShadowOuterColor(c));
296 g.drawLine(1, 1, 1, h - 2);
297 g.drawLine(2, 1, w - 2, 1);
298
299 g.setColor(getHighlightOuterColor(c));
300 g.drawLine(1, h - 1, w - 1, h - 1);
301 g.drawLine(w - 1, 1, w - 1, h - 2);
302
303 g.setColor(getHighlightInnerColor(c));
304 g.drawLine(2, h - 2, w - 2, h - 2);
305 g.drawLine(w - 2, 2, w - 2, h - 3);
306
307 g.translate(-x, -y);
308 g.setColor(oldColor);
309
310 }
311
312 }
|