001: package org.jvnet.substance.watermark;
002:
003: import java.awt.*;
004:
005: import org.jvnet.substance.SubstanceLookAndFeel;
006: import org.jvnet.substance.painter.noise.NoiseFactory;
007: import org.jvnet.substance.painter.noise.NoiseFilter;
008: import org.jvnet.substance.utils.*;
009:
010: /**
011: * Base class for noise-based watermarks. This class is part of officially
012: * supported API.
013: *
014: * @author Kirill Grouchnikov
015: */
016: public class SubstanceNoiseWatermark implements SubstanceWatermark {
017: /**
018: * Watermark image (screen-sized).
019: */
020: private Image watermarkImage = null;
021:
022: /**
023: * Display name of <code>this</code> watermark.
024: */
025: protected String displayName;
026:
027: /**
028: * Factor along X axis.
029: */
030: protected double xFactor;
031:
032: /**
033: * Factor along Y axis.
034: */
035: protected double yFactor;
036:
037: /**
038: * Indicates whether <code>this</code> watermark is based on constant Z
039: * values.
040: */
041: protected boolean hasConstantZ;
042:
043: /**
044: * Noise filter of <code>this</code> watermark.
045: */
046: protected NoiseFilter noiseFilter;
047:
048: /**
049: * Indicates whether the resulting image (after applying the
050: * {@link #noiseFilter}) should be blurred.
051: */
052: protected boolean toBlur;
053:
054: /**
055: * Creates a new noise-based watermark.
056: *
057: * @param displayName
058: * Display name of <code>this</code> watermark.
059: * @param xFactor
060: * Factor along X axis.
061: * @param yFactor
062: * Factor along Y axis.
063: * @param hasConstantZ
064: * Indicates whether <code>this</code> watermark is based on
065: * constant Z values.
066: * @param noiseFilter
067: * Noise filter of <code>this</code> watermark.
068: * @param toBlur
069: * Indicates whether the resulting image should be blurred.
070: */
071: public SubstanceNoiseWatermark(String displayName, double xFactor,
072: double yFactor, boolean hasConstantZ,
073: NoiseFilter noiseFilter, boolean toBlur) {
074: this .displayName = displayName;
075: this .xFactor = xFactor;
076: this .yFactor = yFactor;
077: this .hasConstantZ = hasConstantZ;
078: this .noiseFilter = noiseFilter;
079: this .toBlur = toBlur;
080: }
081:
082: /*
083: * (non-Javadoc)
084: *
085: * @see org.jvnet.substance.watermark.SubstanceWatermark#drawWatermarkImage(java.awt.Graphics,
086: * java.awt.Component, int, int, int, int)
087: */
088: public void drawWatermarkImage(Graphics graphics, Component c,
089: int x, int y, int width, int height) {
090: if (!c.isShowing())
091: return;
092: int dx = c.getLocationOnScreen().x;
093: int dy = c.getLocationOnScreen().y;
094: graphics.drawImage(this .watermarkImage, x, y, x + width, y
095: + height, x + dx, y + dy, x + dx + width, y + dy
096: + height, null);
097: }
098:
099: /*
100: * (non-Javadoc)
101: *
102: * @see org.jvnet.substance.watermark.SubstanceWatermark#updateWatermarkImage()
103: */
104: public boolean updateWatermarkImage() {
105: // fix by Chris for bug 67 - support for multiple screens
106: Rectangle virtualBounds = new Rectangle();
107: GraphicsEnvironment ge = GraphicsEnvironment
108: .getLocalGraphicsEnvironment();
109: GraphicsDevice[] gds = ge.getScreenDevices();
110: for (GraphicsDevice gd : gds) {
111: GraphicsConfiguration gc = gd.getDefaultConfiguration();
112: virtualBounds = virtualBounds.union(gc.getBounds());
113: }
114:
115: int screenWidth = virtualBounds.width;
116: int screenHeight = virtualBounds.height;
117: this .watermarkImage = SubstanceCoreUtilities.getBlankImage(
118: screenWidth, screenHeight);
119:
120: Graphics2D graphics = (Graphics2D) this .watermarkImage
121: .getGraphics().create();
122:
123: boolean status = this .drawWatermarkImage(graphics, 0, 0,
124: screenWidth, screenHeight, false);
125: graphics.dispose();
126: return status;
127: }
128:
129: /*
130: * (non-Javadoc)
131: *
132: * @see org.jvnet.substance.watermark.SubstanceWatermark#getDisplayName()
133: */
134: public String getDisplayName() {
135: return this .displayName;
136: }
137:
138: /*
139: * (non-Javadoc)
140: *
141: * @see org.jvnet.substance.watermark.SubstanceWatermark#isDependingOnTheme()
142: */
143: public boolean isDependingOnTheme() {
144: return true;
145: }
146:
147: /*
148: * (non-Javadoc)
149: *
150: * @see org.jvnet.substance.watermark.SubstanceWatermark#previewWatermark(java.awt.Graphics,
151: * int, int, int, int)
152: */
153: public void previewWatermark(Graphics g, int x, int y, int width,
154: int height) {
155: this .drawWatermarkImage((Graphics2D) g, x, y, width, height,
156: true);
157: }
158:
159: /**
160: * Draws the specified portion of the watermark image.
161: *
162: * @param graphics
163: * Graphic context.
164: * @param x
165: * the <i>x</i> coordinate of the watermark to be drawn.
166: * @param y
167: * The <i>y</i> coordinate of the watermark to be drawn.
168: * @param width
169: * The width of the watermark to be drawn.
170: * @param height
171: * The height of the watermark to be drawn.
172: * @param isPreview
173: * Indication whether the result is a preview image.
174: * @return Indication whether the draw succeeded.
175: */
176: private boolean drawWatermarkImage(Graphics2D graphics, int x,
177: int y, int width, int height, boolean isPreview) {
178: if (isPreview) {
179: graphics.drawImage(NoiseFactory.getNoiseImage(
180: SubstanceThemeUtilities.getTheme(null,
181: ComponentState.DEFAULT).getFirstTheme(),
182: SubstanceThemeUtilities.getTheme(null,
183: ComponentState.DEFAULT).getSecondTheme(),
184: width, height, this .xFactor, this .yFactor,
185: this .hasConstantZ, this .noiseFilter, this .toBlur,
186: true), x, y, null);
187: } else {
188: int alpha = SubstanceCoreUtilities
189: .isThemeDark(SubstanceLookAndFeel.getTheme()) ? 200
190: : 50;
191: graphics.setComposite(AlphaComposite.getInstance(
192: AlphaComposite.SRC_OVER, alpha / 255.0f));
193: graphics.drawImage(NoiseFactory.getNoiseImage(
194: SubstanceLookAndFeel.getTheme().getFirstTheme()
195: .getWatermarkTheme(), SubstanceLookAndFeel
196: .getTheme().getSecondTheme()
197: .getWatermarkTheme(), width, height,
198: this .xFactor, this .yFactor, this .hasConstantZ,
199: this .noiseFilter, this .toBlur, false), x, y, null);
200: }
201: return true;
202: }
203:
204: /*
205: * (non-Javadoc)
206: *
207: * @see org.jvnet.substance.watermark.SubstanceWatermark#dispose()
208: */
209: public void dispose() {
210: this.watermarkImage = null;
211: }
212: }
|