author | yan |
Mon, 19 May 2014 10:43:09 +0400 | |
changeset 24500 | 399b77799d66 |
parent 22574 | 7f8ce0c8c20a |
child 24567 | a0ebe5fd56ff |
permissions | -rw-r--r-- |
2 | 1 |
/* |
22574
7f8ce0c8c20a
8032627: Add @SuppressWarnings("serial") to appropriate javax.swing classes
darcy
parents:
20458
diff
changeset
|
2 |
* Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 24 |
*/ |
25 |
package javax.swing.border; |
|
26 |
||
27 |
import java.awt.Graphics; |
|
28 |
import java.awt.Insets; |
|
29 |
import java.awt.Color; |
|
30 |
import java.awt.Component; |
|
31 |
import java.awt.Graphics2D; |
|
32 |
import java.awt.Shape; |
|
33 |
import java.awt.geom.Path2D; |
|
34 |
import java.awt.geom.Rectangle2D; |
|
35 |
import java.awt.geom.RoundRectangle2D; |
|
36 |
import java.beans.ConstructorProperties; |
|
37 |
||
38 |
/** |
|
39 |
* A class which implements a line border of arbitrary thickness |
|
40 |
* and of a single color. |
|
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 |
|
20458 | 47 |
* of all JavaBeans™ |
2 | 48 |
* has been added to the <code>java.beans</code> package. |
49 |
* Please see {@link java.beans.XMLEncoder}. |
|
50 |
* |
|
51 |
* @author David Kloba |
|
52 |
*/ |
|
22574
7f8ce0c8c20a
8032627: Add @SuppressWarnings("serial") to appropriate javax.swing classes
darcy
parents:
20458
diff
changeset
|
53 |
@SuppressWarnings("serial") // Same-version serialization only |
2 | 54 |
public class LineBorder extends AbstractBorder |
55 |
{ |
|
56 |
private static Border blackLine; |
|
57 |
private static Border grayLine; |
|
58 |
||
59 |
protected int thickness; |
|
60 |
protected Color lineColor; |
|
61 |
protected boolean roundedCorners; |
|
62 |
||
24500
399b77799d66
8040893: Add block tags for @return and @param to swing border classes
yan
parents:
22574
diff
changeset
|
63 |
/** |
399b77799d66
8040893: Add block tags for @return and @param to swing border classes
yan
parents:
22574
diff
changeset
|
64 |
* Convenience method for getting the Color.black LineBorder of thickness 1. |
399b77799d66
8040893: Add block tags for @return and @param to swing border classes
yan
parents:
22574
diff
changeset
|
65 |
* |
399b77799d66
8040893: Add block tags for @return and @param to swing border classes
yan
parents:
22574
diff
changeset
|
66 |
* @return a {@code LineBorder} with {@code Color.black} and thickness of 1 |
399b77799d66
8040893: Add block tags for @return and @param to swing border classes
yan
parents:
22574
diff
changeset
|
67 |
*/ |
2 | 68 |
public static Border createBlackLineBorder() { |
69 |
if (blackLine == null) { |
|
70 |
blackLine = new LineBorder(Color.black, 1); |
|
71 |
} |
|
72 |
return blackLine; |
|
73 |
} |
|
74 |
||
24500
399b77799d66
8040893: Add block tags for @return and @param to swing border classes
yan
parents:
22574
diff
changeset
|
75 |
/** |
399b77799d66
8040893: Add block tags for @return and @param to swing border classes
yan
parents:
22574
diff
changeset
|
76 |
* Convenience method for getting the Color.gray LineBorder of thickness 1. |
399b77799d66
8040893: Add block tags for @return and @param to swing border classes
yan
parents:
22574
diff
changeset
|
77 |
* |
399b77799d66
8040893: Add block tags for @return and @param to swing border classes
yan
parents:
22574
diff
changeset
|
78 |
* @return a {@code LineBorder} with {@code Color.gray} and thickness of 1 |
399b77799d66
8040893: Add block tags for @return and @param to swing border classes
yan
parents:
22574
diff
changeset
|
79 |
*/ |
2 | 80 |
public static Border createGrayLineBorder() { |
81 |
if (grayLine == null) { |
|
82 |
grayLine = new LineBorder(Color.gray, 1); |
|
83 |
} |
|
84 |
return grayLine; |
|
85 |
} |
|
86 |
||
87 |
/** |
|
88 |
* Creates a line border with the specified color and a |
|
89 |
* thickness = 1. |
|
24500
399b77799d66
8040893: Add block tags for @return and @param to swing border classes
yan
parents:
22574
diff
changeset
|
90 |
* |
2 | 91 |
* @param color the color for the border |
92 |
*/ |
|
93 |
public LineBorder(Color color) { |
|
94 |
this(color, 1, false); |
|
95 |
} |
|
96 |
||
97 |
/** |
|
98 |
* Creates a line border with the specified color and thickness. |
|
24500
399b77799d66
8040893: Add block tags for @return and @param to swing border classes
yan
parents:
22574
diff
changeset
|
99 |
* |
2 | 100 |
* @param color the color of the border |
101 |
* @param thickness the thickness of the border |
|
102 |
*/ |
|
103 |
public LineBorder(Color color, int thickness) { |
|
104 |
this(color, thickness, false); |
|
105 |
} |
|
106 |
||
107 |
/** |
|
108 |
* Creates a line border with the specified color, thickness, |
|
109 |
* and corner shape. |
|
24500
399b77799d66
8040893: Add block tags for @return and @param to swing border classes
yan
parents:
22574
diff
changeset
|
110 |
* |
2 | 111 |
* @param color the color of the border |
112 |
* @param thickness the thickness of the border |
|
113 |
* @param roundedCorners whether or not border corners should be round |
|
114 |
* @since 1.3 |
|
115 |
*/ |
|
116 |
@ConstructorProperties({"lineColor", "thickness", "roundedCorners"}) |
|
117 |
public LineBorder(Color color, int thickness, boolean roundedCorners) { |
|
118 |
lineColor = color; |
|
119 |
this.thickness = thickness; |
|
120 |
this.roundedCorners = roundedCorners; |
|
121 |
} |
|
122 |
||
123 |
/** |
|
124 |
* Paints the border for the specified component with the |
|
125 |
* specified position and size. |
|
24500
399b77799d66
8040893: Add block tags for @return and @param to swing border classes
yan
parents:
22574
diff
changeset
|
126 |
* |
2 | 127 |
* @param c the component for which this border is being painted |
128 |
* @param g the paint graphics |
|
129 |
* @param x the x position of the painted border |
|
130 |
* @param y the y position of the painted border |
|
131 |
* @param width the width of the painted border |
|
132 |
* @param height the height of the painted border |
|
133 |
*/ |
|
134 |
public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) { |
|
135 |
if ((this.thickness > 0) && (g instanceof Graphics2D)) { |
|
136 |
Graphics2D g2d = (Graphics2D) g; |
|
137 |
||
138 |
Color oldColor = g2d.getColor(); |
|
139 |
g2d.setColor(this.lineColor); |
|
140 |
||
141 |
Shape outer; |
|
142 |
Shape inner; |
|
143 |
||
144 |
int offs = this.thickness; |
|
145 |
int size = offs + offs; |
|
146 |
if (this.roundedCorners) { |
|
147 |
int arc = offs + size; |
|
148 |
outer = new RoundRectangle2D.Float(x, y, width, height, arc, arc); |
|
149 |
inner = new RoundRectangle2D.Float(x + offs, y + offs, width - size, height - size, arc, arc); |
|
150 |
} |
|
151 |
else { |
|
152 |
outer = new Rectangle2D.Float(x, y, width, height); |
|
153 |
inner = new Rectangle2D.Float(x + offs, y + offs, width - size, height - size); |
|
154 |
} |
|
155 |
Path2D path = new Path2D.Float(Path2D.WIND_EVEN_ODD); |
|
156 |
path.append(outer, false); |
|
157 |
path.append(inner, false); |
|
158 |
g2d.fill(path); |
|
159 |
g2d.setColor(oldColor); |
|
160 |
} |
|
161 |
} |
|
162 |
||
163 |
/** |
|
164 |
* Reinitialize the insets parameter with this Border's current Insets. |
|
24500
399b77799d66
8040893: Add block tags for @return and @param to swing border classes
yan
parents:
22574
diff
changeset
|
165 |
* |
2 | 166 |
* @param c the component for which this border insets value applies |
167 |
* @param insets the object to be reinitialized |
|
168 |
*/ |
|
169 |
public Insets getBorderInsets(Component c, Insets insets) { |
|
170 |
insets.set(thickness, thickness, thickness, thickness); |
|
171 |
return insets; |
|
172 |
} |
|
173 |
||
174 |
/** |
|
175 |
* Returns the color of the border. |
|
24500
399b77799d66
8040893: Add block tags for @return and @param to swing border classes
yan
parents:
22574
diff
changeset
|
176 |
* |
399b77799d66
8040893: Add block tags for @return and @param to swing border classes
yan
parents:
22574
diff
changeset
|
177 |
* @return a {@code Color} object representing the color of this object |
2 | 178 |
*/ |
179 |
public Color getLineColor() { |
|
180 |
return lineColor; |
|
181 |
} |
|
182 |
||
183 |
/** |
|
184 |
* Returns the thickness of the border. |
|
24500
399b77799d66
8040893: Add block tags for @return and @param to swing border classes
yan
parents:
22574
diff
changeset
|
185 |
* |
399b77799d66
8040893: Add block tags for @return and @param to swing border classes
yan
parents:
22574
diff
changeset
|
186 |
* @return the thickness of this border |
2 | 187 |
*/ |
188 |
public int getThickness() { |
|
189 |
return thickness; |
|
190 |
} |
|
191 |
||
192 |
/** |
|
193 |
* Returns whether this border will be drawn with rounded corners. |
|
24500
399b77799d66
8040893: Add block tags for @return and @param to swing border classes
yan
parents:
22574
diff
changeset
|
194 |
* |
399b77799d66
8040893: Add block tags for @return and @param to swing border classes
yan
parents:
22574
diff
changeset
|
195 |
* @return {@code true} if this border should have rounded corners |
2 | 196 |
* @since 1.3 |
197 |
*/ |
|
198 |
public boolean getRoundedCorners() { |
|
199 |
return roundedCorners; |
|
200 |
} |
|
201 |
||
202 |
/** |
|
203 |
* Returns whether or not the border is opaque. |
|
24500
399b77799d66
8040893: Add block tags for @return and @param to swing border classes
yan
parents:
22574
diff
changeset
|
204 |
* |
399b77799d66
8040893: Add block tags for @return and @param to swing border classes
yan
parents:
22574
diff
changeset
|
205 |
* @return {@code true} if the border is opaque, {@code false} otherwise |
2 | 206 |
*/ |
207 |
public boolean isBorderOpaque() { |
|
208 |
return !roundedCorners; |
|
209 |
} |
|
210 |
||
211 |
} |