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 SubstanceBinaryWatermark implements SubstanceWatermark {
047: /**
048: * Watermark image (screen-sized).
049: */
050: private static Image watermarkImage = null;
051:
052: /**
053: * Font size.
054: */
055: private static int fontSize = 14;
056:
057: /**
058: * Font instance.
059: */
060: private static Font tahoma = new Font("Tahoma", Font.BOLD, fontSize);
061:
062: /**
063: * Font instance for preview.
064: */
065: private static Font tahomaPreview = new Font("Tahoma", Font.BOLD,
066: fontSize - 2);
067:
068: /*
069: * (non-Javadoc)
070: *
071: * @see org.jvnet.substance.watermark.SubstanceWatermark#drawWatermarkImage(java.awt.Graphics,
072: * int, int, int, int)
073: */
074: public void drawWatermarkImage(Graphics graphics, Component c,
075: int x, int y, int width, int height) {
076: if (!c.isShowing())
077: return;
078: int dx = c.getLocationOnScreen().x;
079: int dy = c.getLocationOnScreen().y;
080: graphics.drawImage(SubstanceBinaryWatermark.watermarkImage, x,
081: y, x + width, y + height, x + dx, y + dy, x + dx
082: + width, y + dy + height, null);
083: }
084:
085: /*
086: * (non-Javadoc)
087: *
088: * @see org.jvnet.substance.watermark.SubstanceWatermark#updateWatermarkImage()
089: */
090: public boolean updateWatermarkImage() {
091: // fix by Chris for bug 67 - support for multiple screens
092: Rectangle virtualBounds = new Rectangle();
093: GraphicsEnvironment ge = GraphicsEnvironment
094: .getLocalGraphicsEnvironment();
095: GraphicsDevice[] gds = ge.getScreenDevices();
096: for (GraphicsDevice gd : gds) {
097: GraphicsConfiguration gc = gd.getDefaultConfiguration();
098: virtualBounds = virtualBounds.union(gc.getBounds());
099: }
100:
101: int screenWidth = virtualBounds.width;
102: int screenHeight = virtualBounds.height;
103: SubstanceBinaryWatermark.watermarkImage = SubstanceCoreUtilities
104: .getBlankImage(screenWidth, screenHeight);
105:
106: Graphics2D graphics = (Graphics2D) SubstanceBinaryWatermark.watermarkImage
107: .getGraphics().create();
108: boolean status = this .drawWatermarkImage(graphics, 0, 0,
109: screenWidth, screenHeight, false);
110: graphics.dispose();
111: return status;
112: }
113:
114: /*
115: * (non-Javadoc)
116: *
117: * @see org.jvnet.substance.watermark.SubstanceWatermark#previewWatermark(java.awt.Graphics,
118: * int, int, int, int)
119: */
120: public void previewWatermark(Graphics g, int x, int y, int width,
121: int height) {
122: this .drawWatermarkImage((Graphics2D) g, x, y, width, height,
123: true);
124: }
125:
126: /**
127: * Draws the specified portion of the watermark image.
128: *
129: * @param graphics
130: * Graphic context.
131: * @param x
132: * the <i>x</i> coordinate of the watermark to be drawn.
133: * @param y
134: * The <i>y</i> coordinate of the watermark to be drawn.
135: * @param width
136: * The width of the watermark to be drawn.
137: * @param height
138: * The height of the watermark to be drawn.
139: * @param isPreview
140: * Indication whether the result is a preview image.
141: * @return Indication whether the draw succeeded.
142: */
143: private boolean drawWatermarkImage(Graphics2D graphics, int x,
144: int y, int width, int height, boolean isPreview) {
145:
146: Color stampColor = null;
147: int fontWidth = 0;
148: int fontHeight = 0;
149: if (isPreview) {
150: stampColor = SubstanceCoreUtilities
151: .isThemeDark(SubstanceLookAndFeel.getTheme()) ? Color.white
152: : Color.black;
153: graphics.setFont(tahomaPreview);
154: fontWidth = fontSize - 6;
155: fontHeight = fontSize - 4;
156: } else {
157: stampColor = SubstanceColorUtilities
158: .getWatermarkStampColor();
159: graphics.setFont(tahoma);
160: fontWidth = fontSize - 4;
161: fontHeight = fontSize - 2;
162: }
163:
164: graphics.setColor(stampColor);
165:
166: int rows = height / fontHeight;
167: int columns = width / fontWidth;
168: for (int col = x; col <= (x + columns); col++) {
169: for (int row = y; row <= (y + rows); row++) {
170: // choose random 0/1 letter
171: double val = isPreview ? Math.abs(Math
172: .sin((2.8 + col + columns * row))) : Math
173: .random();
174: char c = (val >= 0.5) ? '0' : '1';
175: graphics.drawString("" + c, col * fontWidth, fontHeight
176: * (row + 1));
177: }
178: }
179: return true;
180: }
181:
182: /*
183: * (non-Javadoc)
184: *
185: * @see org.jvnet.substance.watermark.SubstanceWatermark#getDisplayName()
186: */
187: public String getDisplayName() {
188: return SubstanceBinaryWatermark.getName();
189: }
190:
191: /**
192: * Returns the name of all watermarks of <code>this</code> class.
193: *
194: * @return The name of all watermarks of <code>this</code> class.
195: */
196: public static String getName() {
197: return "Binary";
198: }
199:
200: /*
201: * (non-Javadoc)
202: *
203: * @see org.jvnet.substance.watermark.SubstanceWatermark#isDependingOnTheme()
204: */
205: public boolean isDependingOnTheme() {
206: return true;
207: }
208:
209: /*
210: * (non-Javadoc)
211: *
212: * @see org.jvnet.substance.watermark.SubstanceWatermark#dispose()
213: */
214: public void dispose() {
215: watermarkImage = null;
216: }
217: }
|