1 7 8 package javax.swing.plaf.metal; 9 10 import java.awt.*; 11 import java.awt.image.*; 12 import javax.swing.*; 13 import java.io.*; 14 import java.util.*; 15 16 23 24 25 class MetalBumps implements Icon { 26 27 static final Color ALPHA = new Color(0, 0, 0, 0); 28 29 protected int xBumps; 30 protected int yBumps; 31 protected Color topColor; 32 protected Color shadowColor; 33 protected Color backColor; 34 35 protected static Vector buffers = new Vector(); 36 protected BumpBuffer buffer; 37 38 public MetalBumps( Dimension bumpArea ) { 39 this( bumpArea.width, bumpArea.height ); 40 } 41 42 public MetalBumps( int width, int height ) { 43 this(width, height, MetalLookAndFeel.getPrimaryControlHighlight(), 44 MetalLookAndFeel.getPrimaryControlDarkShadow(), 45 MetalLookAndFeel.getPrimaryControlShadow()); 46 } 47 48 53 public MetalBumps( int width, int height, 54 Color newTopColor, Color newShadowColor, Color newBackColor ) { 55 setBumpArea( width, height ); 56 setBumpColors( newTopColor, newShadowColor, newBackColor ); 57 } 58 59 private BumpBuffer getBuffer(GraphicsConfiguration gc, Color aTopColor, 60 Color aShadowColor, Color aBackColor) { 61 if (buffer != null && buffer.hasSameConfiguration( 62 gc, aTopColor, aShadowColor, aBackColor)) { 63 return buffer; 64 } 65 BumpBuffer result = null; 66 67 Enumeration elements = buffers.elements(); 68 69 while ( elements.hasMoreElements() ) { 70 BumpBuffer aBuffer = (BumpBuffer)elements.nextElement(); 71 if ( aBuffer.hasSameConfiguration(gc, aTopColor, aShadowColor, 72 aBackColor)) { 73 result = aBuffer; 74 break; 75 } 76 } 77 if (result == null) { 78 result = new BumpBuffer(gc, topColor, shadowColor, backColor); 79 buffers.addElement(result); 80 } 81 return result; 82 } 83 84 public void setBumpArea( Dimension bumpArea ) { 85 setBumpArea( bumpArea.width, bumpArea.height ); 86 } 87 88 public void setBumpArea( int width, int height ) { 89 xBumps = width / 2; 90 yBumps = height / 2; 91 } 92 93 public void setBumpColors( Color newTopColor, Color newShadowColor, Color newBackColor ) { 94 topColor = newTopColor; 95 shadowColor = newShadowColor; 96 if (newBackColor == null) { 97 backColor = ALPHA; 98 } 99 else { 100 backColor = newBackColor; 101 } 102 } 103 104 public void paintIcon( Component c, Graphics g, int x, int y ) { 105 GraphicsConfiguration gc = (g instanceof Graphics2D) ? 106 (GraphicsConfiguration)((Graphics2D)g). 107 getDeviceConfiguration() : null; 108 109 buffer = getBuffer(gc, topColor, shadowColor, backColor); 110 111 int bufferWidth = buffer.getImageSize().width; 112 int bufferHeight = buffer.getImageSize().height; 113 int iconWidth = getIconWidth(); 114 int iconHeight = getIconHeight(); 115 int x2 = x + iconWidth; 116 int y2 = y + iconHeight; 117 int savex = x; 118 119 while (y < y2) { 120 int h = Math.min(y2 - y, bufferHeight); 121 for (x = savex; x < x2; x += bufferWidth) { 122 int w = Math.min(x2 - x, bufferWidth); 123 g.drawImage(buffer.getImage(), 124 x, y, x+w, y+h, 125 0, 0, w, h, 126 null); 127 } 128 y += bufferHeight; 129 } 130 } 131 132 public int getIconWidth() { 133 return xBumps * 2; 134 } 135 136 public int getIconHeight() { 137 return yBumps * 2; 138 } 139 } 140 141 142 class BumpBuffer { 143 144 static final int IMAGE_SIZE = 64; 145 static Dimension imageSize = new Dimension( IMAGE_SIZE, IMAGE_SIZE ); 146 147 transient Image image; 148 Color topColor; 149 Color shadowColor; 150 Color backColor; 151 private GraphicsConfiguration gc; 152 153 public BumpBuffer(GraphicsConfiguration gc, Color aTopColor, 154 Color aShadowColor, Color aBackColor) { 155 this.gc = gc; 156 topColor = aTopColor; 157 shadowColor = aShadowColor; 158 backColor = aBackColor; 159 createImage(); 160 fillBumpBuffer(); 161 } 162 163 public boolean hasSameConfiguration(GraphicsConfiguration gc, 164 Color aTopColor, Color aShadowColor, 165 Color aBackColor) { 166 if (this.gc != null) { 167 if (!this.gc.equals(gc)) { 168 return false; 169 } 170 } 171 else if (gc != null) { 172 return false; 173 } 174 return topColor.equals( aTopColor ) && 175 shadowColor.equals( aShadowColor ) && 176 backColor.equals( aBackColor ); 177 } 178 179 183 public Image getImage() { 184 return image; 185 } 186 187 public Dimension getImageSize() { 188 return imageSize; 189 } 190 191 194 private void fillBumpBuffer() { 195 Graphics g = image.getGraphics(); 196 197 g.setColor( backColor ); 198 g.fillRect( 0, 0, IMAGE_SIZE, IMAGE_SIZE ); 199 200 g.setColor(topColor); 201 for (int x = 0; x < IMAGE_SIZE; x+=4) { 202 for (int y = 0; y < IMAGE_SIZE; y+=4) { 203 g.drawLine( x, y, x, y ); 204 g.drawLine( x+2, y+2, x+2, y+2); 205 } 206 } 207 208 g.setColor(shadowColor); 209 for (int x = 0; x < IMAGE_SIZE; x+=4) { 210 for (int y = 0; y < IMAGE_SIZE; y+=4) { 211 g.drawLine( x+1, y+1, x+1, y+1 ); 212 g.drawLine( x+3, y+3, x+3, y+3); 213 } 214 } 215 g.dispose(); 216 } 217 218 222 private void createImage() { 223 if (gc != null) { 224 image = gc.createCompatibleImage(IMAGE_SIZE, IMAGE_SIZE, 225 (backColor != MetalBumps.ALPHA) ? Transparency.OPAQUE : 226 Transparency.BITMASK); 227 } 228 else { 229 int cmap[] = { backColor.getRGB(), topColor.getRGB(), 230 shadowColor.getRGB() }; 231 IndexColorModel icm = new IndexColorModel(8, 3, cmap, 0, false, 232 (backColor == MetalBumps.ALPHA) ? 0 : -1, 233 DataBuffer.TYPE_BYTE); 234 image = new BufferedImage(IMAGE_SIZE, IMAGE_SIZE, 235 BufferedImage.TYPE_BYTE_INDEXED, icm); 236 } 237 } 238 } 239 | Popular Tags |