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: * Simple implementation of
040: * {@link org.jvnet.substance.watermark.SubstanceWatermark}, drawing darker
041: * even lines as watermark. This is the default watermark implementation. This
042: * class is part of officially supported API.
043: *
044: * @author Kirill Grouchnikov
045: * @author Chris Hall
046: */
047: public class SubstanceStripeWatermark implements SubstanceWatermark {
048: /**
049: * Watermark image (screen-sized).
050: */
051: private static Image watermarkImage = null;
052:
053: /*
054: * (non-Javadoc)
055: *
056: * @see org.jvnet.substance.watermark.SubstanceWatermark#drawWatermarkImage(java.awt.Graphics,
057: * int, int, int, int)
058: */
059: public void drawWatermarkImage(Graphics graphics, Component c,
060: int x, int y, int width, int height) {
061: if (!c.isShowing())
062: return;
063: // System.err.println(System.currentTimeMillis() + ":"
064: // + c.getClass().getName() + ":" + x + ":" + y + ":" + width
065: // + ":" + height);
066: int dx = c.getLocationOnScreen().x;
067: int dy = c.getLocationOnScreen().y;
068: graphics.drawImage(SubstanceStripeWatermark.watermarkImage, x,
069: y, x + width, y + height, x + dx, y + dy, x + dx
070: + width, y + dy + height, null);
071: }
072:
073: /*
074: * (non-Javadoc)
075: *
076: * @see org.jvnet.substance.watermark.SubstanceWatermark#updateWatermarkImage()
077: */
078: public boolean updateWatermarkImage() {
079: // fix by Chris for bug 67 - support for multiple screens
080: Rectangle virtualBounds = new Rectangle();
081: GraphicsEnvironment ge = GraphicsEnvironment
082: .getLocalGraphicsEnvironment();
083: GraphicsDevice[] gds = ge.getScreenDevices();
084: for (GraphicsDevice gd : gds) {
085: GraphicsConfiguration gc = gd.getDefaultConfiguration();
086: virtualBounds = virtualBounds.union(gc.getBounds());
087: }
088:
089: int screenWidth = virtualBounds.width;
090: int screenHeight = virtualBounds.height;
091: SubstanceStripeWatermark.watermarkImage = SubstanceCoreUtilities
092: .getBlankImage(screenWidth, screenHeight);
093:
094: Graphics2D graphics = (Graphics2D) SubstanceStripeWatermark.watermarkImage
095: .getGraphics().create();
096:
097: boolean status = this .drawWatermarkImage(graphics, 0, 0,
098: screenWidth, screenHeight, false);
099: graphics.dispose();
100: return status;
101: }
102:
103: /*
104: * (non-Javadoc)
105: *
106: * @see org.jvnet.substance.watermark.SubstanceWatermark#previewWatermark(java.awt.Graphics,
107: * int, int, int, int)
108: */
109: public void previewWatermark(Graphics g, int x, int y, int width,
110: int height) {
111: this .drawWatermarkImage((Graphics2D) g, x, y, width, height,
112: true);
113: }
114:
115: /**
116: * Draws the specified portion of the watermark image.
117: *
118: * @param graphics
119: * Graphic context.
120: * @param x
121: * the <i>x</i> coordinate of the watermark to be drawn.
122: * @param y
123: * The <i>y</i> coordinate of the watermark to be drawn.
124: * @param width
125: * The width of the watermark to be drawn.
126: * @param height
127: * The height of the watermark to be drawn.
128: * @param isPreview
129: * Indication whether the result is a preview image.
130: * @return Indication whether the draw succeeded.
131: */
132: private boolean drawWatermarkImage(Graphics2D graphics, int x,
133: int y, int width, int height, boolean isPreview) {
134: Color stampColor = null;
135: if (isPreview)
136: stampColor = SubstanceCoreUtilities
137: .isThemeDark(SubstanceLookAndFeel.getTheme()) ? Color.lightGray
138: : Color.darkGray;
139: else {
140: stampColor = SubstanceColorUtilities
141: .getWatermarkStampColor();
142: }
143:
144: graphics.setColor(stampColor);
145: for (int row = y; row < (y + height); row += 2) {
146: graphics.drawLine(x, row, x + width, row);
147: }
148: if (isPreview) {
149: graphics.setColor(Color.gray);
150: for (int row = y + 1; row < (y + height); row += 2) {
151: graphics.drawLine(x, row, x + width, row);
152: }
153: }
154: return true;
155: }
156:
157: /*
158: * (non-Javadoc)
159: *
160: * @see org.jvnet.substance.watermark.SubstanceWatermark#getDisplayName()
161: */
162: public String getDisplayName() {
163: return SubstanceStripeWatermark.getName();
164: }
165:
166: /**
167: * Returns the name of all watermarks of <code>this</code> class.
168: *
169: * @return The name of all watermarks of <code>this</code> class.
170: */
171: public static String getName() {
172: return "Stripes";
173: }
174:
175: /*
176: * (non-Javadoc)
177: *
178: * @see org.jvnet.substance.watermark.SubstanceWatermark#isDependingOnTheme()
179: */
180: public boolean isDependingOnTheme() {
181: return true;
182: }
183:
184: /*
185: * (non-Javadoc)
186: *
187: * @see org.jvnet.substance.watermark.SubstanceWatermark#dispose()
188: */
189: public void dispose() {
190: watermarkImage = null;
191: }
192: }
|