KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > swing > MatteBorderExt


1 /*
2  * $Id: MatteBorderExt.java,v 1.1.1.1 2004/06/16 01:43:39 davidson1 Exp $
3  *
4  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
5  * Santa Clara, California 95054, U.S.A. All rights reserved.
6  */

7
8 package org.jdesktop.swing;
9
10 import java.awt.Color JavaDoc;
11 import java.awt.Component JavaDoc;
12 import java.awt.Graphics JavaDoc;
13 import java.awt.Insets JavaDoc;
14 import java.awt.Point JavaDoc;
15 import javax.swing.Icon JavaDoc;
16 import javax.swing.border.MatteBorder JavaDoc;
17
18 /**
19  * Matte border that allows specialized icons for corners and sides.
20  *
21  * @author Ramesh Gupta
22  */

23
24 public class MatteBorderExt extends MatteBorder JavaDoc {
25     protected Icon JavaDoc[] tileIcons = null;
26     private Icon JavaDoc defaultIcon = null;
27
28     /**
29      * Draws a matte border using specialized icons for corners and sides. If
30          * tileIcons is null, or if the length of tileIcons array is less than 2, this
31          * defaults to the {@link javax.swing.border.MatteBorder superclass} behavior.
32          * Otherwise, tileIcons must specify icons in clockwise order, starting with
33          * the top-left icon at index zero, culminating with the left icon at index 7.
34      * If the length of the tileIcons array is greater than 1, but less than 8,
35      * then tileIcons[0] is used to paint the corners, and tileIcons[1] is used
36          * to paint the sides, with icons rotated as necessary. Other icons, if any,
37      * are ignored.
38      *
39      * @param top top inset
40      * @param left left inset
41      * @param bottom bottom inset
42      * @param right right inset
43      * @param tileIcons array of icons starting with top-left in index 0,
44      * continuing clockwise through the rest of the indices
45      */

46     public MatteBorderExt(int top, int left, int bottom, int right,
47                           Icon JavaDoc[] tileIcons) {
48         super(top, left, bottom, right,
49               (tileIcons == null) || (tileIcons.length == 0) ? null :
50               tileIcons[0]);
51         this.tileIcons = tileIcons;
52     }
53
54     /**
55      * {@inheritDoc}
56      */

57     public MatteBorderExt(int top, int left, int bottom, int right,
58                           Color JavaDoc matteColor) {
59         super(top, left, bottom, right, matteColor);
60     }
61
62     /**
63      * {@inheritDoc}
64      */

65     public MatteBorderExt(Insets JavaDoc borderInsets, Color JavaDoc matteColor) {
66         super(borderInsets, matteColor);
67     }
68
69     /**
70      * {@inheritDoc}
71      */

72     public MatteBorderExt(int top, int left, int bottom, int right,
73                           Icon JavaDoc tileIcon) {
74         super(top, left, bottom, right, tileIcon);
75     }
76
77     /**
78      * {@inheritDoc}
79      */

80     public MatteBorderExt(Insets JavaDoc borderInsets, Icon JavaDoc tileIcon) {
81         super(borderInsets, tileIcon);
82     }
83
84     /**
85      * {@inheritDoc}
86      */

87     public MatteBorderExt(Icon JavaDoc tileIcon) {
88         super(tileIcon);
89     }
90
91     /**
92      * Returns the icons used by this border
93      *
94      * @return the icons used by this border
95      */

96     public Icon JavaDoc[] getTileIcons() {
97         return tileIcons;
98     }
99
100     /**
101      * {@inheritDoc}
102      */

103     public void paintBorder(Component JavaDoc c, Graphics JavaDoc g, int x, int y,
104                             int width, int height) {
105         if ( (tileIcons == null) || (tileIcons.length < 2)) {
106             super.paintBorder(c, g, x, y, width, height);
107             return;
108         }
109
110         Insets JavaDoc insets = getBorderInsets(c);
111         int clipWidth, clipHeight;
112
113         clipWidth = Math.min(width, insets.left); // clip to component width or insets
114
clipHeight = Math.min(height, insets.top); // clip to component height or insets
115

116         if ( (clipWidth <= 0) || (clipHeight <= 0)) {
117             return; // nothing to paint
118
}
119
120         // We now know that we have at least two icons!
121

122         Color JavaDoc oldColor = g.getColor(); // restore before exiting
123
g.translate(x, y); // restore before exiting
124

125         for (int i = 0; i < tileIcons.length; i++) {
126             // Make sure we have an icon to paint with
127
if (tileIcons[i] == null) {
128                 tileIcons[i] = getDefaultIcon();
129             }
130         }
131
132         paintTopLeft(c, g, 0, 0, insets.left, insets.top);
133         paintTop(c, g, insets.left, 0, width - insets.left - insets.right, insets.top);
134         paintTopRight(c, g, width - insets.right, 0, insets.right, insets.top);
135         paintRight(c, g, width - insets.right, insets.top, insets.right, height - insets.top - insets.bottom);
136         paintBottomRight(c, g, width - insets.right, height - insets.bottom, insets.right, insets.bottom);
137         paintBottom(c, g, insets.left, height - insets.bottom, width - insets.left - insets.right, insets.bottom);
138         paintBottomLeft(c, g, 0, height - insets.bottom, insets.left, insets.bottom);
139         paintLeft(c, g, 0, insets.top, insets.left, height - insets.top - insets.bottom);
140
141         g.translate( -x, -y); // restore
142
g.setColor(oldColor); // restore
143

144     }
145
146     protected void paint(Icon JavaDoc icon, Component JavaDoc c, Graphics JavaDoc g, int x, int y,
147                              int width, int height) {
148         Graphics JavaDoc cg = g.create();
149         cg.setClip(x, y, width, height);
150         int tileW = icon.getIconWidth();
151         int tileH = icon.getIconHeight();
152         int xpos, ypos, startx, starty;
153         for (ypos = 0; height - ypos > 0; ypos += tileH) {
154             for (xpos = 0; width - xpos > 0; xpos += tileW) {
155                 icon.paintIcon(c, cg, x+xpos, y+ypos);
156             }
157         }
158         cg.dispose();
159     }
160
161     /**
162      * Only called by paintBorder()
163      */

164     protected void paintTopLeft(Component JavaDoc c, Graphics JavaDoc g, int x, int y, int width, int height) {
165         Graphics JavaDoc cg = g.create();
166         cg.setClip(x, y, width, height);
167         tileIcons[0].paintIcon(c, cg, x, y);
168         cg.dispose();
169     }
170
171     /**
172      * Only called by paintBorder()
173      */

174     protected void paintTop(Component JavaDoc c, Graphics JavaDoc g, int x, int y, int width, int height) {
175         paint(tileIcons[1], c, g, x, y, width, height);
176     }
177
178     /**
179      * Only called by paintBorder()
180      */

181     protected void paintTopRight(Component JavaDoc c, Graphics JavaDoc g, int x, int y, int width, int height) {
182         if (tileIcons.length == 8) {
183             paint(tileIcons[2], c, g, x, y, width, height);
184         }
185         else {
186             Icon JavaDoc icon = tileIcons[0];
187             /** @todo Rotate -90 and paint icon */
188         }
189     }
190
191     /**
192      * Only called by paintBorder()
193      */

194     protected void paintRight(Component JavaDoc c, Graphics JavaDoc g, int x, int y, int width, int height) {
195         if (tileIcons.length == 8) {
196             paint(tileIcons[3], c, g, x, y, width, height);
197         }
198         else {
199             Icon JavaDoc icon = tileIcons[1];
200             /** @todo Rotate -90 and paint icon */
201         }
202     }
203
204     /**
205      * Only called by paintBorder()
206      */

207     protected void paintBottomRight(Component JavaDoc c, Graphics JavaDoc g, int x, int y, int width, int height) {
208         if (tileIcons.length == 8) {
209             paint(tileIcons[4], c, g, x, y, width, height);
210         }
211         else {
212             Icon JavaDoc icon = tileIcons[0];
213             /** @todo Rotate -180 and paint icon */
214         }
215     }
216
217     /**
218      * Only called by paintBorder()
219      */

220     protected void paintBottom(Component JavaDoc c, Graphics JavaDoc g, int x, int y, int width, int height) {
221         if (tileIcons.length == 8) {
222             paint(tileIcons[5], c, g, x, y, width, height);
223         }
224         else {
225             Icon JavaDoc icon = tileIcons[1];
226             /** @todo Rotate -180 and paint icon */
227         }
228     }
229
230     /**
231      * Only called by paintBorder()
232      */

233     protected void paintBottomLeft(Component JavaDoc c, Graphics JavaDoc g, int x, int y, int width, int height) {
234         if (tileIcons.length == 8) {
235             paint(tileIcons[6], c, g, x, y, width, height);
236         }
237         else {
238             Icon JavaDoc icon = tileIcons[0];
239             /** @todo Rotate -270 and paint icon */
240         }
241     }
242
243     /**
244      * Only called by paintBorder()
245      */

246     protected void paintLeft(Component JavaDoc c, Graphics JavaDoc g, int x, int y, int width, int height) {
247         if (tileIcons.length == 8) {
248             paint(tileIcons[7], c, g, x, y, width, height);
249         }
250         else {
251             Icon JavaDoc icon = tileIcons[1];
252             /** @todo Rotate -270 and paint icon */
253         }
254     }
255
256     /**
257      * Only called by paintBorder()
258      */

259     protected Icon JavaDoc getDefaultIcon() {
260         if (defaultIcon == null) {
261             defaultIcon = new Icon JavaDoc() {
262                 private int width = 3;
263                 private int height = 3;
264
265                 public int getIconWidth() {
266                     return width;
267                 }
268
269                 public int getIconHeight() {
270                     return height;
271                 }
272
273                 public void paintIcon(Component JavaDoc c, Graphics JavaDoc g, int x, int y) {
274                     g.setColor(c.getBackground().darker().darker());
275                     //g.translate(x, y);
276
g.fillRect(x, y, width, height);
277                 }
278             };
279         }
280         return defaultIcon;
281     }
282 }
Popular Tags