001: /*
002: * Copyright (c) 2005-2008 Flamingo / Substance Kirill Grouchnikov. 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 Flamingo Kirill Grouchnikov 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: package org.jvnet.substance.flamingo.ribbon.gallery.oob;
031:
032: import java.awt.*;
033: import java.awt.image.*;
034: import java.util.HashMap;
035: import java.util.Map;
036:
037: import org.jvnet.flamingo.common.icon.ResizableIcon;
038: import org.jvnet.substance.SubstanceLookAndFeel;
039: import org.jvnet.substance.utils.SubstanceCoreUtilities;
040: import org.jvnet.substance.watermark.SubstanceWatermark;
041:
042: /**
043: * Resizable icon for <b>Substance </b> watermarks.
044: *
045: * @author Kirill Grouchnikov
046: */
047: public class WatermarkResizableIcon implements ResizableIcon {
048: /**
049: * Current width in pixels.
050: */
051: private int currWidth;
052:
053: /**
054: * Current height in pixels.
055: */
056: private int currHeight;
057:
058: /**
059: * Original width in pixels.
060: */
061: private int origWidth;
062:
063: /**
064: * Original height in pixels.
065: */
066: private int origHeight;
067:
068: /**
069: * Associated watermark. Can be <code>null</code>.
070: */
071: private SubstanceWatermark watermark;
072:
073: /**
074: * Images for water signs.
075: */
076: private static Map<Integer, BufferedImage> waterSigns = new HashMap<Integer, BufferedImage>();
077:
078: /**
079: * Creates a new icon.
080: *
081: * @param watermark
082: * Associated watermark (can be <code>null</code>).
083: * @param startWidth
084: * Original width in pixels.
085: * @param startHeight
086: * Original heigth in pixels.
087: */
088: public WatermarkResizableIcon(SubstanceWatermark watermark,
089: int startWidth, int startHeight) {
090: this .watermark = watermark;
091: this .origWidth = startWidth;
092: this .origHeight = startHeight;
093: this .currWidth = startWidth;
094: this .currHeight = startHeight;
095: }
096:
097: /*
098: * (non-Javadoc)
099: *
100: * @see org.jvnet.flamingo.common.ResizableIcon#setDimension(java.awt.Dimension)
101: */
102: public void setDimension(Dimension newDimension) {
103: this .currWidth = newDimension.width;
104: this .currHeight = newDimension.height;
105: }
106:
107: /*
108: * (non-Javadoc)
109: *
110: * @see javax.swing.Icon#getIconHeight()
111: */
112: public int getIconHeight() {
113: return this .currHeight;
114: }
115:
116: /*
117: * (non-Javadoc)
118: *
119: * @see javax.swing.Icon#getIconWidth()
120: */
121: public int getIconWidth() {
122: return this .currWidth;
123: }
124:
125: /*
126: * (non-Javadoc)
127: *
128: * @see org.jvnet.flamingo.common.ResizableIcon#setHeight(int)
129: */
130: public void setHeight(int height) {
131: double coef = (double) height / (double) this .currHeight;
132: this .currWidth = (int) (coef * this .currWidth);
133: this .currHeight = height;
134: }
135:
136: /*
137: * (non-Javadoc)
138: *
139: * @see org.jvnet.flamingo.common.ResizableIcon#setWidth(int)
140: */
141: public void setWidth(int width) {
142: double coef = (double) width / (double) this .currWidth;
143: this .currHeight = (int) (coef * this .currHeight);
144: this .currWidth = width;
145: }
146:
147: /**
148: * Returns a watermark sign image.
149: *
150: * @param size
151: * Image size.
152: * @return Watermark sign image.
153: */
154: private static BufferedImage getWatermarkSign(int size) {
155: if (waterSigns.containsKey(size))
156: return waterSigns.get(size);
157:
158: BufferedImage blurred = SubstanceCoreUtilities.getBlankImage(
159: size, size);
160: Font font = new Font("Tahoma", size > 15 ? Font.PLAIN
161: : Font.BOLD, size);
162: Graphics2D blurredGraphics = (Graphics2D) blurred.getGraphics()
163: .create();
164: blurredGraphics.setRenderingHint(
165: RenderingHints.KEY_TEXT_ANTIALIASING,
166: RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
167: blurredGraphics.setColor(new Color(255, 255, 255, 96));
168: blurredGraphics.fillRect(0, 0, size, size);
169: blurredGraphics.setColor(Color.white);
170: blurredGraphics.setFont(font);
171: blurredGraphics.drawString("\u2248", 1, size - 1);
172:
173: float data[] = { 0.0625f, 0.125f, 0.0625f, 0.125f, 0.25f,
174: 0.125f, 0.0625f, 0.125f, 0.0625f };
175: Kernel kernel = new Kernel(3, 3, data);
176:
177: ConvolveOp convolve = new ConvolveOp(kernel,
178: ConvolveOp.EDGE_NO_OP, null);
179:
180: BufferedImage result = SubstanceCoreUtilities.getBlankImage(
181: size, size);
182: convolve.filter(blurred, result);
183: Graphics2D graphics = (Graphics2D) result.getGraphics()
184: .create();
185: graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
186: RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
187: graphics.setColor(new Color(0, 0, 255));
188: graphics.setFont(font);
189: graphics.drawString("\u2248", 1, size - 1);
190:
191: blurredGraphics.dispose();
192: // graphics.dispose();
193:
194: waterSigns.put(size, result);
195: return result;
196: }
197:
198: /*
199: * (non-Javadoc)
200: *
201: * @see javax.swing.Icon#paintIcon(java.awt.Component, java.awt.Graphics,
202: * int, int)
203: */
204: public void paintIcon(Component c, Graphics g, int x, int y) {
205: Graphics2D graphics = (Graphics2D) g.create();
206: graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
207: RenderingHints.VALUE_ANTIALIAS_ON);
208:
209: graphics.translate(x, y);
210: graphics.clipRect(0, 0, this .currWidth, this .currHeight);
211: if (this .watermark != null) {
212: graphics.setColor(SubstanceLookAndFeel.getTheme()
213: .getDefaultColorScheme().getExtraLightColor());
214: graphics.fillRect(0, 0, this .currWidth, this .currHeight);
215: Composite oldComp = graphics.getComposite();
216: graphics.setComposite(AlphaComposite.getInstance(
217: AlphaComposite.SRC_OVER, 0.6f));
218:
219: this .watermark.previewWatermark(graphics, 0, 0,
220: this .currWidth, this .currHeight);
221: graphics.setComposite(oldComp);
222:
223: int waterSize = this .currHeight / 2;
224: graphics.drawImage(getWatermarkSign(waterSize),
225: this .currWidth - waterSize - 2, this .currHeight
226: - waterSize - 2, null);
227:
228: graphics.setColor(Color.black);
229: graphics.drawRect(0, 0, this .currWidth - 1,
230: this .currHeight - 1);
231: } else {
232: int waterSize = Math.min(this .currHeight, this .currWidth) - 2;
233: graphics.drawImage(getWatermarkSign(waterSize),
234: (this .currWidth - waterSize) / 2,
235: (this .currHeight - waterSize) / 2, null);
236:
237: graphics.setColor(Color.black);
238: graphics.drawRect(0, 0, this .currWidth - 1,
239: this .currHeight - 1);
240: }
241:
242: graphics.dispose();
243: }
244:
245: /*
246: * (non-Javadoc)
247: *
248: * @see org.jvnet.flamingo.common.ResizableIcon#revertToOriginalDimension()
249: */
250: public void revertToOriginalDimension() {
251: this.currHeight = this.origHeight;
252: this.currWidth = this.origWidth;
253: }
254: }
|