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

« back to all changes in this revision

Viewing changes to substance/src/main/java/org/pushingpixels/substance/api/painter/decoration/ImageWrapperDecorationPainter.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.painter.decoration;
 
31
 
 
32
import java.awt.*;
 
33
import java.awt.image.BufferedImage;
 
34
import java.util.LinkedHashMap;
 
35
import java.util.Map;
 
36
 
 
37
import org.pushingpixels.lafwidget.LafWidgetUtilities;
 
38
import org.pushingpixels.substance.api.*;
 
39
import org.pushingpixels.substance.api.painter.SubstancePainterUtils;
 
40
import org.pushingpixels.substance.internal.utils.SubstanceImageCreator;
 
41
 
 
42
/**
 
43
 * Implementation of {@link SubstanceDecorationPainter} that uses brushed metal
 
44
 * painting on decoration areas.
 
45
 * 
 
46
 * @author Kirill Grouchnikov
 
47
 * @since version 4.3
 
48
 */
 
49
public abstract class ImageWrapperDecorationPainter implements
 
50
                SubstanceDecorationPainter {
 
51
        /**
 
52
         * Contains the original (not colorized) image of this painter.
 
53
         */
 
54
        protected Image originalTile = null;
 
55
 
 
56
        /**
 
57
         * The base decoration painter - the colorized image tiles are painted over
 
58
         * the painting of this painter. Can be <code>null</code>.
 
59
         */
 
60
        protected SubstanceDecorationPainter baseDecorationPainter;
 
61
 
 
62
        /**
 
63
         * Map of colorized tiles.
 
64
         */
 
65
        protected LinkedHashMap<String, Image> colorizedTileMap;
 
66
 
 
67
        /**
 
68
         * Alpha channel for the texture image (colorized tiles applied on top of
 
69
         * the {@link #baseDecorationPainter} painting).
 
70
         */
 
71
        protected float textureAlpha;
 
72
 
 
73
        /**
 
74
         * Creates a new image wrapper decoration painter.
 
75
         */
 
76
        public ImageWrapperDecorationPainter() {
 
77
                this.textureAlpha = 0.3f;
 
78
 
 
79
                this.colorizedTileMap = new LinkedHashMap<String, Image>() {
 
80
                        @Override
 
81
                        protected boolean removeEldestEntry(Map.Entry<String, Image> eldest) {
 
82
                                return this.size() > 10;
 
83
                        }
 
84
                };
 
85
        }
 
86
 
 
87
        /*
 
88
         * (non-Javadoc)
 
89
         * 
 
90
         * 
 
91
         * 
 
92
         * 
 
93
         * 
 
94
         * 
 
95
         * 
 
96
         * @seeorg.pushingpixels.substance.painter.decoration.SubstanceDecorationPainter
 
97
         * # paintDecorationArea(java.awt.Graphics2D, java.awt.Component,
 
98
         * org.pushingpixels.substance.painter.decoration.DecorationAreaType, int,
 
99
         * int, org.pushingpixels.substance.api.SubstanceSkin)
 
100
         */
 
101
        @Override
 
102
    public void paintDecorationArea(Graphics2D graphics, Component comp,
 
103
                        DecorationAreaType decorationAreaType, int width, int height,
 
104
                        SubstanceSkin skin) {
 
105
                if ((decorationAreaType == DecorationAreaType.PRIMARY_TITLE_PANE)
 
106
                                || (decorationAreaType == DecorationAreaType.SECONDARY_TITLE_PANE)) {
 
107
                        this.paintTitleBackground(graphics, comp, decorationAreaType,
 
108
                                        width, height, skin);
 
109
                } else {
 
110
                        this.paintExtraBackground(graphics, comp, decorationAreaType,
 
111
                                        width, height, skin);
 
112
                }
 
113
        }
 
114
 
 
115
        /**
 
116
         * Paints the title background.
 
117
         * 
 
118
         * @param graphics
 
119
         *            Graphics context.
 
120
         * @param comp
 
121
         *            Component.
 
122
         * @param decorationAreaType
 
123
         *            Decoration area type. Must not be <code>null</code>.
 
124
         * @param width
 
125
         *            Width.
 
126
         * @param height
 
127
         *            Height.
 
128
         * @param skin
 
129
         *            Skin for painting the title background.
 
130
         */
 
131
        private void paintTitleBackground(Graphics2D graphics, Component comp,
 
132
                        DecorationAreaType decorationAreaType, int width, int height,
 
133
                        SubstanceSkin skin) {
 
134
 
 
135
                SubstanceColorScheme tileScheme = skin
 
136
                                .getBackgroundColorScheme(decorationAreaType);
 
137
                if (this.baseDecorationPainter == null) {
 
138
                        graphics.setColor(tileScheme.getMidColor());
 
139
                        graphics.fillRect(0, 0, width, height);
 
140
                } else {
 
141
                        this.baseDecorationPainter.paintDecorationArea(graphics, comp,
 
142
                                        decorationAreaType, width, height, skin);
 
143
                }
 
144
 
 
145
                Graphics2D temp = (Graphics2D) graphics.create();
 
146
                this.tileArea(temp, comp, tileScheme, 0, 0, 0, 0, width, height);
 
147
                temp.dispose();
 
148
        }
 
149
 
 
150
        /**
 
151
         * Paints the background of non-title decoration areas.
 
152
         * 
 
153
         * @param graphics
 
154
         *            Graphics context.
 
155
         * @param comp
 
156
         *            Component.
 
157
         * @param decorationAreaType
 
158
         *            Decoration area type. Must not be <code>null</code>.
 
159
         * @param width
 
160
         *            Width.
 
161
         * @param height
 
162
         *            Height.
 
163
         * @param skin
 
164
         *            Skin for painting the background of non-title decoration
 
165
         *            areas.
 
166
         */
 
167
        private void paintExtraBackground(Graphics2D graphics, Component comp,
 
168
                        DecorationAreaType decorationAreaType, int width, int height,
 
169
                        SubstanceSkin skin) {
 
170
 
 
171
                Point offset = SubstancePainterUtils.getOffsetInRootPaneCoords(comp);
 
172
 
 
173
                SubstanceColorScheme tileScheme = skin
 
174
                                .getBackgroundColorScheme(decorationAreaType);
 
175
                if (this.baseDecorationPainter != null) {
 
176
                        this.baseDecorationPainter.paintDecorationArea(graphics, comp,
 
177
                                        decorationAreaType, width, height, skin);
 
178
                } else {
 
179
                        graphics.setColor(tileScheme.getMidColor());
 
180
                        graphics.fillRect(0, 0, width, height);
 
181
                }
 
182
                Graphics2D temp = (Graphics2D) graphics.create();
 
183
                this.tileArea(temp, comp, tileScheme, offset.x, offset.y, 0, 0, width,
 
184
                                height);
 
185
                temp.dispose();
 
186
        }
 
187
 
 
188
        /**
 
189
         * Tiles the specified area with colorized version of the image tile. This
 
190
         * is called after the {@link #baseDecorationPainter} has painted the area.
 
191
         * This method should respect the current {@link #textureAlpha} value.
 
192
         * 
 
193
         * @param g
 
194
         *            Graphic context.
 
195
         * @param comp
 
196
         *            Component.
 
197
         * @param tileScheme
 
198
         *            Scheme for the tile colorization.
 
199
         * @param offsetTextureX
 
200
         *            X offset for the tiling.
 
201
         * @param offsetTextureY
 
202
         *            Y offset for the tiling.
 
203
         * @param x
 
204
         *            X coordinate of the tiling region.
 
205
         * @param y
 
206
         *            Y coordinate of the tiling region.
 
207
         * @param width
 
208
         *            Width of the tiling region.
 
209
         * @param height
 
210
         *            Height of the tiling region.
 
211
         */
 
212
        protected void tileArea(Graphics2D g, Component comp,
 
213
                        SubstanceColorScheme tileScheme, int offsetTextureX,
 
214
                        int offsetTextureY, int x, int y, int width, int height) {
 
215
 
 
216
                Graphics2D graphics = (Graphics2D) g.create();
 
217
                graphics.setComposite(LafWidgetUtilities.getAlphaComposite(comp,
 
218
                                this.textureAlpha, g));
 
219
 
 
220
                Image colorizedTile = this.getColorizedTile(tileScheme);
 
221
                int tileWidth = colorizedTile.getWidth(null);
 
222
                int tileHeight = colorizedTile.getHeight(null);
 
223
 
 
224
                offsetTextureX = offsetTextureX % tileWidth;
 
225
                offsetTextureY = offsetTextureY % tileHeight;
 
226
                int currTileTop = -offsetTextureY;
 
227
                do {
 
228
                        int currTileLeft = -offsetTextureX;
 
229
                        do {
 
230
                                graphics.drawImage(colorizedTile, currTileLeft, currTileTop,
 
231
                                                null);
 
232
                                currTileLeft += tileWidth;
 
233
                        } while (currTileLeft < width);
 
234
                        currTileTop += tileHeight;
 
235
                } while (currTileTop < height);
 
236
 
 
237
                graphics.dispose();
 
238
        }
 
239
 
 
240
        /**
 
241
         * Sets the base decoration painter.
 
242
         * 
 
243
         * @param baseDecorationPainter
 
244
         *            Base decoration painter.
 
245
         */
 
246
        public void setBaseDecorationPainter(
 
247
                        SubstanceDecorationPainter baseDecorationPainter) {
 
248
                this.baseDecorationPainter = baseDecorationPainter;
 
249
        }
 
250
 
 
251
        /**
 
252
         * Sets the alpha channel for the image texture.
 
253
         * 
 
254
         * @param textureAlpha
 
255
         *            Alpha channel for the image texture.
 
256
         */
 
257
        public void setTextureAlpha(float textureAlpha) {
 
258
                this.textureAlpha = textureAlpha;
 
259
        }
 
260
 
 
261
        /**
 
262
         * Returns a colorized image tile.
 
263
         * 
 
264
         * @param scheme
 
265
         *            Color scheme for the colorization.
 
266
         * @return Colorized tile.
 
267
         */
 
268
        protected Image getColorizedTile(SubstanceColorScheme scheme) {
 
269
                Image result = this.colorizedTileMap.get(scheme.getDisplayName());
 
270
                if (result == null) {
 
271
                        BufferedImage tileBi = new BufferedImage(this.originalTile
 
272
                                        .getWidth(null), this.originalTile.getHeight(null),
 
273
                                        BufferedImage.TYPE_INT_ARGB);
 
274
                        tileBi.getGraphics().drawImage(this.originalTile, 0, 0, null);
 
275
                        result = SubstanceImageCreator.getColorSchemeImage(tileBi, scheme,
 
276
                                        0.0f);
 
277
                        this.colorizedTileMap.put(scheme.getDisplayName(), result);
 
278
                }
 
279
                return result;
 
280
        }
 
281
}