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. */
39 package javax
.swing
.border
;
41 import java
.awt
.Color
;
42 import java
.awt
.Component
;
43 import java
.awt
.Graphics
;
44 import java
.awt
.Insets
;
45 import javax
.swing
.Icon
;
49 * A border that is filled with either a solid color or with repeated
52 * <p><img src="doc-files/MatteBorder-1.png" width="500" height="150"
53 * alt="[Two MatteBorders]" />
55 * @author Sascha Brawer (brawer@dandelis.ch)
57 public class MatteBorder
61 * Determined using the <code>serialver</code> tool
62 * of Apple/Sun JDK 1.3.1 on MacOS X 10.1.5.
64 static final long serialVersionUID
= 4422248989617298224L;
68 * The color that is used for filling the border, or
69 * <code>null</code> if the border is filled with repetitions of a
74 protected Color color
;
78 * The icon is used for filling the border with a tile, or
79 * <code>null</code> if the border is filled with a solid
84 protected Icon tileIcon
;
88 * Constructs a MatteBorder given the width on each side
91 * <p><img src="doc-files/MatteBorder-2.png" width="500" height="150"
92 * alt="[A picture of a MatteBorder made by this constructor]" />
94 * @param top the width of the border at its top edge.
95 * @param left the width of the border at its left edge.
96 * @param bottom the width of the border at its bottom edge.
97 * @param right the width of the border at its right edge.
98 * @param matteColor the color for filling the border.
100 public MatteBorder(int top
, int left
, int bottom
, int right
,
103 super(top
, left
, bottom
, right
);
105 if (matteColor
== null)
106 throw new IllegalArgumentException();
108 this.color
= matteColor
;
113 * Constructs a MatteBorder given its insets and fill color.
115 * <p><img src="doc-files/MatteBorder-3.png" width="500" height="150"
116 * alt="[A picture of a MatteBorder made by this constructor]" />
118 * @param borderInsets an Insets object whose <code>top</code>,
119 * <code>left</code>, <code>bottom</code> and <code>right</code>
120 * fields indicate the with of the border at the respective
123 * @param matteColor the color for filling the border.
125 public MatteBorder(Insets borderInsets
, Color matteColor
)
127 this(borderInsets
.top
, borderInsets
.left
,
128 borderInsets
.bottom
, borderInsets
.right
,
134 * Constructs a MatteBorder given the width on each side
135 * and an icon for tiling the border area.
137 * <p><img src="doc-files/MatteBorder-4.png" width="500" height="150"
138 * alt="[A picture of a MatteBorder made by this constructor]" />
140 * @param top the width of the border at its top edge.
141 * @param left the width of the border at its left edge.
142 * @param bottom the width of the border at its bottom edge.
143 * @param right the width of the border at its right edge.
144 * @param tileIcon an icon for tiling the border area.
146 public MatteBorder(int top
, int left
, int bottom
, int right
,
149 super(top
, left
, bottom
, right
);
151 if (tileIcon
== null)
152 throw new IllegalArgumentException();
154 this.tileIcon
= tileIcon
;
159 * Constructs a MatteBorder given its insets and an icon
160 * for tiling the border area.
162 * <p><img src="doc-files/MatteBorder-5.png" width="500" height="150"
163 * alt="[A picture of a MatteBorder made by this constructor]" />
165 * @param borderInsets an Insets object whose <code>top</code>,
166 * <code>left</code>, <code>bottom</code> and <code>right</code>
167 * fields indicate the with of the border at the respective
170 * @param tileIcon an icon for tiling the border area.
172 public MatteBorder(Insets borderInsets
, Icon tileIcon
)
174 this(borderInsets
.top
, borderInsets
.left
,
175 borderInsets
.bottom
, borderInsets
.right
,
181 * Constructs a MatteBorder given an icon for tiling the
182 * border area. The icon width is used for the border insets
183 * at the left and right edge, the icon height for the top and
186 * <p><img src="doc-files/MatteBorder-6.png" width="379" height="150"
187 * alt="[A picture of a MatteBorder made by this constructor]" />
189 * @param tileIcon an icon for tiling the border area.
191 public MatteBorder(Icon tileIcon
)
193 this(-1, -1, -1, -1, tileIcon
);
198 * Paints the border for a given component.
200 * @param c the component whose border is to be painted.
201 * @param g the graphics for painting.
202 * @param x the horizontal position for painting the border.
203 * @param y the vertical position for painting the border.
204 * @param width the width of the available area for painting the border.
205 * @param height the height of the available area for painting the border.
207 public void paintBorder(Component c
, Graphics g
,
208 int x
, int y
, int width
, int height
)
210 Insets i
= getBorderInsets();
211 paintEdge(c
, g
, x
, y
, width
, i
.top
, 0, 0); // top edge
212 paintEdge(c
, g
, x
, y
+ height
- i
.bottom
, // bottom edge
214 0, height
- i
.bottom
);
215 paintEdge(c
, g
, x
, y
+ i
.top
, // left edge
216 i
.left
, height
- i
.top
,
218 paintEdge(c
, g
, x
+ width
- i
.right
, y
+ i
.top
, // right edge
219 i
.right
, height
- i
.bottom
,
220 width
- i
.right
, i
.top
);
225 * Measures the width of this border.
227 * @param c the component whose border is to be measured.
229 * @return an Insets object whose <code>left</code>, <code>right</code>,
230 * <code>top</code> and <code>bottom</code> fields indicate the
231 * width of the border at the respective edge.
233 * @see #getBorderInsets(java.awt.Component, java.awt.Insets)
235 public Insets
getBorderInsets(Component c
)
237 /* There is no obvious reason for overriding this method, but we
238 * try to have exactly the same API as the Sun reference
241 return this.getBorderInsets(c
, null);
246 * Measures the width of this border, storing the results into a
247 * pre-existing Insets object.
249 * @param insets an Insets object for holding the result values.
250 * After invoking this method, the <code>left</code>,
251 * <code>right</code>, <code>top</code> and
252 * <code>bottom</code> fields indicate the width of the
253 * border at the respective edge.
255 * @return the same object that was passed for <code>insets</code>.
257 * @see #getBorderInsets()
259 public Insets
getBorderInsets(Component c
, Insets insets
)
262 insets
= new Insets(0, 0, 0, 0);
264 if ((tileIcon
!= null)
265 && (top
< 0) && (left
< 0)
266 && (right
< 0) && (bottom
< 0))
268 insets
.left
= insets
.right
= tileIcon
.getIconWidth();
269 insets
.top
= insets
.bottom
= tileIcon
.getIconHeight();
273 /* Copy top, left, bottom and right into the respective
276 return super.getBorderInsets(c
, insets
);
281 * Measures the width of this border.
283 * @return an Insets object whose <code>left</code>, <code>right</code>,
284 * <code>top</code> and <code>bottom</code> fields indicate the
285 * width of the border at the respective edge.
287 * @see #getBorderInsets(java.awt.Component, java.awt.Insets)
289 public Insets
getBorderInsets()
291 /* The inherited implementation of EmptyBorder.isBorderOpaque()
292 * would do the same. It is not clear why this is overriden in the
293 * Sun implementation, at least not from just reading the JavaDoc.
295 return this.getBorderInsets(null, null);
300 * Returns the color that is used for filling the border, or
301 * <code>null</code> if the border is filled with repetitions of a
304 public Color
getMatteColor()
311 * Returns the icon is used for tiling the border, or
312 * <code>null</code> if the border is filled with a color instead of
315 public Icon
getTileIcon()
322 * Determines whether this border fills every pixel in its area
325 * @return <code>true</code> if the border is filled with an
326 * opaque color; <code>false</code> if it is filled with
327 * a semi-transparent color or with an icon.
329 public boolean isBorderOpaque()
331 return (color
!= null) && (color
.getAlpha() == 255);
336 * Paints a rectangular area of the border. This private helper
337 * method is called once for each of the border edges
338 * by {@link #paintBorder}.
340 * @param c the component whose border is being painted.
341 * @param g the graphics for painting.
342 * @param x the horizontal position of the rectangular area.
343 * @param y the vertical position of the rectangular area.
344 * @param width the width of the rectangular area.
345 * @param height the height of the rectangular area.
346 * @param dx the x displacement for repeating the tile.
347 * @param dy the y displacement for repeating the tile.
349 private void paintEdge(Component c
, Graphics g
,
350 int x
, int y
, int width
, int height
,
354 int iconWidth
, iconHeight
;
357 if ((width
<= 0) || (height
<= 0))
360 /* Paint a colored rectangle if desired. */
363 oldColor
= g
.getColor();
367 g
.fillRect(x
, y
, width
, height
);
371 g
.setColor(oldColor
);
376 /* Determine the width and height of the icon. Some icons return
377 * -1 if it is an image whose dimensions have not yet been
378 * retrieved. There is not much we can do about this, but we
379 * should at least avoid entering the paint loop below
380 * with negative increments.
382 iconWidth
= tileIcon
.getIconWidth();
383 iconHeight
= tileIcon
.getIconHeight();
384 if ((iconWidth
<= 0) || (iconHeight
<= 0))
388 dy
= dy
% iconHeight
;
390 clipped
= g
.create();
393 clipped
.setClip(x
, y
, width
, height
);
394 for (int ty
= y
- dy
; ty
< y
+ height
; ty
+= iconHeight
)
395 for (int tx
= x
- dx
; tx
< x
+ width
; tx
+= iconWidth
)
396 tileIcon
.paintIcon(c
, clipped
, tx
, ty
);