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: import java.io.InputStream;
034:
035: import org.jvnet.substance.SubstanceLookAndFeel;
036: import org.jvnet.substance.utils.SubstanceColorUtilities;
037: import org.jvnet.substance.utils.SubstanceCoreUtilities;
038:
039: /**
040: * Implementation of {@link org.jvnet.substance.watermark.SubstanceWatermark},
041: * drawing random Katakana glyphs as watermark. This class is part of officially
042: * supported API.
043: *
044: * @author Kirill Grouchnikov
045: * @author Chris Hall
046: */
047: public class SubstanceKatakanaWatermark implements SubstanceWatermark {
048: /**
049: * Watermark image (screen-sized).
050: */
051: private static Image watermarkImage = null;
052:
053: /**
054: * Font size.
055: */
056: private static int fontSize = 14;
057:
058: /**
059: * Font instance.
060: */
061: private static Font font = getFont();
062:
063: /**
064: * Returns the font instance from the bundled resource.
065: *
066: * @return Font instance from the bundled resource.
067: */
068: private static Font getFont() {
069: // the following is fix by Dag Joar and Christian Schlichtherle
070: // for application running with -Xbootclasspath VM flag. In this case,
071: // the using MyClass.class.getClassLoader() would return null,
072: // but the context class loader will function properly
073: // that classes will be properly loaded regardless of whether the lib is
074: // added to the system class path, the extension class path and
075: // regardless of the class loader architecture set up by some
076: // frameworks.
077: ClassLoader cl = SubstanceCoreUtilities
078: .getClassLoaderForResources();
079: // InputStream is = cl.getResourceAsStream("resource/matrix code
080: // nfi.ttf");
081: InputStream is = cl
082: .getResourceAsStream("resource/katakana.ttf");
083: if (is != null) {
084: try {
085: Font kf = Font.createFont(Font.TRUETYPE_FONT, is);
086: int fontSize = 14;
087: return kf.deriveFont(Font.BOLD, fontSize);
088: } catch (Exception exc) {
089: }
090: }
091: return null;
092: }
093:
094: /*
095: * (non-Javadoc)
096: *
097: * @see org.jvnet.substance.watermark.SubstanceWatermark#drawWatermarkImage(java.awt.Graphics,
098: * int, int, int, int)
099: */
100: public void drawWatermarkImage(Graphics graphics, Component c,
101: int x, int y, int width, int height) {
102: if (!c.isShowing())
103: return;
104: int dx = c.getLocationOnScreen().x;
105: int dy = c.getLocationOnScreen().y;
106: graphics.drawImage(SubstanceKatakanaWatermark.watermarkImage,
107: x, y, x + width, y + height, x + dx, y + dy, x + dx
108: + width, y + dy + height, null);
109: }
110:
111: /*
112: * (non-Javadoc)
113: *
114: * @see org.jvnet.substance.watermark.SubstanceWatermark#updateWatermarkImage()
115: */
116: public boolean updateWatermarkImage() {
117: if (font == null)
118: return false;
119: // fix by Chris for bug 67 - support for multiple screens
120: Rectangle virtualBounds = new Rectangle();
121: GraphicsEnvironment ge = GraphicsEnvironment
122: .getLocalGraphicsEnvironment();
123: GraphicsDevice[] gds = ge.getScreenDevices();
124: for (GraphicsDevice gd : gds) {
125: GraphicsConfiguration gc = gd.getDefaultConfiguration();
126: virtualBounds = virtualBounds.union(gc.getBounds());
127: }
128:
129: int screenWidth = virtualBounds.width;
130: int screenHeight = virtualBounds.height;
131: SubstanceKatakanaWatermark.watermarkImage = SubstanceCoreUtilities
132: .getBlankImage(screenWidth, screenHeight);
133:
134: Graphics2D graphics = (Graphics2D) SubstanceKatakanaWatermark.watermarkImage
135: .getGraphics().create();
136:
137: boolean status = this .drawWatermarkImage(graphics, 0, 0,
138: screenWidth, screenHeight, false);
139: graphics.dispose();
140: return status;
141: }
142:
143: /*
144: * (non-Javadoc)
145: *
146: * @see org.jvnet.substance.watermark.SubstanceWatermark#previewWatermark(java.awt.Graphics,
147: * int, int, int, int)
148: */
149: public void previewWatermark(Graphics g, int x, int y, int width,
150: int height) {
151: this .drawWatermarkImage((Graphics2D) g, x, y, width, height,
152: true);
153: }
154:
155: /**
156: * Draws the specified portion of the watermark image.
157: *
158: * @param graphics
159: * Graphic context.
160: * @param x
161: * the <i>x</i> coordinate of the watermark to be drawn.
162: * @param y
163: * The <i>y</i> coordinate of the watermark to be drawn.
164: * @param width
165: * The width of the watermark to be drawn.
166: * @param height
167: * The height of the watermark to be drawn.
168: * @param isPreview
169: * Indication whether the result is a preview image.
170: * @return Indication whether the draw succeeded.
171: */
172: private boolean drawWatermarkImage(Graphics2D graphics, int x,
173: int y, int width, int height, boolean isPreview) {
174:
175: Color stampColor = null;
176: if (isPreview) {
177: stampColor = SubstanceCoreUtilities
178: .isThemeDark(SubstanceLookAndFeel.getTheme()) ? Color.white
179: : Color.black;
180: } else {
181: stampColor = SubstanceColorUtilities
182: .getWatermarkStampColor();
183: }
184:
185: graphics.setColor(stampColor);
186:
187: graphics.setFont(font);
188: int fontWidth = fontSize;
189: int fontHeight = fontSize - 2;
190: int rows = height / fontHeight;
191: int columns = width / fontWidth;
192: for (int col = 0; col <= columns; col++) {
193: for (int row = 0; row <= rows; row++) {
194: // choose random katakana letter
195: // int letterIndex = (int) (0x30A0 + Math.random()
196: // * (0x30FF - 0x30A0));
197: int letterIndex = isPreview ? (33 + (col + columns
198: * row) % 95) : (int) (33 + Math.random() * 95);
199: char c = (char) letterIndex;
200: graphics.drawString("" + c, x + col * fontWidth, y
201: + fontHeight * (row + 1));
202: }
203: }
204: return true;
205: }
206:
207: /*
208: * (non-Javadoc)
209: *
210: * @see org.jvnet.substance.watermark.SubstanceWatermark#getDisplayName()
211: */
212: public String getDisplayName() {
213: return SubstanceKatakanaWatermark.getName();
214: }
215:
216: /**
217: * Returns the name of all watermarks of <code>this</code> class.
218: *
219: * @return The name of all watermarks of <code>this</code> class.
220: */
221: public static String getName() {
222: return "Katakana";
223: }
224:
225: /*
226: * (non-Javadoc)
227: *
228: * @see org.jvnet.substance.watermark.SubstanceWatermark#isDependingOnTheme()
229: */
230: public boolean isDependingOnTheme() {
231: return true;
232: }
233:
234: /*
235: * (non-Javadoc)
236: *
237: * @see org.jvnet.substance.watermark.SubstanceWatermark#dispose()
238: */
239: public void dispose() {
240: watermarkImage = null;
241: }
242: }
|