1 7 8 package javax.swing.plaf.metal; 9 10 import javax.swing.plaf.*; 11 import javax.swing.*; 12 import java.awt.*; 13 import java.awt.image.*; 14 import java.lang.ref.*; 15 import java.util.*; 16 17 23 24 class MetalUtils { 25 26 static void drawFlush3DBorder(Graphics g, Rectangle r) { 27 drawFlush3DBorder(g, r.x, r.y, r.width, r.height); 28 } 29 30 33 static void drawFlush3DBorder(Graphics g, int x, int y, int w, int h) { 34 g.translate( x, y); 35 g.setColor( MetalLookAndFeel.getControlDarkShadow() ); 36 g.drawRect( 0, 0, w-2, h-2 ); 37 g.setColor( MetalLookAndFeel.getControlHighlight() ); 38 g.drawRect( 1, 1, w-2, h-2 ); 39 g.setColor( MetalLookAndFeel.getControl() ); 40 g.drawLine( 0, h-1, 1, h-2 ); 41 g.drawLine( w-1, 0, w-2, 1 ); 42 g.translate( -x, -y); 43 } 44 45 49 static void drawPressed3DBorder(Graphics g, Rectangle r) { 50 drawPressed3DBorder( g, r.x, r.y, r.width, r.height ); 51 } 52 53 static void drawDisabledBorder(Graphics g, int x, int y, int w, int h) { 54 g.translate( x, y); 55 g.setColor( MetalLookAndFeel.getControlShadow() ); 56 g.drawRect( 0, 0, w-1, h-1 ); 57 g.translate(-x, -y); 58 } 59 60 64 static void drawPressed3DBorder(Graphics g, int x, int y, int w, int h) { 65 g.translate( x, y); 66 67 drawFlush3DBorder(g, 0, 0, w, h); 68 69 g.setColor( MetalLookAndFeel.getControlShadow() ); 70 g.drawLine( 1, 1, 1, h-2 ); 71 g.drawLine( 1, 1, w-2, 1 ); 72 g.translate( -x, -y); 73 } 74 75 80 static void drawDark3DBorder(Graphics g, Rectangle r) { 81 drawDark3DBorder(g, r.x, r.y, r.width, r.height); 82 } 83 84 89 static void drawDark3DBorder(Graphics g, int x, int y, int w, int h) { 90 g.translate( x, y); 91 92 drawFlush3DBorder(g, 0, 0, w, h); 93 94 g.setColor( MetalLookAndFeel.getControl() ); 95 g.drawLine( 1, 1, 1, h-2 ); 96 g.drawLine( 1, 1, w-2, 1 ); 97 g.setColor( MetalLookAndFeel.getControlShadow() ); 98 g.drawLine( 1, h-2, 1, h-2 ); 99 g.drawLine( w-2, 1, w-2, 1 ); 100 g.translate( -x, -y); 101 } 102 103 static void drawButtonBorder(Graphics g, int x, int y, int w, int h, boolean active) { 104 if (active) { 105 drawActiveButtonBorder(g, x, y, w, h); 106 } else { 107 drawFlush3DBorder(g, x, y, w, h); 108 } 109 } 110 111 static void drawActiveButtonBorder(Graphics g, int x, int y, int w, int h) { 112 drawFlush3DBorder(g, x, y, w, h); 113 g.setColor( MetalLookAndFeel.getPrimaryControl() ); 114 g.drawLine( x+1, y+1, x+1, h-3 ); 115 g.drawLine( x+1, y+1, w-3, x+1 ); 116 g.setColor( MetalLookAndFeel.getPrimaryControlDarkShadow() ); 117 g.drawLine( x+2, h-2, w-2, h-2 ); 118 g.drawLine( w-2, y+2, w-2, h-2 ); 119 } 120 121 static void drawDefaultButtonBorder(Graphics g, int x, int y, int w, int h, boolean active) { 122 drawButtonBorder(g, x+1, y+1, w-1, h-1, active); 123 g.translate(x, y); 124 g.setColor( MetalLookAndFeel.getControlDarkShadow() ); 125 g.drawRect( 0, 0, w-3, h-3 ); 126 g.drawLine( w-2, 0, w-2, 0); 127 g.drawLine( 0, h-2, 0, h-2); 128 g.translate(-x, -y); 129 } 130 131 static void drawDefaultButtonPressedBorder(Graphics g, int x, int y, int w, int h) { 132 drawPressed3DBorder(g, x + 1, y + 1, w - 1, h - 1); 133 g.translate(x, y); 134 g.setColor(MetalLookAndFeel.getControlDarkShadow()); 135 g.drawRect(0, 0, w - 3, h - 3); 136 g.drawLine(w - 2, 0, w - 2, 0); 137 g.drawLine(0, h - 2, 0, h - 2); 138 g.setColor(MetalLookAndFeel.getControl()); 139 g.drawLine(w - 1, 0, w - 1, 0); 140 g.drawLine(0, h - 1, 0, h - 1); 141 g.translate(-x, -y); 142 } 143 144 148 static boolean isLeftToRight( Component c ) { 149 return c.getComponentOrientation().isLeftToRight(); 150 } 151 152 static int getInt(Object key, int defaultValue) { 153 Object value = UIManager.get(key); 154 155 if (value instanceof Integer ) { 156 return ((Integer )value).intValue(); 157 } 158 if (value instanceof String ) { 159 try { 160 return Integer.parseInt((String )value); 161 } catch (NumberFormatException nfe) {} 162 } 163 return defaultValue; 164 } 165 166 192 static boolean drawGradient(Component c, Graphics g, String key, 193 int x, int y, int w, int h, boolean vertical) { 194 java.util.List gradient = (java.util.List )UIManager.get(key); 195 if (gradient == null || !(g instanceof Graphics2D)) { 196 return false; 197 } 198 199 if (w <= 0 || h <= 0) { 200 return true; 201 } 202 203 GradientPainter.INSTANCE.paint( 204 c, (Graphics2D)g, gradient, x, y, w, h, vertical); 205 return true; 206 } 207 208 209 private static class GradientPainter extends CachedPainter { 210 214 public static final GradientPainter INSTANCE = new GradientPainter(8); 215 216 private static final int IMAGE_SIZE = 64; 219 220 223 private int w; 224 227 private int h; 228 229 230 GradientPainter(int count) { 231 super(count); 232 } 233 234 public void paint(Component c, Graphics2D g, 235 java.util.List gradient, int x, int y, int w, 236 int h, boolean isVertical) { 237 int imageWidth; 238 int imageHeight; 239 if (isVertical) { 240 imageWidth = IMAGE_SIZE; 241 imageHeight = h; 242 } 243 else { 244 imageWidth = w; 245 imageHeight = IMAGE_SIZE; 246 } 247 synchronized(c.getTreeLock()) { 248 this.w = w; 249 this.h = h; 250 paint(c, g, x, y, imageWidth, imageHeight, 251 gradient, isVertical); 252 } 253 } 254 255 protected void paintToImage(Component c, Graphics g, 256 int w, int h, Object [] args) { 257 Graphics2D g2 = (Graphics2D)g; 258 java.util.List gradient = (java.util.List )args[0]; 259 boolean isVertical = ((Boolean )args[1]).booleanValue(); 260 if (isVertical) { 262 drawVerticalGradient(g2, 263 ((Number )gradient.get(0)).floatValue(), 264 ((Number )gradient.get(1)).floatValue(), 265 (Color)gradient.get(2), 266 (Color)gradient.get(3), 267 (Color)gradient.get(4), w, h); 268 } 269 else { 270 drawHorizontalGradient(g2, 271 ((Number )gradient.get(0)).floatValue(), 272 ((Number )gradient.get(1)).floatValue(), 273 (Color)gradient.get(2), 274 (Color)gradient.get(3), 275 (Color)gradient.get(4), w, h); 276 } 277 } 278 279 protected void paintImage(Component c, Graphics g, 280 int x, int y, int imageW, int imageH, 281 Image image, Object [] args) { 282 boolean isVertical = ((Boolean )args[1]).booleanValue(); 283 g.translate(x, y); 285 if (isVertical) { 286 for (int counter = 0; counter < w; counter += IMAGE_SIZE) { 287 int tileSize = Math.min(IMAGE_SIZE, w - counter); 288 g.drawImage(image, counter, 0, counter + tileSize, h, 289 0, 0, tileSize, h, null); 290 } 291 } 292 else { 293 for (int counter = 0; counter < h; counter += IMAGE_SIZE) { 294 int tileSize = Math.min(IMAGE_SIZE, h - counter); 295 g.drawImage(image, 0, counter, w, counter + tileSize, 296 0, 0, w, tileSize, null); 297 } 298 } 299 g.translate(-x, -y); 300 } 301 302 private void drawVerticalGradient(Graphics2D g, float ratio1, 303 float ratio2, Color c1,Color c2, 304 Color c3, int w, int h) { 305 int mid = (int)(ratio1 * h); 306 int mid2 = (int)(ratio2 * h); 307 if (mid > 0) { 308 g.setPaint(getGradient((float)0, (float)0, c1, (float)0, 309 (float)mid, c2)); 310 g.fillRect(0, 0, w, mid); 311 } 312 if (mid2 > 0) { 313 g.setColor(c2); 314 g.fillRect(0, mid, w, mid2); 315 } 316 if (mid > 0) { 317 g.setPaint(getGradient((float)0, (float)mid + mid2, c2, 318 (float)0, (float)mid * 2 + mid2, c1)); 319 g.fillRect(0, mid + mid2, w, mid); 320 } 321 if (h - mid * 2 - mid2 > 0) { 322 g.setPaint(getGradient((float)0, (float)mid * 2 + mid2, c1, 323 (float)0, (float)h, c3)); 324 g.fillRect(0, mid * 2 + mid2, w, h - mid * 2 - mid2); 325 } 326 } 327 328 private void drawHorizontalGradient(Graphics2D g, float ratio1, 329 float ratio2, Color c1,Color c2, 330 Color c3, int w, int h) { 331 int mid = (int)(ratio1 * w); 332 int mid2 = (int)(ratio2 * w); 333 if (mid > 0) { 334 g.setPaint(getGradient((float)0, (float)0, c1, 335 (float)mid, (float)0, c2)); 336 g.fillRect(0, 0, mid, h); 337 } 338 if (mid2 > 0) { 339 g.setColor(c2); 340 g.fillRect(mid, 0, mid2, h); 341 } 342 if (mid > 0) { 343 g.setPaint(getGradient((float)mid + mid2, (float)0, c2, 344 (float)mid * 2 + mid2, (float)0, c1)); 345 g.fillRect(mid + mid2, 0, mid, h); 346 } 347 if (w - mid * 2 - mid2 > 0) { 348 g.setPaint(getGradient((float)mid * 2 + mid2, (float)0, c1, 349 w, (float)0, c3)); 350 g.fillRect(mid * 2 + mid2, 0, w - mid * 2 - mid2, h); 351 } 352 } 353 354 private GradientPaint getGradient(float x1, float y1, 355 Color c1, float x2, float y2, 356 Color c2) { 357 return new GradientPaint(x1, y1, c1, x2, y2, c2, true); 358 } 359 } 360 361 362 365 static boolean isToolBarButton(JComponent c) { 366 return (c.getParent() instanceof JToolBar); 367 } 368 369 static Icon getOceanToolBarIcon(Image i) { 370 ImageProducer prod = new FilteredImageSource(i.getSource(), 371 new OceanToolBarImageFilter()); 372 return new IconUIResource(new ImageIcon( 373 Toolkit.getDefaultToolkit().createImage(prod))); 374 } 375 376 static Icon getOceanDisabledButtonIcon(Image image) { 377 Object [] range = (Object [])UIManager.get("Button.disabledGrayRange"); 378 int min = 180; 379 int max = 215; 380 if (range != null) { 381 min = ((Integer )range[0]).intValue(); 382 max = ((Integer )range[1]).intValue(); 383 } 384 ImageProducer prod = new FilteredImageSource(image.getSource(), 385 new OceanDisabledButtonImageFilter(min , max)); 386 return new IconUIResource(new ImageIcon( 387 Toolkit.getDefaultToolkit().createImage(prod))); 388 } 389 390 391 392 393 396 private static class OceanDisabledButtonImageFilter extends RGBImageFilter{ 397 private float min; 398 private float factor; 399 400 OceanDisabledButtonImageFilter(int min, int max) { 401 canFilterIndexColorModel = true; 402 this.min = (float)min; 403 this.factor = (max - min) / 255f; 404 } 405 406 public int filterRGB(int x, int y, int rgb) { 407 int gray = Math.min(255, (int)(((0.2125f * ((rgb >> 16) & 0xFF)) + 409 (0.7154f * ((rgb >> 8) & 0xFF)) + 410 (0.0721f * (rgb & 0xFF)) + .5f) * factor + min)); 411 412 return (rgb & 0xff000000) | (gray << 16) | (gray << 8) | 413 (gray << 0); 414 } 415 } 416 417 418 421 private static class OceanToolBarImageFilter extends RGBImageFilter { 422 OceanToolBarImageFilter() { 423 canFilterIndexColorModel = true; 424 } 425 426 public int filterRGB(int x, int y, int rgb) { 427 int r = ((rgb >> 16) & 0xff); 428 int g = ((rgb >> 8) & 0xff); 429 int b = (rgb & 0xff); 430 int gray = Math.max(Math.max(r, g), b); 431 return (rgb & 0xff000000) | (gray << 16) | (gray << 8) | 432 (gray << 0); 433 } 434 } 435 } 436 | Popular Tags |