KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > border > BevelBorder


1 /*
2  * @(#)BevelBorder.java 1.20 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7 package javax.swing.border;
8
9 import java.awt.Graphics JavaDoc;
10 import java.awt.Insets JavaDoc;
11 import java.awt.Rectangle JavaDoc;
12 import java.awt.Color JavaDoc;
13 import java.awt.Component JavaDoc;
14
15
16 /**
17  * A class which implements a simple two-line bevel border.
18  * <p>
19  * <strong>Warning:</strong>
20  * Serialized objects of this class will not be compatible with
21  * future Swing releases. The current serialization support is
22  * appropriate for short term storage or RMI between applications running
23  * the same version of Swing. As of 1.4, support for long term storage
24  * of all JavaBeans<sup><font size="-2">TM</font></sup>
25  * has been added to the <code>java.beans</code> package.
26  * Please see {@link java.beans.XMLEncoder}.
27  *
28  * @version 1.20 12/19/03
29  * @author David Kloba
30  */

31 public class BevelBorder extends AbstractBorder JavaDoc
32 {
33     /** Raised bevel type. */
34     public static final int RAISED = 0;
35     /** Lowered bevel type. */
36     public static final int LOWERED = 1;
37
38     protected int bevelType;
39     protected Color JavaDoc highlightOuter;
40     protected Color JavaDoc highlightInner;
41     protected Color JavaDoc shadowInner;
42     protected Color JavaDoc shadowOuter;
43
44     /**
45      * Creates a bevel border with the specified type and whose
46      * colors will be derived from the background color of the
47      * component passed into the paintBorder method.
48      * @param bevelType the type of bevel for the border
49      */

50     public BevelBorder(int bevelType) {
51         this.bevelType = bevelType;
52     }
53
54     /**
55      * Creates a bevel border with the specified type, highlight and
56      * shadow colors.
57      * @param bevelType the type of bevel for the border
58      * @param highlight the color to use for the bevel highlight
59      * @param shadow the color to use for the bevel shadow
60      */

61     public BevelBorder(int bevelType, Color JavaDoc highlight, Color JavaDoc shadow) {
62         this(bevelType, highlight.brighter(), highlight, shadow, shadow.brighter());
63     }
64
65     /**
66      * Creates a bevel border with the specified type, highlight and
67      * shadow colors.
68      * <p>
69      * Note: The shadow inner and outer colors are
70      * switched for a lowered bevel border.
71      *
72      * @param bevelType the type of bevel for the border
73      * @param highlightOuterColor the color to use for the bevel outer highlight
74      * @param highlightInnerColor the color to use for the bevel inner highlight
75      * @param shadowOuterColor the color to use for the bevel outer shadow
76      * @param shadowInnerColor the color to use for the bevel inner shadow
77      */

78     public BevelBorder(int bevelType, Color JavaDoc highlightOuterColor,
79                        Color JavaDoc highlightInnerColor, Color JavaDoc shadowOuterColor,
80                        Color JavaDoc shadowInnerColor) {
81         this(bevelType);
82         this.highlightOuter = highlightOuterColor;
83         this.highlightInner = highlightInnerColor;
84         this.shadowOuter = shadowOuterColor;
85         this.shadowInner = shadowInnerColor;
86     }
87
88     /**
89      * Paints the border for the specified component with the specified
90      * position and size.
91      * @param c the component for which this border is being painted
92      * @param g the paint graphics
93      * @param x the x position of the painted border
94      * @param y the y position of the painted border
95      * @param width the width of the painted border
96      * @param height the height of the painted border
97      */

98     public void paintBorder(Component JavaDoc c, Graphics JavaDoc g, int x, int y, int width, int height) {
99         if (bevelType == RAISED) {
100              paintRaisedBevel(c, g, x, y, width, height);
101
102         } else if (bevelType == LOWERED) {
103              paintLoweredBevel(c, g, x, y, width, height);
104         }
105     }
106
107     /**
108      * Returns the insets of the border.
109      * @param c the component for which this border insets value applies
110      */

111     public Insets JavaDoc getBorderInsets(Component JavaDoc c) {
112     return new Insets JavaDoc(2, 2, 2, 2);
113     }
114
115     /**
116      * Reinitialize the insets parameter with this Border's current Insets.
117      * @param c the component for which this border insets value applies
118      * @param insets the object to be reinitialized
119      */

120     public Insets JavaDoc getBorderInsets(Component JavaDoc c, Insets JavaDoc insets) {
121         insets.left = insets.top = insets.right = insets.bottom = 2;
122         return insets;
123     }
124
125     /**
126      * Returns the outer highlight color of the bevel border
127      * when rendered on the specified component. If no highlight
128      * color was specified at instantiation, the highlight color
129      * is derived from the specified component's background color.
130      * @param c the component for which the highlight may be derived
131      */

132     public Color JavaDoc getHighlightOuterColor(Component JavaDoc c) {
133         Color JavaDoc highlight = getHighlightOuterColor();
134         return highlight != null? highlight :
135                                        c.getBackground().brighter().brighter();
136     }
137
138     /**
139      * Returns the inner highlight color of the bevel border
140      * when rendered on the specified component. If no highlight
141      * color was specified at instantiation, the highlight color
142      * is derived from the specified component's background color.
143      * @param c the component for which the highlight may be derived
144      */

145     public Color JavaDoc getHighlightInnerColor(Component JavaDoc c) {
146         Color JavaDoc highlight = getHighlightInnerColor();
147         return highlight != null? highlight :
148                                        c.getBackground().brighter();
149     }
150
151     /**
152      * Returns the inner shadow color of the bevel border
153      * when rendered on the specified component. If no shadow
154      * color was specified at instantiation, the shadow color
155      * is derived from the specified component's background color.
156      * @param c the component for which the shadow may be derived
157      */

158     public Color JavaDoc getShadowInnerColor(Component JavaDoc c) {
159         Color JavaDoc shadow = getShadowInnerColor();
160         return shadow != null? shadow :
161                                     c.getBackground().darker();
162     }
163
164     /**
165      * Returns the outer shadow color of the bevel border
166      * when rendered on the specified component. If no shadow
167      * color was specified at instantiation, the shadow color
168      * is derived from the specified component's background color.
169      * @param c the component for which the shadow may be derived
170      */

171     public Color JavaDoc getShadowOuterColor(Component JavaDoc c) {
172         Color JavaDoc shadow = getShadowOuterColor();
173         return shadow != null? shadow :
174                                     c.getBackground().darker().darker();
175     }
176
177     /**
178      * Returns the outer highlight color of the bevel border.
179      * Will return null if no highlight color was specified
180      * at instantiation.
181      */

182     public Color JavaDoc getHighlightOuterColor() {
183         return highlightOuter;
184     }
185
186     /**
187      * Returns the inner highlight color of the bevel border.
188      * Will return null if no highlight color was specified
189      * at instantiation.
190      */

191     public Color JavaDoc getHighlightInnerColor() {
192         return highlightInner;
193     }
194
195     /**
196      * Returns the inner shadow color of the bevel border.
197      * Will return null if no shadow color was specified
198      * at instantiation.
199      */

200     public Color JavaDoc getShadowInnerColor() {
201         return shadowInner;
202     }
203
204     /**
205      * Returns the outer shadow color of the bevel border.
206      * Will return null if no shadow color was specified
207      * at instantiation.
208      */

209     public Color JavaDoc getShadowOuterColor() {
210         return shadowOuter;
211     }
212
213     /**
214      * Returns the type of the bevel border.
215      */

216     public int getBevelType() {
217         return bevelType;
218     }
219
220     /**
221      * Returns whether or not the border is opaque.
222      */

223     public boolean isBorderOpaque() { return true; }
224
225     protected void paintRaisedBevel(Component JavaDoc c, Graphics JavaDoc g, int x, int y,
226                                     int width, int height) {
227         Color JavaDoc oldColor = g.getColor();
228         int h = height;
229         int w = width;
230
231         g.translate(x, y);
232
233         g.setColor(getHighlightOuterColor(c));
234         g.drawLine(0, 0, 0, h-2);
235         g.drawLine(1, 0, w-2, 0);
236
237         g.setColor(getHighlightInnerColor(c));
238         g.drawLine(1, 1, 1, h-3);
239         g.drawLine(2, 1, w-3, 1);
240
241         g.setColor(getShadowOuterColor(c));
242         g.drawLine(0, h-1, w-1, h-1);
243         g.drawLine(w-1, 0, w-1, h-2);
244
245         g.setColor(getShadowInnerColor(c));
246         g.drawLine(1, h-2, w-2, h-2);
247         g.drawLine(w-2, 1, w-2, h-3);
248
249         g.translate(-x, -y);
250         g.setColor(oldColor);
251
252     }
253
254     protected void paintLoweredBevel(Component JavaDoc c, Graphics JavaDoc g, int x, int y,
255                                         int width, int height) {
256         Color JavaDoc oldColor = g.getColor();
257         int h = height;
258         int w = width;
259
260         g.translate(x, y);
261
262         g.setColor(getShadowInnerColor(c));
263         g.drawLine(0, 0, 0, h-1);
264         g.drawLine(1, 0, w-1, 0);
265
266         g.setColor(getShadowOuterColor(c));
267         g.drawLine(1, 1, 1, h-2);
268         g.drawLine(2, 1, w-2, 1);
269
270         g.setColor(getHighlightOuterColor(c));
271         g.drawLine(1, h-1, w-1, h-1);
272         g.drawLine(w-1, 1, w-1, h-2);
273
274         g.setColor(getHighlightInnerColor(c));
275         g.drawLine(2, h-2, w-2, h-2);
276         g.drawLine(w-2, 2, w-2, h-3);
277
278         g.translate(-x, -y);
279         g.setColor(oldColor);
280
281     }
282
283 }
284
Popular Tags