001: /*
002: * Copyright (c) 2005-2008 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 Substance 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.watermark;
031:
032: import java.awt.*;
033:
034: import org.jvnet.substance.SubstanceLookAndFeel;
035: import org.jvnet.substance.utils.SubstanceColorUtilities;
036: import org.jvnet.substance.utils.SubstanceCoreUtilities;
037:
038: /**
039: * Implementation of {@link org.jvnet.substance.watermark.SubstanceWatermark},
040: * drawing random binary (0-1) glyphs as watermark. This class is part of
041: * officially supported API.
042: *
043: * @author Kirill Grouchnikov
044: * @author Chris Hall
045: */
046: public class SubstanceMosaicWatermark implements SubstanceWatermark {
047: /**
048: * Watermark image (screen-sized).
049: */
050: private static Image watermarkImage = null;
051:
052: /*
053: * (non-Javadoc)
054: *
055: * @see org.jvnet.substance.watermark.SubstanceWatermark#drawWatermarkImage(java.awt.Graphics,
056: * int, int, int, int)
057: */
058: public void drawWatermarkImage(Graphics graphics, Component c,
059: int x, int y, int width, int height) {
060: if (!c.isShowing())
061: return;
062: int dx = c.getLocationOnScreen().x;
063: int dy = c.getLocationOnScreen().y;
064: graphics.drawImage(SubstanceMosaicWatermark.watermarkImage, x,
065: y, x + width, y + height, x + dx, y + dy, x + dx
066: + width, y + dy + height, null);
067: }
068:
069: /*
070: * (non-Javadoc)
071: *
072: * @see org.jvnet.substance.watermark.SubstanceWatermark#updateWatermarkImage()
073: */
074: public boolean updateWatermarkImage() {
075: // fix by Chris for bug 67 - support for multiple screens
076: Rectangle virtualBounds = new Rectangle();
077: GraphicsEnvironment ge = GraphicsEnvironment
078: .getLocalGraphicsEnvironment();
079: GraphicsDevice[] gds = ge.getScreenDevices();
080: for (GraphicsDevice gd : gds) {
081: GraphicsConfiguration gc = gd.getDefaultConfiguration();
082: virtualBounds = virtualBounds.union(gc.getBounds());
083: }
084:
085: int screenWidth = virtualBounds.width;
086: int screenHeight = virtualBounds.height;
087: SubstanceMosaicWatermark.watermarkImage = SubstanceCoreUtilities
088: .getBlankImage(screenWidth, screenHeight);
089:
090: Graphics2D graphics = (Graphics2D) SubstanceMosaicWatermark.watermarkImage
091: .getGraphics().create();
092:
093: boolean status = this .drawWatermarkImage(graphics, 0, 0,
094: screenWidth, screenHeight, false);
095: graphics.dispose();
096: return status;
097: }
098:
099: /*
100: * (non-Javadoc)
101: *
102: * @see org.jvnet.substance.watermark.SubstanceWatermark#previewWatermark(java.awt.Graphics,
103: * int, int, int, int)
104: */
105: public void previewWatermark(Graphics g, int x, int y, int width,
106: int height) {
107: this .drawWatermarkImage((Graphics2D) g, x, y, width, height,
108: true);
109: }
110:
111: /**
112: * Draws the specified portion of the watermark image.
113: *
114: * @param graphics
115: * Graphic context.
116: * @param x
117: * the <i>x</i> coordinate of the watermark to be drawn.
118: * @param y
119: * The <i>y</i> coordinate of the watermark to be drawn.
120: * @param width
121: * The width of the watermark to be drawn.
122: * @param height
123: * The height of the watermark to be drawn.
124: * @param isPreview
125: * Indication whether the result is a preview image.
126: * @return Indication whether the draw succeeded.
127: */
128: private boolean drawWatermarkImage(Graphics2D graphics, int x,
129: int y, int width, int height, boolean isPreview) {
130: int sqDim = 7;
131: int sqGap = 2;
132: int cellDim = sqDim + sqGap;
133:
134: int rows = height / cellDim;
135: int columns = width / cellDim;
136: for (int col = 0; col <= columns; col++) {
137: for (int row = 0; row <= rows; row++) {
138: double val = isPreview ? Math.abs(Math
139: .sin((1 + col + columns * row))) : Math
140: .random();
141: int delta = isPreview ? (int) (150.0 * val)
142: : (int) (10.0 * val);
143: int alpha = 0;
144: if (isPreview) {
145: // if (SubstanceCoreUtilities.isThemeDark(SubstanceLookAndFeel
146: // .getTheme()))
147: // alpha = 50 + delta * 3 / 2;
148: // else
149: alpha = 50 + delta;
150: } else {
151: if (SubstanceCoreUtilities
152: .isThemeDark(SubstanceLookAndFeel
153: .getTheme()))
154: alpha = 10 + delta * 3 / 2;
155: else
156: alpha = 5 + delta;
157: }
158: Color stampColor = null;
159: if (isPreview) {
160: stampColor = SubstanceCoreUtilities
161: .isThemeDark(SubstanceLookAndFeel
162: .getTheme()) ? Color.lightGray
163: : Color.darkGray;
164: stampColor = SubstanceColorUtilities.getAlphaColor(
165: stampColor, alpha);
166: } else {
167: stampColor = SubstanceColorUtilities
168: .getWatermarkStampColor(alpha);
169: }
170: graphics.setColor(stampColor);
171: graphics.fillRect(x + col * cellDim, y + row * cellDim,
172: sqDim, sqDim);
173: }
174: }
175: return true;
176: }
177:
178: /*
179: * (non-Javadoc)
180: *
181: * @see org.jvnet.substance.watermark.SubstanceWatermark#getDisplayName()
182: */
183: public String getDisplayName() {
184: return SubstanceMosaicWatermark.getName();
185: }
186:
187: /**
188: * Returns the name of all watermarks of <code>this</code> class.
189: *
190: * @return The name of all watermarks of <code>this</code> class.
191: */
192: public static String getName() {
193: return "Square mosaic";
194: }
195:
196: /*
197: * (non-Javadoc)
198: *
199: * @see org.jvnet.substance.watermark.SubstanceWatermark#isDependingOnTheme()
200: */
201: public boolean isDependingOnTheme() {
202: return true;
203: }
204:
205: /*
206: * (non-Javadoc)
207: *
208: * @see org.jvnet.substance.watermark.SubstanceWatermark#dispose()
209: */
210: public void dispose() {
211: watermarkImage = null;
212: }
213: }
|