1 7 package javax.swing.border; 8 9 import java.awt.Graphics ; 10 import java.awt.Insets ; 11 import java.awt.Rectangle ; 12 import java.awt.Component ; 13 import java.awt.Color ; 14 15 import javax.swing.Icon ; 16 17 33 public class MatteBorder extends EmptyBorder 34 { 35 protected Color color; 36 protected Icon tileIcon; 37 38 46 public MatteBorder(int top, int left, int bottom, int right, Color matteColor) { 47 super(top, left, bottom, right); 48 this.color = matteColor; 49 } 50 51 56 public MatteBorder(Insets borderInsets, Color matteColor) { 57 super(borderInsets); 58 this.color = matteColor; 59 } 60 61 69 public MatteBorder(int top, int left, int bottom, int right, Icon tileIcon) { 70 super(top, left, bottom, right); 71 this.tileIcon = tileIcon; 72 } 73 74 79 public MatteBorder(Insets borderInsets, Icon tileIcon) { 80 super(borderInsets); 81 this.tileIcon = tileIcon; 82 } 83 84 92 public MatteBorder(Icon tileIcon) { 93 this(-1,-1,-1,-1, tileIcon); 94 } 95 96 99 public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) { 100 Insets insets = getBorderInsets(c); 101 Color oldColor = g.getColor(); 102 g.translate(x, y); 103 104 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 cg; 122 123 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 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 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 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 178 public Insets getBorderInsets(Component c) { 179 return getBorderInsets(); 180 } 181 182 187 public Insets getBorderInsets(Component c, Insets insets) { 188 return computeInsets(insets); 189 } 190 191 194 public Insets getBorderInsets() { 195 return computeInsets(new Insets (0,0,0,0)); 196 } 197 198 199 private Insets computeInsets(Insets 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 221 public Color getMatteColor() { 222 return color; 223 } 224 225 229 public Icon getTileIcon() { 230 return tileIcon; 231 } 232 233 236 public boolean isBorderOpaque() { 237 return color != null; 239 } 240 241 } 242 | Popular Tags |