KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)LineBorder.java 1.23 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 line border of arbitrary thickness
17  * and of a single color.
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.23 12/19/03
29  * @author David Kloba
30  */

31 public class LineBorder extends AbstractBorder JavaDoc
32 {
33     private static Border JavaDoc blackLine;
34     private static Border JavaDoc grayLine;
35
36     protected int thickness;
37     protected Color JavaDoc lineColor;
38     protected boolean roundedCorners;
39
40     /** Convenience method for getting the Color.black LineBorder of thickness 1.
41       */

42     public static Border JavaDoc createBlackLineBorder() {
43         if (blackLine == null) {
44             blackLine = new LineBorder JavaDoc(Color.black, 1);
45         }
46         return blackLine;
47     }
48
49     /** Convenience method for getting the Color.gray LineBorder of thickness 1.
50       */

51     public static Border JavaDoc createGrayLineBorder() {
52         if (grayLine == null) {
53             grayLine = new LineBorder JavaDoc(Color.gray, 1);
54         }
55         return grayLine;
56     }
57
58     /**
59      * Creates a line border with the specified color and a
60      * thickness = 1.
61      * @param color the color for the border
62      */

63     public LineBorder(Color JavaDoc color) {
64         this(color, 1, false);
65     }
66
67     /**
68      * Creates a line border with the specified color and thickness.
69      * @param color the color of the border
70      * @param thickness the thickness of the border
71      */

72     public LineBorder(Color JavaDoc color, int thickness) {
73         this(color, thickness, false);
74     }
75
76     /**
77      * Creates a line border with the specified color, thickness,
78      * and corner shape.
79      * @param color the color of the border
80      * @param thickness the thickness of the border
81      * @param roundedCorners whether or not border corners should be round
82      * @since 1.3
83      */

84     public LineBorder(Color JavaDoc color, int thickness, boolean roundedCorners) {
85         lineColor = color;
86         this.thickness = thickness;
87     this.roundedCorners = roundedCorners;
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         Color JavaDoc oldColor = g.getColor();
102         int i;
103
104     /// PENDING(klobad) How/should do we support Roundtangles?
105
g.setColor(lineColor);
106         for(i = 0; i < thickness; i++) {
107         if(!roundedCorners)
108                 g.drawRect(x+i, y+i, width-i-i-1, height-i-i-1);
109         else
110                 g.drawRoundRect(x+i, y+i, width-i-i-1, height-i-i-1, thickness, thickness);
111         }
112         g.setColor(oldColor);
113     }
114
115     /**
116      * Returns the insets of the border.
117      * @param c the component for which this border insets value applies
118      */

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

128     public Insets JavaDoc getBorderInsets(Component JavaDoc c, Insets JavaDoc insets) {
129         insets.left = insets.top = insets.right = insets.bottom = thickness;
130         return insets;
131     }
132
133     /**
134      * Returns the color of the border.
135      */

136     public Color JavaDoc getLineColor() {
137         return lineColor;
138     }
139
140     /**
141      * Returns the thickness of the border.
142      */

143     public int getThickness() {
144         return thickness;
145     }
146
147     /**
148      * Returns whether this border will be drawn with rounded corners.
149      */

150     public boolean getRoundedCorners() {
151         return roundedCorners;
152     }
153
154     /**
155      * Returns whether or not the border is opaque.
156      */

157     public boolean isBorderOpaque() {
158         return !roundedCorners;
159     }
160
161 }
162
Popular Tags