KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)EtchedBorder.java 1.18 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  * A class which implements a simple etched border which can
17  * either be etched-in or etched-out. If no highlight/shadow
18  * colors are initialized when the border is created, then
19  * these colors will be dynamically derived from the background
20  * color of the component argument passed into the paintBorder()
21  * method.
22  * <p>
23  * <strong>Warning:</strong>
24  * Serialized objects of this class will not be compatible with
25  * future Swing releases. The current serialization support is
26  * appropriate for short term storage or RMI between applications running
27  * the same version of Swing. As of 1.4, support for long term storage
28  * of all JavaBeans<sup><font size="-2">TM</font></sup>
29  * has been added to the <code>java.beans</code> package.
30  * Please see {@link java.beans.XMLEncoder}.
31  *
32  * @version 1.18 12/19/03
33  * @author David Kloba
34  * @author Amy Fowler
35  */

36 public class EtchedBorder extends AbstractBorder JavaDoc
37 {
38     /** Raised etched type. */
39     public static final int RAISED = 0;
40     /** Lowered etched type. */
41     public static final int LOWERED = 1;
42
43     protected int etchType;
44     protected Color JavaDoc highlight;
45     protected Color JavaDoc shadow;
46
47     /**
48      * Creates a lowered etched border whose colors will be derived
49      * from the background color of the component passed into
50      * the paintBorder method.
51      */

52     public EtchedBorder() {
53         this(LOWERED);
54     }
55
56     /**
57      * Creates an etched border with the specified etch-type
58      * whose colors will be derived
59      * from the background color of the component passed into
60      * the paintBorder method.
61      * @param etchType the type of etch to be drawn by the border
62      */

63     public EtchedBorder(int etchType) {
64         this(etchType, null, null);
65     }
66
67     /**
68      * Creates a lowered etched border with the specified highlight and
69      * shadow colors.
70      * @param highlight the color to use for the etched highlight
71      * @param shadow the color to use for the etched shadow
72      */

73     public EtchedBorder(Color JavaDoc highlight, Color JavaDoc shadow) {
74         this(LOWERED, highlight, shadow);
75     }
76
77     /**
78      * Creates an etched border with the specified etch-type,
79      * highlight and shadow colors.
80      * @param etchType the type of etch to be drawn by the border
81      * @param highlight the color to use for the etched highlight
82      * @param shadow the color to use for the etched shadow
83      */

84     public EtchedBorder(int etchType, Color JavaDoc highlight, Color JavaDoc shadow) {
85         this.etchType = etchType;
86         this.highlight = highlight;
87         this.shadow = shadow;
88     }
89
90     /**
91      * Paints the border for the specified component with the
92      * specified position and size.
93      * @param c the component for which this border is being painted
94      * @param g the paint graphics
95      * @param x the x position of the painted border
96      * @param y the y position of the painted border
97      * @param width the width of the painted border
98      * @param height the height of the painted border
99      */

100     public void paintBorder(Component JavaDoc c, Graphics JavaDoc g, int x, int y, int width, int height) {
101     int w = width;
102     int h = height;
103     
104     g.translate(x, y);
105     
106     g.setColor(etchType == LOWERED? getShadowColor(c) : getHighlightColor(c));
107     g.drawRect(0, 0, w-2, h-2);
108     
109     g.setColor(etchType == LOWERED? getHighlightColor(c) : getShadowColor(c));
110     g.drawLine(1, h-3, 1, 1);
111     g.drawLine(1, 1, w-3, 1);
112     
113     g.drawLine(0, h-1, w-1, h-1);
114     g.drawLine(w-1, h-1, w-1, 0);
115     
116     g.translate(-x, -y);
117     }
118
119     /**
120      * Returns the insets of the border.
121      * @param c the component for which this border insets value applies
122      */

123     public Insets JavaDoc getBorderInsets(Component JavaDoc c) {
124         return new Insets JavaDoc(2, 2, 2, 2);
125     }
126
127     /**
128      * Reinitialize the insets parameter with this Border's current Insets.
129      * @param c the component for which this border insets value applies
130      * @param insets the object to be reinitialized
131      */

132     public Insets JavaDoc getBorderInsets(Component JavaDoc c, Insets JavaDoc insets) {
133         insets.left = insets.top = insets.right = insets.bottom = 2;
134         return insets;
135     }
136
137     /**
138      * Returns whether or not the border is opaque.
139      */

140     public boolean isBorderOpaque() { return true; }
141
142     /**
143      * Returns which etch-type is set on the etched border.
144      */

145     public int getEtchType() {
146         return etchType;
147     }
148
149     /**
150      * Returns the highlight color of the etched 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      */

156     public Color JavaDoc getHighlightColor(Component JavaDoc c) {
157         return highlight != null? highlight :
158                                        c.getBackground().brighter();
159     }
160
161     /**
162      * Returns the highlight color of the etched border.
163      * Will return null if no highlight color was specified
164      * at instantiation.
165      */

166     public Color JavaDoc getHighlightColor() {
167         return highlight;
168     }
169
170     /**
171      * Returns the shadow color of the etched border
172      * when rendered on the specified component. If no shadow
173      * color was specified at instantiation, the shadow color
174      * is derived from the specified component's background color.
175      * @param c the component for which the shadow may be derived
176      */

177     public Color JavaDoc getShadowColor(Component JavaDoc c) {
178         return shadow != null? shadow : c.getBackground().darker();
179     }
180
181     /**
182      * Returns the shadow color of the etched border.
183      * Will return null if no shadow color was specified
184      * at instantiation.
185      */

186     public Color JavaDoc getShadowColor() {
187         return shadow;
188     }
189
190 }
191
Popular Tags