1 /* SoftBevelBorder.java --
2 Copyright (C) 2003 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
38 package javax
.swing
.border
;
40 import java
.awt
.Color
;
41 import java
.awt
.Component
;
42 import java
.awt
.Graphics
;
43 import java
.awt
.Insets
;
47 * A rectangular, three pixel thick border that looks like a BevelBorder
48 * with slightly softened corners.
50 * <p>Like BevelBorder, SoftBevelBorder has a highlight and a shadow
51 * color. In the raised variant, the highlight color is used for the
52 * top and left edges, and the shadow color is used for the bottom and
53 * right edge. In the lowered variant, color usage is reversed. For
54 * an image, see the documentation of the individual constructors.
56 * @author Sascha Brawer (brawer@dandelis.ch)
58 public class SoftBevelBorder
62 * Determined using the <code>serialver</code> tool
63 * of Sun JDK 1.4.1_01 on GNU/Linux 2.4.20. Interestingly,
64 * the Apple/Sun JDK 1.3.1 on MacOS X 10.1.5 gives a different
65 * value, namely -6658357140774549493L.
67 static final long serialVersionUID
= 5248789787305979975L;
71 * Constructs a SoftBevelBorder whose colors will be derived from the
72 * background of the enclosed component. The background color is
73 * retrieved each time the border is painted, so a SoftBevelBorder
74 * constructed by this method will automatically reflect a change
75 * to the component’s background color.
77 * <p><img src="doc-files/SoftBevelBorder-1.png" width="500" height="200"
78 * alt="[An illustration showing raised and lowered SoftBevelBorders]" />
80 * @param bevelType the desired appearance of the border. The value
81 * must be either {@link BevelBorder#RAISED}
82 * or {@link BevelBorder#LOWERED}.
84 * @throws IllegalArgumentException if <code>bevelType</code> has
85 * an unsupported value.
87 public SoftBevelBorder(int bevelType
)
94 * Constructs a SoftBevelBorder given its appearance type and two
95 * colors for its highlight and shadow.
97 * <p><img src="doc-files/SoftBevelBorder-2.png" width="500" height="150"
98 * alt="[An illustration showing SoftBevelBorders that were
99 * constructed with this method]" />
101 * @param bevelType the desired appearance of the border. The value
102 * must be either {@link BevelBorder#RAISED} or {@link
103 * BevelBorder#LOWERED}.
105 * @param highlight the color that will be used for the inner side
106 * of the highlighted edges (top and left if if
107 * <code>bevelType</code> is {@link BevelBorder#RAISED};
108 * bottom and right otherwise). The color for the outer side
109 * is a brightened version of this color.
111 * @param shadow the color that will be used for the outer side of
112 * the shadowed edges (bottom and right if
113 * <code>bevelType</code> is {@link BevelBorder#RAISED}; top
114 * and left otherwise). The color for the inner side is a
115 * brightened version of this color.
117 * @throws IllegalArgumentException if <code>bevelType</code> has an
120 * @throws NullPointerException if <code>highlight</code> or
121 * <code>shadow</code> is <code>null</code>.
123 * @see java.awt.Color.brighter()
125 public SoftBevelBorder(int bevelType
, Color highlight
, Color shadow
)
128 /* highlightOuter */ highlight
.brighter(),
129 /* highlightInner */ highlight
,
130 /* shadowOuter */ shadow
,
131 /* shadowInner */ shadow
.brighter());
136 * Constructs a SoftBevelBorder given its appearance type and all
139 * <p><img src="doc-files/SoftBevelBorder-3.png" width="500" height="150"
140 * alt="[An illustration showing SoftBevelBorders that were
141 * constructed with this method]" />
143 * @param bevelType the desired appearance of the border. The value
144 * must be either {@link BevelBorder#RAISED} or {@link
145 * BevelBorder#LOWERED}.
147 * @param highlightOuter the color that will be used for the outer
148 * side of the highlighted edges (top and left if
149 * <code>bevelType</code> is {@link BevelBorder#RAISED};
150 * bottom and right otherwise).
152 * @param highlightInner the color that will be used for the inner
153 * side of the highlighted edges.
155 * @param shadowOuter the color that will be used for the outer side
156 * of the shadowed edges (bottom and right if
157 * <code>bevelType</code> is {@link BevelBorder#RAISED}; top
158 * and left otherwise).
160 * @param shadowInner the color that will be used for the inner
161 * side of the shadowed edges.
163 * @throws IllegalArgumentException if <code>bevelType</code> has
164 * an unsupported value.
166 * @throws NullPointerException if one of the passed colors
167 * is <code>null</code>.
169 public SoftBevelBorder(int bevelType
,
170 Color highlightOuter
, Color highlightInner
,
171 Color shadowOuter
, Color shadowInner
)
174 highlightOuter
, highlightInner
,
175 shadowOuter
, shadowInner
);
180 * Paints the border for a given component.
182 * @param c the component whose border is to be painted.
183 * @param g the graphics for painting.
184 * @param x the horizontal position for painting the border.
185 * @param y the vertical position for painting the border.
186 * @param width the width of the available area for painting the border.
187 * @param height the height of the available area for painting the border.
189 public void paintBorder(Component c
, Graphics g
,
190 int x
, int y
, int width
, int height
)
195 paintSoftBevel(g
, x
, y
, width
, height
,
196 getHighlightOuterColor(c
), getHighlightInnerColor(c
),
197 getShadowInnerColor(c
), getShadowOuterColor(c
));
201 paintSoftBevel(g
, x
, y
, width
, height
,
202 getShadowOuterColor(c
), getShadowInnerColor(c
),
203 getHighlightInnerColor(c
), getHighlightOuterColor(c
));
210 * Measures the width of this border.
212 * @param c the component whose border is to be measured.
214 * @return an Insets object whose <code>left</code>, <code>right</code>,
215 * <code>top</code> and <code>bottom</code> fields indicate the
216 * width of the border at the respective edge.
218 * @see #getBorderInsets(java.awt.Component, java.awt.Insets)
220 public Insets
getBorderInsets(Component c
)
222 return new Insets(3, 3, 3, 3);
227 * Measures the width of this border, storing the results into a
228 * pre-existing Insets object.
230 * @param insets an Insets object for holding the result values.
231 * After invoking this method, the <code>left</code>,
232 * <code>right</code>, <code>top</code> and
233 * <code>bottom</code> fields indicate the width of the
234 * border at the respective edge.
236 * @return the same object that was passed for <code>insets</code>.
238 * @see #getBorderInsets()
240 public Insets
getBorderInsets(Component c
, Insets insets
)
242 insets
.left
= insets
.right
= insets
.top
= insets
.bottom
= 3;
248 * Determines whether this border fills every pixel in its area
251 * <p>The enlarged view (see documentation for constructors) shows
252 * that a SoftBevelBorder does not paint all pixels. Therefore,
253 * this method always returns <code>false</code>.
255 * @return <code>false</code>.
257 public boolean isBorderOpaque()
264 * Paints a soft bevel in four colors.
268 * @@.........# @ = color a
274 * @param g the graphics for painting.
275 * @param x the horizontal position for painting the border.
276 * @param y the vertical position for painting the border.
277 * @param width the width of the available area for painting the border.
278 * @param height the height of the available area for painting the border.
279 * @param a the color for the outer side of the top and left edges.
280 * @param b the color for the inner side of the top and left edges.
281 * @param c the color for the inner side of the bottom and right edges.
282 * @param d the color for the outer side of the bottom and right edges.
284 private static void paintSoftBevel(Graphics g
,
285 int x
, int y
, int width
, int height
,
286 Color a
, Color b
, Color c
, Color d
)
290 oldColor
= g
.getColor();
297 /* To understand this code, it might be helpful to look at the
298 * images that are included with the JavaDoc, especially
299 * SoftBevelBorder-3.png. They are located in the "doc-files"
303 g
.drawLine(0, 0, width
- 1, 0); // a, horizontal
304 g
.drawLine(0, 1, 2, 1); // a, horizontal
305 g
.drawLine(0, 2, 0, height
- 1); // a, vertical
308 g
.drawLine(width
, 0, width
, 0); // b, horizontal
309 g
.drawLine(2, 1, width
- 1, 1); // b, horizontal
310 g
.drawLine(1, 2, 2, 2); // b, horizontal
311 g
.drawLine(1, 3, 1, height
- 1); // b, vertical
312 g
.drawLine(0, height
- 1, 0, height
); // b, vertical
315 g
.drawLine(width
- 1, height
- 1, // c, one pixel
316 width
- 1, height
- 1);
319 g
.drawLine(2, height
, width
, height
); // d, horizontal
320 g
.drawLine(width
, 2, width
, height
- 1); // d, vertical
325 g
.setColor(oldColor
);