1 /*
2 * Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved.
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. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle 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 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.
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™
44 * has been added to the {@code java.beans} package.
45 * Please see {@link java.beans.XMLEncoder}.
46 *
47 * @author Amy Fowler
48 */
49 @SuppressWarnings("serial")
50 public class MatteBorder extends EmptyBorder
51 {
52 /**
53 * The color rendered for the border.
54 */
55 protected Color color;
56 /**
57 * The icon to be used for tiling the border.
58 */
59 protected Icon tileIcon;
60
61 /**
62 * Creates a matte border with the specified insets and color.
63 * @param top the top inset of the border
64 * @param left the left inset of the border
65 * @param bottom the bottom inset of the border
66 * @param right the right inset of the border
67 * @param matteColor the color rendered for the border
68 */
69 public MatteBorder(int top, int left, int bottom, int right, Color matteColor) {
70 super(top, left, bottom, right);
71 this.color = matteColor;
72 }
73
74 /**
75 * Creates a matte border with the specified insets and color.
76 * @param borderInsets the insets of the border
77 * @param matteColor the color rendered for the border
78 * @since 1.3
79 */
80 public MatteBorder(Insets borderInsets, Color matteColor) {
81 super(borderInsets);
82 this.color = matteColor;
83 }
84
85 /**
86 * Creates a matte border with the specified insets and tile icon.
87 * @param top the top inset of the border
88 * @param left the left inset of the border
89 * @param bottom the bottom inset of the border
90 * @param right the right inset of the border
91 * @param tileIcon the icon to be used for tiling the border
92 */
93 public MatteBorder(int top, int left, int bottom, int right, Icon tileIcon) {
94 super(top, left, bottom, right);
95 this.tileIcon = tileIcon;
96 }
97
98 /**
99 * Creates a matte border with the specified insets and tile icon.
100 * @param borderInsets the insets of the border
101 * @param tileIcon the icon to be used for tiling the border
102 * @since 1.3
103 */
104 public MatteBorder(Insets borderInsets, Icon tileIcon) {
105 super(borderInsets);
106 this.tileIcon = tileIcon;
107 }
108
109 /**
110 * Creates a matte border with the specified tile icon. The
111 * insets will be calculated dynamically based on the size of
112 * the tile icon, where the top and bottom will be equal to the
113 * tile icon's height, and the left and right will be equal to
114 * the tile icon's width.
115 * @param tileIcon the icon to be used for tiling the border
116 */
117 public MatteBorder(Icon tileIcon) {
118 this(-1,-1,-1,-1, tileIcon);
119 }
120
121 /**
122 * Paints the matte border.
123 */
124 public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
125 Insets insets = getBorderInsets(c);
126 Color oldColor = g.getColor();
127 g.translate(x, y);
128
129 // If the tileIcon failed loading, paint as gray.
130 if (tileIcon != null) {
131 color = (tileIcon.getIconWidth() == -1) ? Color.gray : null;
132 }
133
134 if (color != null) {
135 g.setColor(color);
136 g.fillRect(0, 0, width - insets.right, insets.top);
137 g.fillRect(0, insets.top, insets.left, height - insets.top);
138 g.fillRect(insets.left, height - insets.bottom, width - insets.left, insets.bottom);
139 g.fillRect(width - insets.right, 0, insets.right, height - insets.bottom);
140
141 } else if (tileIcon != null) {
142 int tileW = tileIcon.getIconWidth();
143 int tileH = tileIcon.getIconHeight();
144 paintEdge(c, g, 0, 0, width - insets.right, insets.top, tileW, tileH);
145 paintEdge(c, g, 0, insets.top, insets.left, height - insets.top, tileW, tileH);
146 paintEdge(c, g, insets.left, height - insets.bottom, width - insets.left, insets.bottom, tileW, tileH);
147 paintEdge(c, g, width - insets.right, 0, insets.right, height - insets.bottom, tileW, tileH);
148 }
149 g.translate(-x, -y);
150 g.setColor(oldColor);
151
152 }
153
154 private void paintEdge(Component c, Graphics g, int x, int y, int width, int height, int tileW, int tileH) {
155 g = g.create(x, y, width, height);
156 int sY = -(y % tileH);
157 for (x = -(x % tileW); x < width; x += tileW) {
158 for (y = sY; y < height; y += tileH) {
159 this.tileIcon.paintIcon(c, g, x, y);
160 }
161 }
162 g.dispose();
163 }
164
165 /**
166 * Reinitialize the insets parameter with this Border's current Insets.
167 * @param c the component for which this border insets value applies
168 * @param insets the object to be reinitialized
169 * @since 1.3
170 */
171 public Insets getBorderInsets(Component c, Insets insets) {
172 return computeInsets(insets);
173 }
174
175 /**
176 * Returns the insets of the border.
177 * @since 1.3
178 */
179 public Insets getBorderInsets() {
180 return computeInsets(new Insets(0,0,0,0));
181 }
182
183 /* should be protected once api changes area allowed */
184 private Insets computeInsets(Insets insets) {
185 if (tileIcon != null && top == -1 && bottom == -1 &&
186 left == -1 && right == -1) {
187 int w = tileIcon.getIconWidth();
188 int h = tileIcon.getIconHeight();
189 insets.top = h;
190 insets.right = w;
191 insets.bottom = h;
192 insets.left = w;
193 } else {
194 insets.left = left;
195 insets.top = top;
196 insets.right = right;
197 insets.bottom = bottom;
198 }
199 return insets;
200 }
201
202 /**
203 * Returns the color used for tiling the border or null
204 * if a tile icon is being used.
205 *
206 * @return the {@code Color} object used to render the border or {@code null}
207 * if a tile icon is used
208 * @since 1.3
209 */
210 public Color getMatteColor() {
211 return color;
212 }
213
214 /**
215 * Returns the icon used for tiling the border or null
216 * if a solid color is being used.
217 *
218 * @return the {@code Icon} used to tile the border or {@code null} if a
219 * solid color is used to fill the border
220 * @since 1.3
221 */
222 public Icon getTileIcon() {
223 return tileIcon;
224 }
225
226 /**
227 * Returns whether or not the border is opaque.
228 *
229 * @return {@code true} if the border is opaque, {@code false} otherwise
230 */
231 public boolean isBorderOpaque() {
232 // If a tileIcon is set, then it may contain transparent bits
233 return color != null;
234 }
235
236 }
--- EOF ---