1 30 31 package com.jgoodies.looks.plastic; 32 33 import java.awt.Color ; 34 import java.awt.Graphics ; 35 import java.awt.Rectangle ; 36 37 import javax.swing.ButtonModel ; 38 import javax.swing.UIManager ; 39 import javax.swing.plaf.metal.MetalScrollButton ; 40 41 47 class PlasticArrowButton extends MetalScrollButton { 48 49 private static Color shadowColor; 50 private static Color highlightColor; 51 52 protected boolean isFreeStanding; 53 54 55 public PlasticArrowButton(int direction, int width, boolean freeStanding) { 56 super(direction, width, freeStanding); 57 shadowColor = UIManager.getColor("ScrollBar.darkShadow"); 58 highlightColor = UIManager.getColor("ScrollBar.highlight"); 59 isFreeStanding = freeStanding; 60 } 61 62 63 public void setFreeStanding(boolean freeStanding) { 64 super.setFreeStanding(freeStanding); 65 isFreeStanding = freeStanding; 66 } 67 68 69 public void paint(Graphics g) { 70 boolean leftToRight = PlasticUtils.isLeftToRight(this); 71 boolean isEnabled = getParent().isEnabled(); 72 boolean isPressed = getModel().isPressed(); 73 74 Color arrowColor = isEnabled 75 ? PlasticLookAndFeel.getControlInfo() 76 : PlasticLookAndFeel.getControlDisabled(); 77 int width = getWidth(); 78 int height = getHeight(); 79 int w = width; 80 int h = height; 81 int arrowHeight = calculateArrowHeight(height, width); 82 int arrowOffset = calculateArrowOffset(); 83 boolean paintNorthBottom = isPaintingNorthBottom(); 84 85 g.setColor(isPressed ? PlasticLookAndFeel.getControlShadow() : getBackground()); 86 g.fillRect(0, 0, width, height); 87 88 if (getDirection() == NORTH) { 89 paintNorth(g, leftToRight, isEnabled, arrowColor, isPressed, 90 width, height, w, h, arrowHeight, arrowOffset, paintNorthBottom); 91 } else if (getDirection() == SOUTH) { 92 paintSouth(g, leftToRight, isEnabled, arrowColor, isPressed, 93 width, height, w, h, arrowHeight, arrowOffset); 94 } else if (getDirection() == EAST) { 95 paintEast(g, isEnabled, arrowColor, isPressed, 96 width, height, w, h, arrowHeight); 97 } else if (getDirection() == WEST) { 98 paintWest(g, isEnabled, arrowColor, isPressed, 99 width, height, w, h, arrowHeight); 100 } 101 if (PlasticUtils.is3D("ScrollBar.")) 102 paint3D(g); 103 } 104 105 113 protected int calculateArrowHeight(int height, int width) { 114 return (height + 1) / 4; 115 } 116 117 protected int calculateArrowOffset() { 118 return 0; 119 } 120 121 protected boolean isPaintingNorthBottom() { 122 return false; 123 } 124 125 126 private void paintWest(Graphics g, boolean isEnabled, Color arrowColor, 127 boolean isPressed, int width, int height, int w, int h, int arrowHeight) { 128 129 if (!isFreeStanding) { 130 height += 2; 131 width += 1; 132 g.translate(-1, 0); 133 } 134 135 g.setColor(arrowColor); 137 138 int startX = (((w + 1) - arrowHeight) / 2); 139 int startY = (h / 2); 140 141 for (int line = 0; line < arrowHeight; line++) { 142 g.drawLine( 143 startX + line, 144 startY - line, 145 startX + line, 146 startY + line + 1); 147 } 148 149 if (isEnabled) { 150 g.setColor(highlightColor); 151 152 if (!isPressed) { 153 g.drawLine(1, 1, width - 1, 1); 154 g.drawLine(1, 1, 1, height - 3); 155 } 156 g.drawLine(1, height - 1, width - 1, height - 1); 157 158 g.setColor(shadowColor); 159 g.drawLine(0, 0, width - 1, 0); 160 g.drawLine(0, 0, 0, height - 2); 161 g.drawLine(1, height - 2, width - 1, height - 2); 162 } else { 163 PlasticUtils.drawDisabledBorder(g, 0, 0, width + 1, height); 164 } 165 166 if (!isFreeStanding) { 167 height -= 2; 168 width -= 1; 169 g.translate(1, 0); 170 } 171 } 172 173 174 private void paintEast(Graphics g, boolean isEnabled, Color arrowColor, 175 boolean isPressed, int width, int height, int w, int h, int arrowHeight) { 176 if (!isFreeStanding) { 177 height += 2; 178 width += 1; 179 } 180 181 g.setColor(arrowColor); 183 184 int startX = (((w + 1) - arrowHeight) / 2) + arrowHeight - 1; 185 int startY = (h / 2); 186 for (int line = 0; line < arrowHeight; line++) { 187 g.drawLine( 188 startX - line, 189 startY - line, 190 startX - line, 191 startY + line + 1); 192 } 193 194 if (isEnabled) { 195 g.setColor(highlightColor); 196 if (!isPressed) { 197 g.drawLine(0, 1, width - 3, 1); 198 g.drawLine(0, 1, 0, height - 3); 199 } 200 g.drawLine(width - 1, 1, width - 1, height - 1); 201 g.drawLine(0, height - 1, width - 1, height - 1); 202 203 g.setColor(shadowColor); 204 g.drawLine(0, 0, width - 2, 0); 205 g.drawLine(width - 2, 1, width - 2, height - 2); 206 g.drawLine(0, height - 2, width - 2, height - 2); 207 } else { 208 PlasticUtils.drawDisabledBorder(g, -1, 0, width + 1, height); 209 } 210 if (!isFreeStanding) { 211 height -= 2; 212 width -= 1; 213 } 214 } 215 216 217 protected void paintSouth(Graphics g, boolean leftToRight, boolean isEnabled, 218 Color arrowColor, boolean isPressed, 219 int width, int height, int w, int h, int arrowHeight, int arrowOffset) { 220 221 if (!isFreeStanding) { 222 height += 1; 223 if (!leftToRight) { 224 width += 1; 225 g.translate(-1, 0); 226 } else { 227 width += 2; 228 } 229 } 230 231 g.setColor(arrowColor); 233 234 int startY = (((h + 0) - arrowHeight) / 2) + arrowHeight - 1; int startX = w / 2; 236 237 239 for (int line = 0; line < arrowHeight; line++) { 240 g.fillRect(startX - line - arrowOffset, startY - line, 2*(line + 1), 1); 241 } 242 243 if (isEnabled) { 244 g.setColor(highlightColor); 245 if (!isPressed) { 246 g.drawLine(1, 0, width - 3, 0); 247 g.drawLine(1, 0, 1, height - 3); 248 } 249 g.drawLine(0, height - 1, width - 1, height - 1); 250 g.drawLine(width - 1, 0, width - 1, height - 1); 251 252 g.setColor(shadowColor); 253 g.drawLine(0, 0, 0, height - 2); 254 g.drawLine(width - 2, 0, width - 2, height - 2); 255 g.drawLine(1, height - 2, width - 2, height - 2); 256 } else { 257 PlasticUtils.drawDisabledBorder(g, 0, -1, width, height + 1); 258 } 259 260 if (!isFreeStanding) { 261 height -= 1; 262 if (!leftToRight) { 263 width -= 1; 264 g.translate(1, 0); 265 } else { 266 width -= 2; 267 } 268 } 269 } 270 271 272 protected void paintNorth(Graphics g, boolean leftToRight, boolean isEnabled, 273 Color arrowColor, boolean isPressed, 274 int width, int height, int w, int h, int arrowHeight, int arrowOffset, 275 boolean paintBottom) { 276 if (!isFreeStanding) { 277 height += 1; 278 g.translate(0, -1); 279 if (!leftToRight) { 280 width += 1; 281 g.translate(-1, 0); 282 } else { 283 width += 2; 284 } 285 } 286 287 g.setColor(arrowColor); 289 int startY = ((h + 1) - arrowHeight) / 2; int startX = w / 2; 291 for (int line = 0; line < arrowHeight; line++) { 293 g.fillRect(startX - line - arrowOffset, startY + line, 2*(line + 1), 1); 294 } 295 296 if (isEnabled) { 297 g.setColor(highlightColor); 298 299 if (!isPressed) { 300 g.drawLine(1, 1, width - 3, 1); 301 g.drawLine(1, 1, 1, height - 1); 302 } 303 304 g.drawLine(width - 1, 1, width - 1, height - 1); 305 306 g.setColor(shadowColor); 307 g.drawLine(0, 0, width - 2, 0); 308 g.drawLine(0, 0, 0, height - 1); 309 g.drawLine(width - 2, 1, width - 2, height - 1); 310 if (paintBottom) { 311 g.fillRect(0, height - 1, width - 1, 1); 312 } 313 } else { 314 PlasticUtils.drawDisabledBorder(g, 0, 0, width, height + 1); 315 if (paintBottom) { 316 g.setColor(PlasticLookAndFeel.getControlShadow()); 317 g.fillRect(0, height - 1, width - 1, 1); 318 } 319 } 320 if (!isFreeStanding) { 321 height -= 1; 322 g.translate(0, 1); 323 if (!leftToRight) { 324 width -= 1; 325 g.translate(1, 0); 326 } else { 327 width -= 2; 328 } 329 } 330 } 331 332 333 private void paint3D(Graphics g) { 334 ButtonModel buttonModel = getModel(); 335 if (buttonModel.isArmed() && buttonModel.isPressed() || buttonModel.isSelected()) 336 return; 337 338 int width = getWidth(); 339 int height = getHeight(); 340 if (getDirection() == EAST) 341 width -= 2; 342 else if (getDirection() == SOUTH) 343 height -= 2; 344 345 Rectangle r = new Rectangle (1, 1, width, height); 346 boolean isHorizontal = (getDirection() == EAST || getDirection() == WEST); 347 PlasticUtils.addLight3DEffekt(g, r, isHorizontal); 348 } 349 } | Popular Tags |