001 /* 002 * @(#)PaintUtils.java 1.0 2008-03-01 003 * 004 * Copyright (c) 2008 Jeremy Wood 005 * E-mail: mickleness@gmail.com 006 * All rights reserved. 007 * 008 * The copyright of this software is owned by Jeremy Wood. 009 * You may not use, copy or modify this software, except in 010 * accordance with the license agreement you entered into with 011 * Jeremy Wood. For details see accompanying license terms. 012 */ 013 014 package com.colorpicker.awt; 015 016 import java.awt.*; 017 018 import javax.swing.*; 019 020 /** Some static methods for some common painting functions. 021 * 022 * @version 1.0 023 * @author Jeremy Wood 024 **/ 025 public class PaintUtils { 026 027 /** Four shades of white, each with increasing opacity. */ 028 public final static Color[] whites = new Color[] { 029 new Color(255,255,255,50), 030 new Color(255,255,255,100), 031 new Color(255,255,255,150) 032 }; 033 034 /** Four shades of black, each with increasing opacity. */ 035 public final static Color[] blacks = new Color[] { 036 new Color(0,0,0,50), 037 new Color(0,0,0,100), 038 new Color(0,0,0,150) 039 }; 040 041 /** @return the color used to indicate when a component has 042 * focus. By default this uses the color (64,113,167), but you can 043 * override this by calling: 044 * <BR><code>UIManager.put("focusRing",customColor);</code> 045 */ 046 public static Color getFocusRingColor() { 047 Object obj = UIManager.getColor("focusRing"); 048 if(obj instanceof Color) 049 return (Color)obj; 050 return new Color(64,113,167); 051 } 052 053 /** Paints 3 different strokes around a shape to indicate focus. 054 * The widest stroke is the most transparent, so this achieves a nice 055 * "glow" effect. 056 * <P>The catch is that you have to render this underneath the shape, 057 * and the shape should be filled completely. 058 * 059 * @param g the graphics to paint to 060 * @param shape the shape to outline 061 * @param biggestStroke the widest stroke to use. 062 */ 063 public static void paintFocus(Graphics2D g,Shape shape,int biggestStroke) { 064 Color focusColor = getFocusRingColor(); 065 Color[] focusArray = new Color[] { 066 new Color(focusColor.getRed(), focusColor.getGreen(), focusColor.getBlue(),255), 067 new Color(focusColor.getRed(), focusColor.getGreen(), focusColor.getBlue(),170), 068 new Color(focusColor.getRed(), focusColor.getGreen(), focusColor.getBlue(),110) 069 }; 070 g.setStroke(new BasicStroke(biggestStroke)); 071 g.setColor(focusArray[2]); 072 g.draw(shape); 073 g.setStroke(new BasicStroke(biggestStroke-1)); 074 g.setColor(focusArray[1]); 075 g.draw(shape); 076 g.setStroke(new BasicStroke(biggestStroke-2)); 077 g.setColor(focusArray[0]); 078 g.draw(shape); 079 g.setStroke(new BasicStroke(1)); 080 } 081 082 /** Uses translucent shades of white and black to draw highlights 083 * and shadows around a rectangle, and then frames the rectangle 084 * with a shade of gray (120). 085 * <P>This should be called to add a finishing touch on top of 086 * existing graphics. 087 * @param g the graphics to paint to. 088 * @param r the rectangle to paint. 089 */ 090 public static void drawBevel(Graphics g,Rectangle r) { 091 drawColors(blacks,g, r.x, r.y+r.height, r.x+r.width, r.y+r.height, SwingConstants.SOUTH); 092 drawColors(blacks,g, r.x+r.width, r.y, r.x+r.width, r.y+r.height, SwingConstants.EAST); 093 094 drawColors(whites,g, r.x, r.y, r.x+r.width, r.y, SwingConstants.NORTH); 095 drawColors(whites,g, r.x, r.y, r.x, r.y+r.height, SwingConstants.WEST); 096 097 g.setColor(new Color(120, 120, 120)); 098 g.drawRect(r.x, r.y, r.width, r.height); 099 } 100 101 private static void drawColors(Color[] colors,Graphics g,int x1,int y1,int x2,int y2,int direction) { 102 for(int a = 0; a<colors.length; a++) { 103 g.setColor(colors[colors.length-a-1]); 104 if(direction==SwingConstants.SOUTH) { 105 g.drawLine(x1, y1-a, x2, y2-a); 106 } else if(direction==SwingConstants.NORTH) { 107 g.drawLine(x1, y1+a, x2, y2+a); 108 } else if(direction==SwingConstants.EAST) { 109 g.drawLine(x1-a, y1, x2-a, y2); 110 } else if(direction==SwingConstants.WEST) { 111 g.drawLine(x1+a, y1, x2+a, y2); 112 } 113 } 114 } 115 }