1 30 31 package com.jgoodies.looks.plastic; 32 33 import java.awt.*; 34 import java.awt.image.BufferedImage ; 35 import java.awt.image.DataBuffer ; 36 import java.awt.image.IndexColorModel ; 37 import java.util.Enumeration ; 38 import java.util.Vector ; 39 40 import javax.swing.Icon ; 41 42 48 final class PlasticBumps implements Icon { 49 50 protected int xBumps; 51 protected int yBumps; 52 protected Color topColor; 53 protected Color shadowColor; 54 protected Color backColor; 55 56 protected static Vector buffers = new Vector (); 57 protected BumpBuffer buffer; 58 59 public PlasticBumps(Dimension bumpArea) { 60 this(bumpArea.width, bumpArea.height); 61 } 62 63 public PlasticBumps(int width, int height) { 64 this(width, height, 65 PlasticLookAndFeel.getPrimaryControlHighlight(), 66 PlasticLookAndFeel.getPrimaryControlDarkShadow(), 67 PlasticLookAndFeel.getPrimaryControlShadow()); 68 } 69 70 public PlasticBumps(int width, int height, 71 Color newTopColor, Color newShadowColor, Color newBackColor) { 72 setBumpArea(width, height); 73 setBumpColors(newTopColor, newShadowColor, newBackColor); 74 } 75 76 private BumpBuffer getBuffer(GraphicsConfiguration gc, 77 Color aTopColor, Color aShadowColor, Color aBackColor) { 78 if (buffer != null 79 && buffer.hasSameConfiguration(gc, aTopColor, aShadowColor, aBackColor)) { 80 return buffer; 81 } 82 BumpBuffer result = null; 83 Enumeration elements = buffers.elements(); 84 while (elements.hasMoreElements()) { 85 BumpBuffer aBuffer = (BumpBuffer) elements.nextElement(); 86 if (aBuffer.hasSameConfiguration(gc, aTopColor, aShadowColor, aBackColor)) { 87 result = aBuffer; 88 break; 89 } 90 } 91 if (result == null) { 92 result = new BumpBuffer(gc, topColor, shadowColor, backColor); 93 buffers.addElement(result); 94 } 95 return result; 96 } 97 98 public void setBumpArea(Dimension bumpArea) { 99 setBumpArea(bumpArea.width, bumpArea.height); 100 } 101 102 public void setBumpArea(int width, int height) { 103 xBumps = width / 2; 104 yBumps = height / 2; 105 } 106 107 public void setBumpColors(Color newTopColor, Color newShadowColor, Color newBackColor) { 108 topColor = newTopColor; 109 shadowColor = newShadowColor; 110 backColor = newBackColor; 111 } 112 113 public void paintIcon(Component c, Graphics g, int x, int y) { 114 GraphicsConfiguration gc = (g instanceof Graphics2D) 115 ? (GraphicsConfiguration) ((Graphics2D) g).getDeviceConfiguration() 116 : null; 117 118 buffer = getBuffer(gc, topColor, shadowColor, backColor); 119 120 int bufferWidth = buffer.getImageSize().width; 121 int bufferHeight = buffer.getImageSize().height; 122 int iconWidth = getIconWidth(); 123 int iconHeight = getIconHeight(); 124 int x2 = x + iconWidth; 125 int y2 = y + iconHeight; 126 int savex = x; 127 128 while (y < y2) { 129 int h = Math.min(y2 - y, bufferHeight); 130 for (x = savex; x < x2; x += bufferWidth) { 131 int w = Math.min(x2 - x, bufferWidth); 132 g.drawImage(buffer.getImage(), x, y, x + w, y + h, 0, 0, w, h, null); 133 } 134 y += bufferHeight; 135 } 136 } 137 138 public int getIconWidth() { return xBumps * 2; } 139 public int getIconHeight() { return yBumps * 2; } 140 } 141 142 final 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( 154 GraphicsConfiguration gc, 155 Color aTopColor, 156 Color aShadowColor, 157 Color aBackColor) { 158 this.gc = gc; 159 topColor = aTopColor; 160 shadowColor = aShadowColor; 161 backColor = aBackColor; 162 createImage(); 163 fillBumpBuffer(); 164 } 165 166 public boolean hasSameConfiguration( 167 GraphicsConfiguration aGC, 168 Color aTopColor, 169 Color aShadowColor, 170 Color aBackColor) { 171 if (this.gc != null) { 172 if (!this.gc.equals(aGC)) { 173 return false; 174 } 175 } else if (aGC != null) { 176 return false; 177 } 178 return topColor.equals(aTopColor) 179 && shadowColor.equals(aShadowColor) 180 && backColor.equals(aBackColor); 181 } 182 183 187 public Image getImage() { return image; } 188 189 public Dimension getImageSize() { return imageSize; } 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 } else { 226 int cmap[] = { backColor.getRGB(), topColor.getRGB(), shadowColor.getRGB()}; 227 IndexColorModel icm = 228 new IndexColorModel (8, 3, cmap, 0, false, -1, DataBuffer.TYPE_BYTE); 229 image = new BufferedImage (IMAGE_SIZE, IMAGE_SIZE, BufferedImage.TYPE_BYTE_INDEXED, icm); 230 } 231 } 232 } 233 | Popular Tags |