author | malenkov |
Wed, 30 Apr 2014 19:28:05 +0400 | |
changeset 24544 | c0133e7c7162 |
parent 22574 | 7f8ce0c8c20a |
child 24500 | 399b77799d66 |
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.beans.ConstructorProperties; |
|
32 |
||
33 |
/** |
|
34 |
* A class which implements a simple two-line bevel border. |
|
35 |
* <p> |
|
36 |
* <strong>Warning:</strong> |
|
37 |
* Serialized objects of this class will not be compatible with |
|
38 |
* future Swing releases. The current serialization support is |
|
39 |
* appropriate for short term storage or RMI between applications running |
|
40 |
* the same version of Swing. As of 1.4, support for long term storage |
|
20458 | 41 |
* of all JavaBeans™ |
2 | 42 |
* has been added to the <code>java.beans</code> package. |
43 |
* Please see {@link java.beans.XMLEncoder}. |
|
44 |
* |
|
45 |
* @author David Kloba |
|
46 |
*/ |
|
22574
7f8ce0c8c20a
8032627: Add @SuppressWarnings("serial") to appropriate javax.swing classes
darcy
parents:
20458
diff
changeset
|
47 |
@SuppressWarnings("serial") // Same-version serialization only |
2 | 48 |
public class BevelBorder extends AbstractBorder |
49 |
{ |
|
50 |
/** Raised bevel type. */ |
|
51 |
public static final int RAISED = 0; |
|
52 |
/** Lowered bevel type. */ |
|
53 |
public static final int LOWERED = 1; |
|
54 |
||
55 |
protected int bevelType; |
|
56 |
protected Color highlightOuter; |
|
57 |
protected Color highlightInner; |
|
58 |
protected Color shadowInner; |
|
59 |
protected Color shadowOuter; |
|
60 |
||
61 |
/** |
|
62 |
* Creates a bevel border with the specified type and whose |
|
63 |
* colors will be derived from the background color of the |
|
64 |
* component passed into the paintBorder method. |
|
65 |
* @param bevelType the type of bevel for the border |
|
66 |
*/ |
|
67 |
public BevelBorder(int bevelType) { |
|
68 |
this.bevelType = bevelType; |
|
69 |
} |
|
70 |
||
71 |
/** |
|
72 |
* Creates a bevel border with the specified type, highlight and |
|
73 |
* shadow colors. |
|
74 |
* @param bevelType the type of bevel for the border |
|
75 |
* @param highlight the color to use for the bevel highlight |
|
76 |
* @param shadow the color to use for the bevel shadow |
|
77 |
*/ |
|
78 |
public BevelBorder(int bevelType, Color highlight, Color shadow) { |
|
79 |
this(bevelType, highlight.brighter(), highlight, shadow, shadow.brighter()); |
|
80 |
} |
|
81 |
||
82 |
/** |
|
83 |
* Creates a bevel border with the specified type, highlight and |
|
84 |
* shadow colors. |
|
85 |
* |
|
86 |
* @param bevelType the type of bevel for the border |
|
87 |
* @param highlightOuterColor the color to use for the bevel outer highlight |
|
88 |
* @param highlightInnerColor the color to use for the bevel inner highlight |
|
89 |
* @param shadowOuterColor the color to use for the bevel outer shadow |
|
90 |
* @param shadowInnerColor the color to use for the bevel inner shadow |
|
91 |
*/ |
|
92 |
@ConstructorProperties({"bevelType", "highlightOuterColor", "highlightInnerColor", "shadowOuterColor", "shadowInnerColor"}) |
|
93 |
public BevelBorder(int bevelType, Color highlightOuterColor, |
|
94 |
Color highlightInnerColor, Color shadowOuterColor, |
|
95 |
Color shadowInnerColor) { |
|
96 |
this(bevelType); |
|
97 |
this.highlightOuter = highlightOuterColor; |
|
98 |
this.highlightInner = highlightInnerColor; |
|
99 |
this.shadowOuter = shadowOuterColor; |
|
100 |
this.shadowInner = shadowInnerColor; |
|
101 |
} |
|
102 |
||
103 |
/** |
|
104 |
* Paints the border for the specified component with the specified |
|
105 |
* position and size. |
|
106 |
* @param c the component for which this border is being painted |
|
107 |
* @param g the paint graphics |
|
108 |
* @param x the x position of the painted border |
|
109 |
* @param y the y position of the painted border |
|
110 |
* @param width the width of the painted border |
|
111 |
* @param height the height of the painted border |
|
112 |
*/ |
|
113 |
public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) { |
|
114 |
if (bevelType == RAISED) { |
|
115 |
paintRaisedBevel(c, g, x, y, width, height); |
|
116 |
||
117 |
} else if (bevelType == LOWERED) { |
|
118 |
paintLoweredBevel(c, g, x, y, width, height); |
|
119 |
} |
|
120 |
} |
|
121 |
||
122 |
/** |
|
123 |
* Reinitialize the insets parameter with this Border's current Insets. |
|
124 |
* @param c the component for which this border insets value applies |
|
125 |
* @param insets the object to be reinitialized |
|
126 |
*/ |
|
127 |
public Insets getBorderInsets(Component c, Insets insets) { |
|
128 |
insets.set(2, 2, 2, 2); |
|
129 |
return insets; |
|
130 |
} |
|
131 |
||
132 |
/** |
|
133 |
* Returns the outer highlight color of the bevel border |
|
134 |
* when rendered on the specified component. If no highlight |
|
135 |
* color was specified at instantiation, the highlight color |
|
136 |
* is derived from the specified component's background color. |
|
137 |
* @param c the component for which the highlight may be derived |
|
138 |
* @since 1.3 |
|
139 |
*/ |
|
140 |
public Color getHighlightOuterColor(Component c) { |
|
141 |
Color highlight = getHighlightOuterColor(); |
|
142 |
return highlight != null? highlight : |
|
143 |
c.getBackground().brighter().brighter(); |
|
144 |
} |
|
145 |
||
146 |
/** |
|
147 |
* Returns the inner highlight color of the bevel border |
|
148 |
* when rendered on the specified component. If no highlight |
|
149 |
* color was specified at instantiation, the highlight color |
|
150 |
* is derived from the specified component's background color. |
|
151 |
* @param c the component for which the highlight may be derived |
|
152 |
* @since 1.3 |
|
153 |
*/ |
|
154 |
public Color getHighlightInnerColor(Component c) { |
|
155 |
Color highlight = getHighlightInnerColor(); |
|
156 |
return highlight != null? highlight : |
|
157 |
c.getBackground().brighter(); |
|
158 |
} |
|
159 |
||
160 |
/** |
|
161 |
* Returns the inner shadow color of the bevel border |
|
162 |
* when rendered on the specified component. If no shadow |
|
163 |
* color was specified at instantiation, the shadow color |
|
164 |
* is derived from the specified component's background color. |
|
165 |
* @param c the component for which the shadow may be derived |
|
166 |
* @since 1.3 |
|
167 |
*/ |
|
168 |
public Color getShadowInnerColor(Component c) { |
|
169 |
Color shadow = getShadowInnerColor(); |
|
170 |
return shadow != null? shadow : |
|
171 |
c.getBackground().darker(); |
|
172 |
} |
|
173 |
||
174 |
/** |
|
175 |
* Returns the outer shadow color of the bevel border |
|
176 |
* when rendered on the specified component. If no shadow |
|
177 |
* color was specified at instantiation, the shadow color |
|
178 |
* is derived from the specified component's background color. |
|
179 |
* @param c the component for which the shadow may be derived |
|
180 |
* @since 1.3 |
|
181 |
*/ |
|
182 |
public Color getShadowOuterColor(Component c) { |
|
183 |
Color shadow = getShadowOuterColor(); |
|
184 |
return shadow != null? shadow : |
|
185 |
c.getBackground().darker().darker(); |
|
186 |
} |
|
187 |
||
188 |
/** |
|
189 |
* Returns the outer highlight color of the bevel border. |
|
190 |
* Will return null if no highlight color was specified |
|
191 |
* at instantiation. |
|
192 |
* @since 1.3 |
|
193 |
*/ |
|
194 |
public Color getHighlightOuterColor() { |
|
195 |
return highlightOuter; |
|
196 |
} |
|
197 |
||
198 |
/** |
|
199 |
* Returns the inner highlight color of the bevel border. |
|
200 |
* Will return null if no highlight color was specified |
|
201 |
* at instantiation. |
|
202 |
* @since 1.3 |
|
203 |
*/ |
|
204 |
public Color getHighlightInnerColor() { |
|
205 |
return highlightInner; |
|
206 |
} |
|
207 |
||
208 |
/** |
|
209 |
* Returns the inner shadow color of the bevel border. |
|
210 |
* Will return null if no shadow color was specified |
|
211 |
* at instantiation. |
|
212 |
* @since 1.3 |
|
213 |
*/ |
|
214 |
public Color getShadowInnerColor() { |
|
215 |
return shadowInner; |
|
216 |
} |
|
217 |
||
218 |
/** |
|
219 |
* Returns the outer shadow color of the bevel border. |
|
220 |
* Will return null if no shadow color was specified |
|
221 |
* at instantiation. |
|
222 |
* @since 1.3 |
|
223 |
*/ |
|
224 |
public Color getShadowOuterColor() { |
|
225 |
return shadowOuter; |
|
226 |
} |
|
227 |
||
228 |
/** |
|
229 |
* Returns the type of the bevel border. |
|
230 |
*/ |
|
231 |
public int getBevelType() { |
|
232 |
return bevelType; |
|
233 |
} |
|
234 |
||
235 |
/** |
|
236 |
* Returns whether or not the border is opaque. |
|
237 |
*/ |
|
238 |
public boolean isBorderOpaque() { return true; } |
|
239 |
||
240 |
protected void paintRaisedBevel(Component c, Graphics g, int x, int y, |
|
241 |
int width, int height) { |
|
242 |
Color oldColor = g.getColor(); |
|
243 |
int h = height; |
|
244 |
int w = width; |
|
245 |
||
246 |
g.translate(x, y); |
|
247 |
||
248 |
g.setColor(getHighlightOuterColor(c)); |
|
249 |
g.drawLine(0, 0, 0, h-2); |
|
250 |
g.drawLine(1, 0, w-2, 0); |
|
251 |
||
252 |
g.setColor(getHighlightInnerColor(c)); |
|
253 |
g.drawLine(1, 1, 1, h-3); |
|
254 |
g.drawLine(2, 1, w-3, 1); |
|
255 |
||
256 |
g.setColor(getShadowOuterColor(c)); |
|
257 |
g.drawLine(0, h-1, w-1, h-1); |
|
258 |
g.drawLine(w-1, 0, w-1, h-2); |
|
259 |
||
260 |
g.setColor(getShadowInnerColor(c)); |
|
261 |
g.drawLine(1, h-2, w-2, h-2); |
|
262 |
g.drawLine(w-2, 1, w-2, h-3); |
|
263 |
||
264 |
g.translate(-x, -y); |
|
265 |
g.setColor(oldColor); |
|
266 |
||
267 |
} |
|
268 |
||
269 |
protected void paintLoweredBevel(Component c, Graphics g, int x, int y, |
|
270 |
int width, int height) { |
|
271 |
Color oldColor = g.getColor(); |
|
272 |
int h = height; |
|
273 |
int w = width; |
|
274 |
||
275 |
g.translate(x, y); |
|
276 |
||
277 |
g.setColor(getShadowInnerColor(c)); |
|
278 |
g.drawLine(0, 0, 0, h-1); |
|
279 |
g.drawLine(1, 0, w-1, 0); |
|
280 |
||
281 |
g.setColor(getShadowOuterColor(c)); |
|
282 |
g.drawLine(1, 1, 1, h-2); |
|
283 |
g.drawLine(2, 1, w-2, 1); |
|
284 |
||
285 |
g.setColor(getHighlightOuterColor(c)); |
|
286 |
g.drawLine(1, h-1, w-1, h-1); |
|
287 |
g.drawLine(w-1, 1, w-1, h-2); |
|
288 |
||
289 |
g.setColor(getHighlightInnerColor(c)); |
|
290 |
g.drawLine(2, h-2, w-2, h-2); |
|
291 |
g.drawLine(w-2, 2, w-2, h-3); |
|
292 |
||
293 |
g.translate(-x, -y); |
|
294 |
g.setColor(oldColor); |
|
295 |
||
296 |
} |
|
297 |
||
298 |
} |