~ubuntu-branches/debian/stretch/insubstantial/stretch

« back to all changes in this revision

Viewing changes to substance/src/main/java/org/pushingpixels/substance/api/watermark/SubstanceCrosshatchWatermark.java

  • Committer: Package Import Robot
  • Author(s): Felix Natter
  • Date: 2016-01-18 20:58:45 UTC
  • Revision ID: package-import@ubuntu.com-20160118205845-crbmrkda61qsi5qa
Tags: upstream-7.3+dfsg2
ImportĀ upstreamĀ versionĀ 7.3+dfsg2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (c) 2005-2010 Substance Kirill Grouchnikov. All Rights Reserved.
 
3
 *
 
4
 * Redistribution and use in source and binary forms, with or without 
 
5
 * modification, are permitted provided that the following conditions are met:
 
6
 * 
 
7
 *  o Redistributions of source code must retain the above copyright notice, 
 
8
 *    this list of conditions and the following disclaimer. 
 
9
 *     
 
10
 *  o Redistributions in binary form must reproduce the above copyright notice, 
 
11
 *    this list of conditions and the following disclaimer in the documentation 
 
12
 *    and/or other materials provided with the distribution. 
 
13
 *     
 
14
 *  o Neither the name of Substance Kirill Grouchnikov nor the names of 
 
15
 *    its contributors may be used to endorse or promote products derived 
 
16
 *    from this software without specific prior written permission. 
 
17
 *     
 
18
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
 
19
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 
 
20
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
 
21
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 
 
22
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
 
23
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
 
24
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 
 
25
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
 
26
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 
 
27
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 
 
28
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 
29
 */
 
30
package org.pushingpixels.substance.api.watermark;
 
31
 
 
32
import java.awt.*;
 
33
import java.awt.image.BufferedImage;
 
34
 
 
35
import org.pushingpixels.substance.api.SubstanceColorScheme;
 
36
import org.pushingpixels.substance.api.SubstanceSkin;
 
37
import org.pushingpixels.substance.internal.utils.SubstanceCoreUtilities;
 
38
 
 
39
/**
 
40
 * Simple implementation of {@link SubstanceWatermark}, drawing cross hatches as
 
41
 * watermark. This implementation is inspired by Office 12 background. This
 
42
 * class is part of officially supported API.
 
43
 * 
 
44
 * @author Kirill Grouchnikov
 
45
 * @author Chris Hall
 
46
 */
 
