2 Copyright (C) 2003, 2004, 2006 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., 51 Franklin Street, Fifth Floor, 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;
46 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 extends EmptyBorder
60 * Determined using the <code>serialver</code> tool
61 * of Apple/Sun JDK 1.3.1 on MacOS X 10.1.5.
63 static final long serialVersionUID = 4422248989617298224L;
67 * The color that is used for filling the border, or
68 * <code>null</code> if the border is filled with repetitions of a
73 protected Color color;
77 * The icon is used for filling the border with a tile, or
78 * <code>null</code> if the border is filled with a solid
83 protected Icon tileIcon;
87 * Constructs a MatteBorder given the width on each side
90 * <p><img src="doc-files/MatteBorder-2.png" width="500" height="150"
91 * alt="[A picture of a MatteBorder made by this constructor]" />
93 * @param top the width of the border at its top edge.
94 * @param left the width of the border at its left edge.
95 * @param bottom the width of the border at its bottom edge.
96 * @param right the width of the border at its right edge.
97 * @param matteColor the color for filling the border.
99 public MatteBorder(int top, int left, int bottom, int right,
102 super(top, left, bottom, right);
104 if (matteColor == null)
105 throw new IllegalArgumentException();
107 this.color = matteColor;
112 * Constructs a MatteBorder given its insets and fill color.
114 * <p><img src="doc-files/MatteBorder-3.png" width="500" height="150"
115 * alt="[A picture of a MatteBorder made by this constructor]" />
117 * @param borderInsets an Insets object whose <code>top</code>,
118 * <code>left</code>, <code>bottom</code> and <code>right</code>
119 * fields indicate the with of the border at the respective
122 * @param matteColor the color for filling the border.
124 public MatteBorder(Insets borderInsets, Color matteColor)
126 this(borderInsets.top, borderInsets.left,
127 borderInsets.bottom, borderInsets.right,
133 * Constructs a MatteBorder given the width on each side
134 * and an icon for tiling the border area.
136 * <p><img src="doc-files/MatteBorder-4.png" width="500" height="150"
137 * alt="[A picture of a MatteBorder made by this constructor]" />
139 * @param top the width of the border at its top edge.
140 * @param left the width of the border at its left edge.
141 * @param bottom the width of the border at its bottom edge.
142 * @param right the width of the border at its right edge.
143 * @param tileIcon an icon for tiling the border area.
145 public MatteBorder(int top, int left, int bottom, int right,
148 super(top, left, bottom, right);
150 this.tileIcon = tileIcon;
155 * Constructs a MatteBorder given its insets and an icon
156 * for tiling the border area.
158 * <p><img src="doc-files/MatteBorder-5.png" width="500" height="150"
159 * alt="[A picture of a MatteBorder made by this constructor]" />
161 * @param borderInsets an Insets object whose <code>top</code>,
162 * <code>left</code>, <code>bottom</code> and <code>right</code>
163 * fields indicate the with of the border at the respective
166 * @param tileIcon an icon for tiling the border area.
168 public MatteBorder(Insets borderInsets, Icon tileIcon)
170 this(borderInsets.top, borderInsets.left,
171 borderInsets.bottom, borderInsets.right,
177 * Constructs a MatteBorder given an icon for tiling the
178 * border area. The icon width is used for the border insets
179 * at the left and right edge, the icon height for the top and
182 * <p><img src="doc-files/MatteBorder-6.png" width="379" height="150"
183 * alt="[A picture of a MatteBorder made by this constructor]" />
185 * @param tileIcon an icon for tiling the border area.
187 public MatteBorder(Icon tileIcon)
189 this(-1, -1, -1, -1, tileIcon);
194 * Paints the border for a given component.
196 * @param c the component whose border is to be painted.
197 * @param g the graphics for painting.
198 * @param x the horizontal position for painting the border.
199 * @param y the vertical position for painting the border.
200 * @param width the width of the available area for painting the border.
201 * @param height the height of the available area for painting the border.
203 public void paintBorder(Component c, Graphics g,
204 int x, int y, int width, int height)
206 Insets i = getBorderInsets();
207 paintEdge(c, g, x, y, width, i.top, 0, 0); // top edge
208 paintEdge(c, g, x, y + height - i.bottom, // bottom edge
210 0, height - i.bottom);
211 paintEdge(c, g, x, y + i.top, // left edge
212 i.left, height - i.top,
214 paintEdge(c, g, x + width - i.right, y + i.top, // right edge
215 i.right, height - i.bottom,
216 width - i.right, i.top);
221 * Measures the width of this border.
223 * @param c the component whose border is to be measured.
225 * @return an Insets object whose <code>left</code>, <code>right</code>,
226 * <code>top</code> and <code>bottom</code> fields indicate the
227 * width of the border at the respective edge.
229 * @see #getBorderInsets(java.awt.Component, java.awt.Insets)
231 public Insets getBorderInsets(Component c)
233 /* There is no obvious reason for overriding this method, but we
234 * try to have exactly the same API as the Sun reference
237 return this.getBorderInsets(c, null);
242 * Measures the width of this border, storing the results into a
243 * pre-existing Insets object.
245 * @param insets an Insets object for holding the result values.
246 * After invoking this method, the <code>left</code>,
247 * <code>right</code>, <code>top</code> and
248 * <code>bottom</code> fields indicate the width of the
249 * border at the respective edge.
251 * @return the same object that was passed for <code>insets</code>.
253 * @see #getBorderInsets()
255 public Insets getBorderInsets(Component c, Insets insets)
258 insets = new Insets(0, 0, 0, 0);
260 if ((tileIcon != null)
261 && (top < 0) && (left < 0)
262 && (right < 0) && (bottom < 0))
264 insets.left = insets.right = tileIcon.getIconWidth();
265 insets.top = insets.bottom = tileIcon.getIconHeight();
269 /* Copy top, left, bottom and right into the respective
272 return super.getBorderInsets(c, insets);
277 * Measures the width of this border.
279 * @return an Insets object whose <code>left</code>, <code>right</code>,
280 * <code>top</code> and <code>bottom</code> fields indicate the
281 * width of the border at the respective edge.
283 * @see #getBorderInsets(java.awt.Component, java.awt.Insets)
285 public Insets getBorderInsets()
287 /* The inherited implementation of EmptyBorder.isBorderOpaque()
288 * would do the same. It is not clear why this is overriden in the
289 * Sun implementation, at least not from just reading the JavaDoc.
291 return this.getBorderInsets(null, null);
296 * Returns the color that is used for filling the border, or
297 * <code>null</code> if the border is filled with repetitions of a
300 * @return The color (possibly <code>null</code>).
302 public Color getMatteColor()
309 * Returns the icon is used for tiling the border, or
310 * <code>null</code> if the border is filled with a color instead of
313 * @return The icon (possibly <code>null</code>).
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 // If this border has no icon end painting here.
377 if (tileIcon == null)
380 /* Determine the width and height of the icon. Some icons return
381 * -1 if it is an image whose dimensions have not yet been
382 * retrieved. There is not much we can do about this, but we
383 * should at least avoid entering the paint loop below
384 * with negative increments.
386 iconWidth = tileIcon.getIconWidth();
387 iconHeight = tileIcon.getIconHeight();
388 if ((iconWidth <= 0) || (iconHeight <= 0))
392 dy = dy % iconHeight;
394 clipped = g.create();
397 clipped.setClip(x, y, width, height);
398 for (int ty = y - dy; ty < y + height; ty += iconHeight)
399 for (int tx = x - dx; tx < x + width; tx += iconWidth)
400 tileIcon.paintIcon(c, clipped, tx, ty);