001: /*
002: * Copyright (c) 2001-2007 JGoodies Karsten Lentzsch. All Rights Reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * o Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * o Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * o Neither the name of JGoodies Karsten Lentzsch nor the names of
015: * its contributors may be used to endorse or promote products derived
016: * from this software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
020: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
021: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
022: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
025: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
026: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
027: * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
028: * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029: */
030:
031: package com.jgoodies.looks.plastic;
032:
033: import java.awt.*;
034: import java.awt.image.BufferedImage;
035: import java.awt.image.DataBuffer;
036: import java.awt.image.IndexColorModel;
037: import java.util.Enumeration;
038: import java.util.Vector;
039:
040: import javax.swing.Icon;
041:
042: /**
043: * The bumps used in the JGoodies Plastic Look&Feel.
044: *
045: * @author Sun
046: * @version $Revision: 1.3 $
047: */
048: final class PlasticBumps implements Icon {
049:
050: protected int xBumps;
051: protected int yBumps;
052: protected Color topColor;
053: protected Color shadowColor;
054: protected Color backColor;
055:
056: protected static Vector buffers = new Vector();
057: protected BumpBuffer buffer;
058:
059: public PlasticBumps(Dimension bumpArea) {
060: this (bumpArea.width, bumpArea.height);
061: }
062:
063: public PlasticBumps(int width, int height) {
064: this (width, height, PlasticLookAndFeel
065: .getPrimaryControlHighlight(), PlasticLookAndFeel
066: .getPrimaryControlDarkShadow(), PlasticLookAndFeel
067: .getPrimaryControlShadow());
068: }
069:
070: public PlasticBumps(int width, int height, Color newTopColor,
071: Color newShadowColor, Color newBackColor) {
072: setBumpArea(width, height);
073: setBumpColors(newTopColor, newShadowColor, newBackColor);
074: }
075:
076: private BumpBuffer getBuffer(GraphicsConfiguration gc,
077: Color aTopColor, Color aShadowColor, Color aBackColor) {
078: if (buffer != null
079: && buffer.hasSameConfiguration(gc, aTopColor,
080: aShadowColor, aBackColor)) {
081: return buffer;
082: }
083: BumpBuffer result = null;
084: Enumeration elements = buffers.elements();
085: while (elements.hasMoreElements()) {
086: BumpBuffer aBuffer = (BumpBuffer) elements.nextElement();
087: if (aBuffer.hasSameConfiguration(gc, aTopColor,
088: aShadowColor, aBackColor)) {
089: result = aBuffer;
090: break;
091: }
092: }
093: if (result == null) {
094: result = new BumpBuffer(gc, topColor, shadowColor,
095: backColor);
096: buffers.addElement(result);
097: }
098: return result;
099: }
100:
101: public void setBumpArea(Dimension bumpArea) {
102: setBumpArea(bumpArea.width, bumpArea.height);
103: }
104:
105: public void setBumpArea(int width, int height) {
106: xBumps = width / 2;
107: yBumps = height / 2;
108: }
109:
110: public void setBumpColors(Color newTopColor, Color newShadowColor,
111: Color newBackColor) {
112: topColor = newTopColor;
113: shadowColor = newShadowColor;
114: backColor = newBackColor;
115: }
116:
117: public void paintIcon(Component c, Graphics g, int x, int y) {
118: GraphicsConfiguration gc = (g instanceof Graphics2D) ? (GraphicsConfiguration) ((Graphics2D) g)
119: .getDeviceConfiguration()
120: : null;
121:
122: buffer = getBuffer(gc, topColor, shadowColor, backColor);
123:
124: int bufferWidth = buffer.getImageSize().width;
125: int bufferHeight = buffer.getImageSize().height;
126: int iconWidth = getIconWidth();
127: int iconHeight = getIconHeight();
128: int x2 = x + iconWidth;
129: int y2 = y + iconHeight;
130: int savex = x;
131:
132: while (y < y2) {
133: int h = Math.min(y2 - y, bufferHeight);
134: for (x = savex; x < x2; x += bufferWidth) {
135: int w = Math.min(x2 - x, bufferWidth);
136: g.drawImage(buffer.getImage(), x, y, x + w, y + h, 0,
137: 0, w, h, null);
138: }
139: y += bufferHeight;
140: }
141: }
142:
143: public int getIconWidth() {
144: return xBumps * 2;
145: }
146:
147: public int getIconHeight() {
148: return yBumps * 2;
149: }
150: }
151:
152: final class BumpBuffer {
153:
154: static final int IMAGE_SIZE = 64;
155: static Dimension imageSize = new Dimension(IMAGE_SIZE, IMAGE_SIZE);
156:
157: transient Image image;
158: Color topColor;
159: Color shadowColor;
160: Color backColor;
161: private GraphicsConfiguration gc;
162:
163: public BumpBuffer(GraphicsConfiguration gc, Color aTopColor,
164: Color aShadowColor, Color aBackColor) {
165: this .gc = gc;
166: topColor = aTopColor;
167: shadowColor = aShadowColor;
168: backColor = aBackColor;
169: createImage();
170: fillBumpBuffer();
171: }
172:
173: public boolean hasSameConfiguration(GraphicsConfiguration aGC,
174: Color aTopColor, Color aShadowColor, Color aBackColor) {
175: if (this .gc != null) {
176: if (!this .gc.equals(aGC)) {
177: return false;
178: }
179: } else if (aGC != null) {
180: return false;
181: }
182: return topColor.equals(aTopColor)
183: && shadowColor.equals(aShadowColor)
184: && backColor.equals(aBackColor);
185: }
186:
187: /**
188: * Returns the Image containing the bumps appropriate for the passed in
189: * <code>GraphicsConfiguration</code>.
190: */
191: public Image getImage() {
192: return image;
193: }
194:
195: public Dimension getImageSize() {
196: return imageSize;
197: }
198:
199: /**
200: * Paints the bumps into the current image.
201: */
202: private void fillBumpBuffer() {
203: Graphics g = image.getGraphics();
204:
205: g.setColor(backColor);
206: g.fillRect(0, 0, IMAGE_SIZE, IMAGE_SIZE);
207:
208: g.setColor(topColor);
209: for (int x = 0; x < IMAGE_SIZE; x += 4) {
210: for (int y = 0; y < IMAGE_SIZE; y += 4) {
211: g.drawLine(x, y, x, y);
212: g.drawLine(x + 2, y + 2, x + 2, y + 2);
213: }
214: }
215:
216: g.setColor(shadowColor);
217: for (int x = 0; x < IMAGE_SIZE; x += 4) {
218: for (int y = 0; y < IMAGE_SIZE; y += 4) {
219: g.drawLine(x + 1, y + 1, x + 1, y + 1);
220: g.drawLine(x + 3, y + 3, x + 3, y + 3);
221: }
222: }
223: g.dispose();
224: }
225:
226: /**
227: * Creates the image appropriate for the passed in
228: * <code>GraphicsConfiguration</code>, which may be null.
229: */
230: private void createImage() {
231: if (gc != null) {
232: image = gc.createCompatibleImage(IMAGE_SIZE, IMAGE_SIZE);
233: } else {
234: int[] cmap = { backColor.getRGB(), topColor.getRGB(),
235: shadowColor.getRGB() };
236: IndexColorModel icm = new IndexColorModel(8, 3, cmap, 0,
237: false, -1, DataBuffer.TYPE_BYTE);
238: image = new BufferedImage(IMAGE_SIZE, IMAGE_SIZE,
239: BufferedImage.TYPE_BYTE_INDEXED, icm);
240: }
241: }
242: }
|