47
public class SubstanceCrosshatchWatermark implements SubstanceWatermark {
 
48
        /**
 
49
         * Watermark image (screen-sized).
 
50
         */
 
51
        private static Image watermarkImage = null;
 
52
 
 
53
        /*
 
54
         * (non-Javadoc)
 
55
         * 
 
56
         * @see
 
57
         * org.pushingpixels.substance.watermark.SubstanceWatermark#drawWatermarkImage(java
 
58
         * .awt.Graphics, int, int, int, int)
 
59
         */
 
60
        @Override
 
61
    public void drawWatermarkImage(Graphics graphics, Component c, int x,
 
62
                        int y, int width, int height) {
 
63
                if (!c.isShowing())
 
64
                        return;
 
65
                int dx = c.getLocationOnScreen().x;
 
66
                int dy = c.getLocationOnScreen().y;
 
67
                graphics.drawImage(SubstanceCrosshatchWatermark.watermarkImage, x, y, x
 
68
                                + width, y + height, x + dx, y + dy, x + dx + width, y + dy
 
69
                                + height, null);
 
70
        }
 
71
 
 
72
        /*
 
73
         * (non-Javadoc)
 
74
         * 
 
75
         * @see
 
76
         * org.pushingpixels.substance.watermark.SubstanceWatermark#updateWatermarkImage
 
77
         * (org.pushingpixels.substance.skin.SubstanceSkin)
 
78
         */
 
79
        @Override
 
80
    public boolean updateWatermarkImage(SubstanceSkin skin) {
 
81
                // fix by Chris for bug 67 - support for multiple screens
 
82
                Rectangle virtualBounds = new Rectangle();
 
83
                GraphicsEnvironment ge = GraphicsEnvironment
 
84
                                .getLocalGraphicsEnvironment();
 
85
                GraphicsDevice[] gds = ge.getScreenDevices();
 
86
                for (GraphicsDevice gd : gds) {
 
87
                        GraphicsConfiguration gc = gd.getDefaultConfiguration();
 
88
                        virtualBounds = virtualBounds.union(gc.getBounds());
 
89
                }
 
90
 
 
91
                int screenWidth = virtualBounds.width;
 
92
                int screenHeight = virtualBounds.height;
 
93
                SubstanceCrosshatchWatermark.watermarkImage = SubstanceCoreUtilities
 
94
                                .getBlankImage(screenWidth, screenHeight);
 
95
 
 
96
                Graphics2D graphics = (Graphics2D) SubstanceCrosshatchWatermark.watermarkImage
 
97
                                .getGraphics().create();
 
98
                boolean status = this.drawWatermarkImage(skin, graphics, 0, 0,
 
99
                                screenWidth, screenHeight, false);
 
100
                graphics.dispose();
 
101
                return status;
 
102
        }
 
103
 
 
104
        /*
 
105
         * (non-Javadoc)
 
106
         * 
 
107
         * @see
 
108
         * org.pushingpixels.substance.api.watermark.SubstanceWatermark#previewWatermark
 
109
         * (java.awt.Graphics, org.pushingpixels.substance.api.SubstanceSkin, int, int, int,
 
110
         * int)
 
111
         */
 
112
        @Override
 
113
        public void previewWatermark(Graphics g, SubstanceSkin skin, int x, int y,
 
114
                        int width, int height) {
 
115
                this
 
116
                                .drawWatermarkImage(skin, (Graphics2D) g, x, y, width, height,
 
117
                                                true);
 
118
        }
 
119
 
 
120
        /**
 
121
         * Draws the specified portion of the watermark image.
 
122
         * 
 
123
         * @param skin
 
124
         *            Skin to use for painting the watermark.
 
125
         * @param graphics
 
126
         *            Graphic context.
 
127
         * @param x
 
128
         *            the <i>x</i> coordinate of the watermark to be drawn.
 
129
         * @param y
 
130
         *            The <i>y</i> coordinate of the watermark to be drawn.
 
131
         * @param width
 
132
         *            The width of the watermark to be drawn.
 
133
         * @param height
 
134
         *            The height of the watermark to be drawn.
 
135
         * @param isPreview
 
136
         *            Indication whether the result is a preview image.
 
137
         * @return Indication whether the draw succeeded.
 
138
         */
 
139
        private boolean drawWatermarkImage(SubstanceSkin skin, Graphics2D graphics,
 
140
                        int x, int y, int width, int height, boolean isPreview) {
 
141
                Color stampColorDark = null;
 
142
                Color stampColorAll = null;
 
143
                Color stampColorLight = null;
 
144
                SubstanceColorScheme scheme = skin.getWatermarkColorScheme();
 
145
                if (isPreview) {
 
146
                        stampColorDark = scheme.isDark() ? Color.white : Color.black;
 
147
                        stampColorAll = Color.lightGray;
 
148
                        stampColorLight = scheme.isDark() ? Color.black : Color.white;
 
149
                } else {
 
150
                        stampColorDark = scheme.getWatermarkDarkColor();
 
151
                        stampColorAll = scheme.getWatermarkStampColor();
 
152
                        stampColorLight = scheme.getWatermarkLightColor();
 
153
                }
 
154
 
 
155
                graphics.setColor(stampColorAll);
 
156
                graphics.fillRect(0, 0, width, height);
 
157
 
 
158
                BufferedImage tile = SubstanceCoreUtilities.getBlankImage(4, 4);
 
159
                tile.setRGB(0, 0, stampColorDark.getRGB());
 
160
                tile.setRGB(2, 2, stampColorDark.getRGB());
 
161
                tile.setRGB(0, 1, stampColorLight.getRGB());
 
162
                tile.setRGB(2, 3, stampColorLight.getRGB());
 
163
 
 
164
                Graphics2D g2d = (Graphics2D) graphics.create();
 
165
                g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
 
166
                                0.4f));
 
167
                for (int row = y; row < (y + height); row += 4) {
 
168
                        for (int col = x; col < (x + width); col += 4) {
 
169
                                g2d.drawImage(tile, col, row, null);
 
170
                        }
 
171
                }
 
172
                g2d.dispose();
 
173
                return true;
 
174
        }
 
175
 
 
176
        /*
 
177
         * (non-Javadoc)
 
178
         * 
 
179
         * @see org.pushingpixels.substance.api.trait.SubstanceTrait#getDisplayName()
 
180
         */
 
181
        @Override
 
182
    public String getDisplayName() {
 
183
                return SubstanceCrosshatchWatermark.getName();
 
184
        }
 
185
 
 
186
        /**
 
187
         * Returns the name of all watermarks of <code>this</code> class.
 
188
         * 
 
189
         * @return The name of all watermarks of <code>this</code> class.
 
190
         */
 
191
        public static String getName() {
 
192
                return "Crosshatch";
 
193
        }
 
194
 
 
195
        /*
 
196
         * (non-Javadoc)
 
197
         * 
 
198
         * @see org.pushingpixels.substance.watermark.SubstanceWatermark#dispose()
 
199
         */
 
200
        @Override
 
201
    public void dispose() {
 
202
                watermarkImage = null;
 
203
        }
 
204
}