KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > border > MatteBorder


1 /*
2  * @(#)MatteBorder.java 1.22 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7 package javax.swing.border;
8
9 import java.awt.Graphics JavaDoc;
10 import java.awt.Insets JavaDoc;
11 import java.awt.Rectangle JavaDoc;
12 import java.awt.Component JavaDoc;
13 import java.awt.Color JavaDoc;
14
15 import javax.swing.Icon JavaDoc;
16
17 /**
18  * A class which provides a matte-like border of either a solid color
19  * or a tiled icon.
20  * <p>
21  * <strong>Warning:</strong>
22  * Serialized objects of this class will not be compatible with
23  * future Swing releases. The current serialization support is
24  * appropriate for short term storage or RMI between applications running
25  * the same version of Swing. As of 1.4, support for long term storage
26  * of all JavaBeans<sup><font size="-2">TM</font></sup>
27  * has been added to the <code>java.beans</code> package.
28  * Please see {@link java.beans.XMLEncoder}.
29  *
30  * @version 1.22 12/19/03
31  * @author Amy Fowler
32  */

33 public class MatteBorder extends EmptyBorder JavaDoc
34 {
35     protected Color JavaDoc color;
36     protected Icon JavaDoc tileIcon;
37
38     /**
39      * Creates a matte border with the specified insets and color.
40      * @param top the top inset of the border
41      * @param left the left inset of the border
42      * @param bottom the bottom inset of the border
43      * @param right the right inset of the border
44      * @param matteColor the color rendered for the border
45      */

46     public MatteBorder(int top, int left, int bottom, int right, Color JavaDoc matteColor) {
47         super(top, left, bottom, right);
48         this.color = matteColor;
49     }
50
51     /**
52      * Creates a matte border with the specified insets and color.
53      * @param borderInsets the insets of the border
54      * @param matteColor the color rendered for the border
55      */

56     public MatteBorder(Insets JavaDoc borderInsets, Color JavaDoc matteColor) {
57         super(borderInsets);
58         this.color = matteColor;
59     }
60
61     /**
62      * Creates a matte border with the specified insets and tile icon.
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 tileIcon the icon to be used for tiling the border
68      */

69     public MatteBorder(int top, int left, int bottom, int right, Icon JavaDoc tileIcon) {
70         super(top, left, bottom, right);
71         this.tileIcon = tileIcon;
72     }
73
74     /**
75      * Creates a matte border with the specified insets and tile icon.
76      * @param borderInsets the insets of the border
77      * @param tileIcon the icon to be used for tiling the border
78      */

79     public MatteBorder(Insets JavaDoc borderInsets, Icon JavaDoc tileIcon) {
80         super(borderInsets);
81         this.tileIcon = tileIcon;
82     }
83
84     /**
85      * Creates a matte border with the specified tile icon. The
86      * insets will be calculated dynamically based on the size of
87      * the tile icon, where the top and bottom will be equal to the
88      * tile icon's height, and the left and right will be equal to
89      * the tile icon's width.
90      * @param tileIcon the icon to be used for tiling the border
91      */

92     public MatteBorder(Icon JavaDoc tileIcon) {
93         this(-1,-1,-1,-1, tileIcon);
94     }
95
96     /**
97      * Paints the matte border.
98      */

99     public void paintBorder(Component JavaDoc c, Graphics JavaDoc g, int x, int y, int width, int height) {
100         Insets JavaDoc insets = getBorderInsets(c);
101         Color JavaDoc oldColor = g.getColor();
102         g.translate(x, y);
103
104         // If the tileIcon failed loading, paint as gray.
105
if (tileIcon != null) {
106             color = (tileIcon.getIconWidth() == -1) ? Color.gray : null;
107         }
108
109         if (color != null) {
110             g.setColor(color);
111             g.fillRect(0, 0, width - insets.right, insets.top);
112             g.fillRect(0, insets.top, insets.left, height - insets.top);
113             g.fillRect(insets.left, height - insets.bottom, width - insets.left, insets.bottom);
114             g.fillRect(width - insets.right, 0, insets.right, height - insets.bottom);
115
116         } else if (tileIcon != null) {
117
118             int tileW = tileIcon.getIconWidth();
119             int tileH = tileIcon.getIconHeight();
120             int xpos, ypos, startx, starty;
121             Graphics JavaDoc cg;
122
123             // Paint top matte edge
124
cg = g.create();
125             cg.setClip(0, 0, width, insets.top);
126             for (ypos = 0; insets.top - ypos > 0; ypos += tileH) {
127                 for (xpos = 0; width - xpos > 0; xpos += tileW) {
128                     tileIcon.paintIcon(c, cg, xpos, ypos);
129                 }
130             }
131             cg.dispose();
132
133             // Paint left matte edge
134
cg = g.create();
135             cg.setClip(0, insets.top, insets.left, height - insets.top);
136             starty = insets.top - (insets.top%tileH);
137             startx = 0;
138             for (ypos = starty; height - ypos > 0; ypos += tileH) {
139                 for (xpos = startx; insets.left - xpos > 0; xpos += tileW) {
140                     tileIcon.paintIcon(c, cg, xpos, ypos);
141                 }
142             }
143             cg.dispose();
144
145             // Paint bottom matte edge
146
cg = g.create();
147             cg.setClip(insets.left, height - insets.bottom, width - insets.left, insets.bottom);
148             starty = (height - insets.bottom) - ((height - insets.bottom)%tileH);
149             startx = insets.left - (insets.left%tileW);
150             for (ypos = starty; height - ypos > 0; ypos += tileH) {
151                 for (xpos = startx; width - xpos > 0; xpos += tileW) {
152                     tileIcon.paintIcon(c, cg, xpos, ypos);
153                 }
154             }
155             cg.dispose();
156
157             // Paint right matte edge
158
cg = g.create();
159             cg.setClip(width - insets.right, insets.top, insets.right, height - insets.top - insets.bottom);
160             starty = insets.top - (insets.top%tileH);
161             startx = width - insets.right - ((width - insets.right)%tileW);
162             for (ypos = starty; height - ypos > 0; ypos += tileH) {
163                 for (xpos = startx; width - xpos > 0; xpos += tileW) {
164                     tileIcon.paintIcon(c, cg, xpos, ypos);
165                 }
166             }
167             cg.dispose();
168         }
169         g.translate(-x, -y);
170         g.setColor(oldColor);
171
172     }
173
174     /**
175      * Returns the insets of the border.
176      * @param c the component for which this border insets value applies
177      */

178     public Insets JavaDoc getBorderInsets(Component JavaDoc c) {
179         return getBorderInsets();
180     }
181
182     /**
183      * Reinitialize the insets parameter with this Border's current Insets.
184      * @param c the component for which this border insets value applies
185      * @param insets the object to be reinitialized
186      */

187     public Insets JavaDoc getBorderInsets(Component JavaDoc c, Insets JavaDoc insets) {
188         return computeInsets(insets);
189     }
190
191     /**
192      * Returns the insets of the border.
193      */

194     public Insets JavaDoc getBorderInsets() {
195         return computeInsets(new Insets JavaDoc(0,0,0,0));
196     }
197
198     /* should be protected once api changes area allowed */
199     private Insets JavaDoc computeInsets(Insets JavaDoc insets) {
200         if (tileIcon != null && top == -1 && bottom == -1 &&
201             left == -1 && right == -1) {
202             int w = tileIcon.getIconWidth();
203             int h = tileIcon.getIconHeight();
204             insets.top = h;
205             insets.right = w;
206             insets.bottom = h;
207             insets.left = w;
208         } else {
209             insets.left = left;
210             insets.top = top;
211             insets.right = right;
212             insets.bottom = bottom;
213         }
214         return insets;
215     }
216
217     /**
218      * Returns the color used for tiling the border or null
219      * if a tile icon is being used.
220      */

221     public Color JavaDoc getMatteColor() {
222         return color;
223     }
224
225    /**
226      * Returns the icon used for tiling the border or null
227      * if a solid color is being used.
228      */

229     public Icon JavaDoc getTileIcon() {
230         return tileIcon;
231     }
232
233     /**
234      * Returns whether or not the border is opaque.
235      */

236     public boolean isBorderOpaque() {
237         // If a tileIcon is set, then it may contain transparent bits
238
return color != null;
239     }
240
241 }
242
Popular Tags