2
|
1 |
/*
|
5584
|
2 |
* Copyright 1997-2010 Sun Microsystems, Inc. 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
|
|
7 |
* published by the Free Software Foundation. Sun designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Sun in the LICENSE file that accompanied this code.
|
|
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 |
*
|
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
23 |
* have any questions.
|
|
24 |
*/
|
|
25 |
package javax.swing.border;
|
|
26 |
|
|
27 |
import java.awt.Graphics;
|
|
28 |
import java.awt.Insets;
|
|
29 |
import java.awt.Component;
|
|
30 |
import java.awt.Color;
|
|
31 |
|
|
32 |
import javax.swing.Icon;
|
|
33 |
|
|
34 |
/**
|
|
35 |
* A class which provides a matte-like border of either a solid color
|
|
36 |
* or a tiled icon.
|
|
37 |
* <p>
|
|
38 |
* <strong>Warning:</strong>
|
|
39 |
* Serialized objects of this class will not be compatible with
|
|
40 |
* future Swing releases. The current serialization support is
|
|
41 |
* appropriate for short term storage or RMI between applications running
|
|
42 |
* the same version of Swing. As of 1.4, support for long term storage
|
|
43 |
* of all JavaBeans<sup><font size="-2">TM</font></sup>
|
|
44 |
* has been added to the <code>java.beans</code> package.
|
|
45 |
* Please see {@link java.beans.XMLEncoder}.
|
|
46 |
*
|
|
47 |
* @author Amy Fowler
|
|
48 |
*/
|
|
49 |
public class MatteBorder extends EmptyBorder
|
|
50 |
{
|
|
51 |
protected Color color;
|
|
52 |
protected Icon tileIcon;
|
|
53 |
|
|
54 |
/**
|
|
55 |
* Creates a matte border with the specified insets and color.
|
|
56 |
* @param top the top inset of the border
|
|
57 |
* @param left the left inset of the border
|
|
58 |
* @param bottom the bottom inset of the border
|
|
59 |
* @param right the right inset of the border
|
|
60 |
* @param matteColor the color rendered for the border
|
|
61 |
*/
|
|
62 |
public MatteBorder(int top, int left, int bottom, int right, Color matteColor) {
|
|
63 |
super(top, left, bottom, right);
|
|
64 |
this.color = matteColor;
|
|
65 |
}
|
|
66 |
|
|
67 |
/**
|
|
68 |
* Creates a matte border with the specified insets and color.
|
|
69 |
* @param borderInsets the insets of the border
|
|
70 |
* @param matteColor the color rendered for the border
|
|
71 |
* @since 1.3
|
|
72 |
*/
|
|
73 |
public MatteBorder(Insets borderInsets, Color matteColor) {
|
|
74 |
super(borderInsets);
|
|
75 |
this.color = matteColor;
|
|
76 |
}
|
|
77 |
|
|
78 |
/**
|
|
79 |
* Creates a matte border with the specified insets and tile icon.
|
|
80 |
* @param top the top inset of the border
|
|
81 |
* @param left the left inset of the border
|
|
82 |
* @param bottom the bottom inset of the border
|
|
83 |
* @param right the right inset of the border
|
|
84 |
* @param tileIcon the icon to be used for tiling the border
|
|
85 |
*/
|
|
86 |
public MatteBorder(int top, int left, int bottom, int right, Icon tileIcon) {
|
|
87 |
super(top, left, bottom, right);
|
|
88 |
this.tileIcon = tileIcon;
|
|
89 |
}
|
|
90 |
|
|
91 |
/**
|
|
92 |
* Creates a matte border with the specified insets and tile icon.
|
|
93 |
* @param borderInsets the insets of the border
|
|
94 |
* @param tileIcon the icon to be used for tiling the border
|
|
95 |
* @since 1.3
|
|
96 |
*/
|
|
97 |
public MatteBorder(Insets borderInsets, Icon tileIcon) {
|
|
98 |
super(borderInsets);
|
|
99 |
this.tileIcon = tileIcon;
|
|
100 |
}
|
|
101 |
|
|
102 |
/**
|
|
103 |
* Creates a matte border with the specified tile icon. The
|
|
104 |
* insets will be calculated dynamically based on the size of
|
|
105 |
* the tile icon, where the top and bottom will be equal to the
|
|
106 |
* tile icon's height, and the left and right will be equal to
|
|
107 |
* the tile icon's width.
|
|
108 |
* @param tileIcon the icon to be used for tiling the border
|
|
109 |
*/
|
|
110 |
public MatteBorder(Icon tileIcon) {
|
|
111 |
this(-1,-1,-1,-1, tileIcon);
|
|
112 |
}
|
|
113 |
|
|
114 |
/**
|
|
115 |
* Paints the matte border.
|
|
116 |
*/
|
|
117 |
public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
|
|
118 |
Insets insets = getBorderInsets(c);
|
|
119 |
Color oldColor = g.getColor();
|
|
120 |
g.translate(x, y);
|
|
121 |
|
|
122 |
// If the tileIcon failed loading, paint as gray.
|
|
123 |
if (tileIcon != null) {
|
|
124 |
color = (tileIcon.getIconWidth() == -1) ? Color.gray : null;
|
|
125 |
}
|
|
126 |
|
|
127 |
if (color != null) {
|
|
128 |
g.setColor(color);
|
|
129 |
g.fillRect(0, 0, width - insets.right, insets.top);
|
|
130 |
g.fillRect(0, insets.top, insets.left, height - insets.top);
|
|
131 |
g.fillRect(insets.left, height - insets.bottom, width - insets.left, insets.bottom);
|
|
132 |
g.fillRect(width - insets.right, 0, insets.right, height - insets.bottom);
|
|
133 |
|
|
134 |
} else if (tileIcon != null) {
|
|
135 |
int tileW = tileIcon.getIconWidth();
|
|
136 |
int tileH = tileIcon.getIconHeight();
|
5584
|
137 |
paintEdge(c, g, 0, 0, width - insets.right, insets.top, tileW, tileH);
|
|
138 |
paintEdge(c, g, 0, insets.top, insets.left, height - insets.top, tileW, tileH);
|
|
139 |
paintEdge(c, g, insets.left, height - insets.bottom, width - insets.left, insets.bottom, tileW, tileH);
|
|
140 |
paintEdge(c, g, width - insets.right, 0, insets.right, height - insets.bottom, tileW, tileH);
|
2
|
141 |
}
|
|
142 |
g.translate(-x, -y);
|
|
143 |
g.setColor(oldColor);
|
|
144 |
|
|
145 |
}
|
|
146 |
|
5584
|
147 |
private void paintEdge(Component c, Graphics g, int x, int y, int width, int height, int tileW, int tileH) {
|
|
148 |
g = g.create(x, y, width, height);
|
|
149 |
int sY = -(y % tileH);
|
|
150 |
for (x = -(x % tileW); x < width; x += tileW) {
|
|
151 |
for (y = sY; y < height; y += tileH) {
|
|
152 |
this.tileIcon.paintIcon(c, g, x, y);
|
|
153 |
}
|
|
154 |
}
|
|
155 |
g.dispose();
|
|
156 |
}
|
|
157 |
|
2
|
158 |
/**
|
|
159 |
* Reinitialize the insets parameter with this Border's current Insets.
|
|
160 |
* @param c the component for which this border insets value applies
|
|
161 |
* @param insets the object to be reinitialized
|
|
162 |
* @since 1.3
|
|
163 |
*/
|
|
164 |
public Insets getBorderInsets(Component c, Insets insets) {
|
|
165 |
return computeInsets(insets);
|
|
166 |
}
|
|
167 |
|
|
168 |
/**
|
|
169 |
* Returns the insets of the border.
|
|
170 |
* @since 1.3
|
|
171 |
*/
|
|
172 |
public Insets getBorderInsets() {
|
|
173 |
return computeInsets(new Insets(0,0,0,0));
|
|
174 |
}
|
|
175 |
|
|
176 |
/* should be protected once api changes area allowed */
|
|
177 |
private Insets computeInsets(Insets insets) {
|
|
178 |
if (tileIcon != null && top == -1 && bottom == -1 &&
|
|
179 |
left == -1 && right == -1) {
|
|
180 |
int w = tileIcon.getIconWidth();
|
|
181 |
int h = tileIcon.getIconHeight();
|
|
182 |
insets.top = h;
|
|
183 |
insets.right = w;
|
|
184 |
insets.bottom = h;
|
|
185 |
insets.left = w;
|
|
186 |
} else {
|
|
187 |
insets.left = left;
|
|
188 |
insets.top = top;
|
|
189 |
insets.right = right;
|
|
190 |
insets.bottom = bottom;
|
|
191 |
}
|
|
192 |
return insets;
|
|
193 |
}
|
|
194 |
|
|
195 |
/**
|
|
196 |
* Returns the color used for tiling the border or null
|
|
197 |
* if a tile icon is being used.
|
|
198 |
* @since 1.3
|
|
199 |
*/
|
|
200 |
public Color getMatteColor() {
|
|
201 |
return color;
|
|
202 |
}
|
|
203 |
|
|
204 |
/**
|
|
205 |
* Returns the icon used for tiling the border or null
|
|
206 |
* if a solid color is being used.
|
|
207 |
* @since 1.3
|
|
208 |
*/
|
|
209 |
public Icon getTileIcon() {
|
|
210 |
return tileIcon;
|
|
211 |
}
|
|
212 |
|
|
213 |
/**
|
|
214 |
* Returns whether or not the border is opaque.
|
|
215 |
*/
|
|
216 |
public boolean isBorderOpaque() {
|
|
217 |
// If a tileIcon is set, then it may contain transparent bits
|
|
218 |
return color != null;
|
|
219 |
}
|
|
220 |
|
|
221 |
}
|