1 7 8 package org.jdesktop.swing; 9 10 import java.awt.Color ; 11 import java.awt.Component ; 12 import java.awt.Graphics ; 13 import java.awt.Insets ; 14 import java.awt.Point ; 15 import javax.swing.Icon ; 16 import javax.swing.border.MatteBorder ; 17 18 23 24 public class MatteBorderExt extends MatteBorder { 25 protected Icon [] tileIcons = null; 26 private Icon defaultIcon = null; 27 28 46 public MatteBorderExt(int top, int left, int bottom, int right, 47 Icon [] tileIcons) { 48 super(top, left, bottom, right, 49 (tileIcons == null) || (tileIcons.length == 0) ? null : 50 tileIcons[0]); 51 this.tileIcons = tileIcons; 52 } 53 54 57 public MatteBorderExt(int top, int left, int bottom, int right, 58 Color matteColor) { 59 super(top, left, bottom, right, matteColor); 60 } 61 62 65 public MatteBorderExt(Insets borderInsets, Color matteColor) { 66 super(borderInsets, matteColor); 67 } 68 69 72 public MatteBorderExt(int top, int left, int bottom, int right, 73 Icon tileIcon) { 74 super(top, left, bottom, right, tileIcon); 75 } 76 77 80 public MatteBorderExt(Insets borderInsets, Icon tileIcon) { 81 super(borderInsets, tileIcon); 82 } 83 84 87 public MatteBorderExt(Icon tileIcon) { 88 super(tileIcon); 89 } 90 91 96 public Icon [] getTileIcons() { 97 return tileIcons; 98 } 99 100 103 public void paintBorder(Component c, Graphics 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 insets = getBorderInsets(c); 111 int clipWidth, clipHeight; 112 113 clipWidth = Math.min(width, insets.left); clipHeight = Math.min(height, insets.top); 116 if ( (clipWidth <= 0) || (clipHeight <= 0)) { 117 return; } 119 120 122 Color oldColor = g.getColor(); g.translate(x, y); 125 for (int i = 0; i < tileIcons.length; i++) { 126 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); g.setColor(oldColor); 144 } 145 146 protected void paint(Icon icon, Component c, Graphics g, int x, int y, 147 int width, int height) { 148 Graphics 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 164 protected void paintTopLeft(Component c, Graphics g, int x, int y, int width, int height) { 165 Graphics cg = g.create(); 166 cg.setClip(x, y, width, height); 167 tileIcons[0].paintIcon(c, cg, x, y); 168 cg.dispose(); 169 } 170 171 174 protected void paintTop(Component c, Graphics g, int x, int y, int width, int height) { 175 paint(tileIcons[1], c, g, x, y, width, height); 176 } 177 178 181 protected void paintTopRight(Component c, Graphics 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 icon = tileIcons[0]; 187 188 } 189 } 190 191 194 protected void paintRight(Component c, Graphics 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 icon = tileIcons[1]; 200 201 } 202 } 203 204 207 protected void paintBottomRight(Component c, Graphics 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 icon = tileIcons[0]; 213 214 } 215 } 216 217 220 protected void paintBottom(Component c, Graphics 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 icon = tileIcons[1]; 226 227 } 228 } 229 230 233 protected void paintBottomLeft(Component c, Graphics 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 icon = tileIcons[0]; 239 240 } 241 } 242 243 246 protected void paintLeft(Component c, Graphics 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 icon = tileIcons[1]; 252 253 } 254 } 255 256 259 protected Icon getDefaultIcon() { 260 if (defaultIcon == null) { 261 defaultIcon = new Icon () { 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 c, Graphics g, int x, int y) { 274 g.setColor(c.getBackground().darker().darker()); 275 g.fillRect(x, y, width, height); 277 } 278 }; 279 } 280 return defaultIcon; 281 } 282 } | Popular Tags |