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.Color ; 13 import java.awt.Component ; 14 15 16 31 public class BevelBorder extends AbstractBorder 32 { 33 34 public static final int RAISED = 0; 35 36 public static final int LOWERED = 1; 37 38 protected int bevelType; 39 protected Color highlightOuter; 40 protected Color highlightInner; 41 protected Color shadowInner; 42 protected Color shadowOuter; 43 44 50 public BevelBorder(int bevelType) { 51 this.bevelType = bevelType; 52 } 53 54 61 public BevelBorder(int bevelType, Color highlight, Color shadow) { 62 this(bevelType, highlight.brighter(), highlight, shadow, shadow.brighter()); 63 } 64 65 78 public BevelBorder(int bevelType, Color highlightOuterColor, 79 Color highlightInnerColor, Color shadowOuterColor, 80 Color shadowInnerColor) { 81 this(bevelType); 82 this.highlightOuter = highlightOuterColor; 83 this.highlightInner = highlightInnerColor; 84 this.shadowOuter = shadowOuterColor; 85 this.shadowInner = shadowInnerColor; 86 } 87 88 98 public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) { 99 if (bevelType == RAISED) { 100 paintRaisedBevel(c, g, x, y, width, height); 101 102 } else if (bevelType == LOWERED) { 103 paintLoweredBevel(c, g, x, y, width, height); 104 } 105 } 106 107 111 public Insets getBorderInsets(Component c) { 112 return new Insets (2, 2, 2, 2); 113 } 114 115 120 public Insets getBorderInsets(Component c, Insets insets) { 121 insets.left = insets.top = insets.right = insets.bottom = 2; 122 return insets; 123 } 124 125 132 public Color getHighlightOuterColor(Component c) { 133 Color highlight = getHighlightOuterColor(); 134 return highlight != null? highlight : 135 c.getBackground().brighter().brighter(); 136 } 137 138 145 public Color getHighlightInnerColor(Component c) { 146 Color highlight = getHighlightInnerColor(); 147 return highlight != null? highlight : 148 c.getBackground().brighter(); 149 } 150 151 158 public Color getShadowInnerColor(Component c) { 159 Color shadow = getShadowInnerColor(); 160 return shadow != null? shadow : 161 c.getBackground().darker(); 162 } 163 164 171 public Color getShadowOuterColor(Component c) { 172 Color shadow = getShadowOuterColor(); 173 return shadow != null? shadow : 174 c.getBackground().darker().darker(); 175 } 176 177 182 public Color getHighlightOuterColor() { 183 return highlightOuter; 184 } 185 186 191 public Color getHighlightInnerColor() { 192 return highlightInner; 193 } 194 195 200 public Color getShadowInnerColor() { 201 return shadowInner; 202 } 203 204 209 public Color getShadowOuterColor() { 210 return shadowOuter; 211 } 212 213 216 public int getBevelType() { 217 return bevelType; 218 } 219 220 223 public boolean isBorderOpaque() { return true; } 224 225 protected void paintRaisedBevel(Component c, Graphics g, int x, int y, 226 int width, int height) { 227 Color oldColor = g.getColor(); 228 int h = height; 229 int w = width; 230 231 g.translate(x, y); 232 233 g.setColor(getHighlightOuterColor(c)); 234 g.drawLine(0, 0, 0, h-2); 235 g.drawLine(1, 0, w-2, 0); 236 237 g.setColor(getHighlightInnerColor(c)); 238 g.drawLine(1, 1, 1, h-3); 239 g.drawLine(2, 1, w-3, 1); 240 241 g.setColor(getShadowOuterColor(c)); 242 g.drawLine(0, h-1, w-1, h-1); 243 g.drawLine(w-1, 0, w-1, h-2); 244 245 g.setColor(getShadowInnerColor(c)); 246 g.drawLine(1, h-2, w-2, h-2); 247 g.drawLine(w-2, 1, w-2, h-3); 248 249 g.translate(-x, -y); 250 g.setColor(oldColor); 251 252 } 253 254 protected void paintLoweredBevel(Component c, Graphics g, int x, int y, 255 int width, int height) { 256 Color oldColor = g.getColor(); 257 int h = height; 258 int w = width; 259 260 g.translate(x, y); 261 262 g.setColor(getShadowInnerColor(c)); 263 g.drawLine(0, 0, 0, h-1); 264 g.drawLine(1, 0, w-1, 0); 265 266 g.setColor(getShadowOuterColor(c)); 267 g.drawLine(1, 1, 1, h-2); 268 g.drawLine(2, 1, w-2, 1); 269 270 g.setColor(getHighlightOuterColor(c)); 271 g.drawLine(1, h-1, w-1, h-1); 272 g.drawLine(w-1, 1, w-1, h-2); 273 274 g.setColor(getHighlightInnerColor(c)); 275 g.drawLine(2, h-2, w-2, h-2); 276 g.drawLine(w-2, 2, w-2, h-3); 277 278 g.translate(-x, -y); 279 g.setColor(oldColor); 280 281 } 282 283 } 284 | Popular Tags |