KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jgoodies > looks > plastic > PlasticBumps


1 /*
2  * Copyright (c) 2001-2005 JGoodies Karsten Lentzsch. All Rights Reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * o Redistributions of source code must retain the above copyright notice,
8  * this list of conditions and the following disclaimer.
9  *
10  * o Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * o Neither the name of JGoodies Karsten Lentzsch nor the names of
15  * its contributors may be used to endorse or promote products derived
16  * from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
22  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
27  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */

30
31 package com.jgoodies.looks.plastic;
32
33 import java.awt.*;
34 import java.awt.image.BufferedImage JavaDoc;
35 import java.awt.image.DataBuffer JavaDoc;
36 import java.awt.image.IndexColorModel JavaDoc;
37 import java.util.Enumeration JavaDoc;
38 import java.util.Vector JavaDoc;
39
40 import javax.swing.Icon JavaDoc;
41
42 /**
43  * The bumps used in the JGoodies Plastic Look&Feel.
44  *
45  * @author Sun
46  * @version $Revision: 1.3 $
47  */

48 final class PlasticBumps implements Icon JavaDoc {
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 JavaDoc buffers = new Vector JavaDoc();
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 JavaDoc 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 JavaDoc 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     /**
184      * Returns the Image containing the bumps appropriate for the passed in
185      * <code>GraphicsConfiguration</code>.
186      */

187     public Image JavaDoc getImage() { return image; }
188
189     public Dimension getImageSize() { return imageSize; }
190
191     /**
192      * Paints the bumps into the current image.
193      */

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     /**
219      * Creates the image appropriate for the passed in
220      * <code>GraphicsConfiguration</code>, which may be null.
221      */

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 JavaDoc icm =
228                 new IndexColorModel JavaDoc(8, 3, cmap, 0, false, -1, DataBuffer.TYPE_BYTE);
229             image = new BufferedImage JavaDoc(IMAGE_SIZE, IMAGE_SIZE, BufferedImage.TYPE_BYTE_INDEXED, icm);
230         }
231     }
232 }
233
Popular Tags