Debian Codegraph @

netx/net/sourceforge/jnlp/splashscreen/impls/defaultsplashscreen2012/TextWithWaterLevel.java

raw
1 /* TextWithWaterLevel.java
2 Copyright (C) 2012 Red Hat, Inc.
3
4 This file is part of IcedTea.
5
6 IcedTea is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 IcedTea is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with IcedTea; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
20
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
25
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
37 package net.sourceforge.jnlp.splashscreen.impls.defaultsplashscreen2012;
38
39 import java.awt.Polygon;
40 import java.awt.Color;
41 import java.awt.Font;
42 import java.awt.FontMetrics;
43 import java.awt.Graphics2D;
44 import java.awt.Point;
45 import java.awt.Rectangle;
46 import java.awt.RenderingHints;
47 import java.awt.image.BufferedImage;
48 import java.util.Random;
49
50 public class TextWithWaterLevel extends TextOutlineRenderer {
51
52 private Color waterColor;
53 private Color bgColor;
54 private int percentageOfWater;
55 private Random sea = new Random();
56 //set to null befor getBackground if waving is needed
57 //or create new TWL ;)
58 private Polygon cachedPolygon;
59
60 public TextWithWaterLevel(String s, Font f) {
61 super(f, s);
62 waterColor = Color.BLUE;
63 bgColor = Color.white;
64
65 }
66
67 protected Point getFutureSize() {
68 BufferedImage bi = new BufferedImage(10, 10, BufferedImage.TYPE_INT_ARGB);
69 FontMetrics fm = bi.createGraphics().getFontMetrics(getFont());
70 int w = fm.stringWidth(getText());
71 int h = fm.getHeight();
72 return new Point(w, h);
73 }
74
75 public BufferedImage getBackground() {
76 Point p = getFutureSize();
77 int w = p.x;
78 int h = p.y;
79 if (w <= 0 || h <= 0) {
80 return null;
81 }
82 BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
83 Graphics2D g2d = bi.createGraphics();
84 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
85 g2d.setColor(bgColor);
86 g2d.fillRect(0, 0, w, h);
87 if (cachedPolygon == null) {
88 int level = (h * percentageOfWater) / 100;
89 int waveHeight = 10;
90 int waveLength = 20;
91 if (level > waveHeight / 2 + 1) {
92 NatCubic line = new NatCubic();
93 int x = 0;
94 while (x < w + 2 * waveLength) {
95 line.addPoint(x, h - level - waveHeight / 2 - sea.nextInt(waveHeight));
96 x = x + waveLength;
97 }
98 cachedPolygon = line.calcualteResult();
99 cachedPolygon.addPoint(w, h);
100 cachedPolygon.addPoint(0, h);
101 }
102 }
103 g2d.setColor(waterColor);
104 if (cachedPolygon != null) {
105 g2d.fillPolygon(cachedPolygon);
106 }
107 //line.paint(g2d);
108 //FlodFill.floodFill(bi, waterColor, new Point(1, h - 1));
109 return bi;
110 }
111
112 public Polygon getCachedPolygon() {
113 return cachedPolygon;
114 }
115
116 public void setCachedPolygon(Polygon cachedPolygon) {
117 this.cachedPolygon = cachedPolygon;
118 }
119
120 @Override
121 public Rectangle cutTo(Graphics2D g2, int x, int y) {
122 if (this.getImg() == null) {
123 this.setImg(getBackground());
124 }
125 if (this.getImg() == null) {
126 return null;
127 }
128 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
129 g2.setFont(getFont());
130 g2.setColor(getTextOutline());
131 g2.drawString(getText(), x, y - 2);
132 g2.drawString(getText(), x, y + 2);
133 g2.drawString(getText(), x - 2, y);
134 g2.drawString(getText(), x + 2, y);
135 //sorry, cuted text have disturbed borders
136 return super.cutTo(g2, x, y);
137 }
138
139 /**
140 * @return the waterColor
141 */
142 public Color getWaterColor() {
143 return waterColor;
144 }
145
146 /**
147 * @param waterColor the waterColor to set
148 */
149 public void setWaterColor(Color waterColor) {
150 this.waterColor = waterColor;
151 }
152
153 /**
154 * @return the bgColor
155 */
156 public Color getBgColor() {
157 return bgColor;
158 }
159
160 /**
161 * @param bgColor the bgColor to set
162 */
163 public void setBgColor(Color bgColor) {
164 this.bgColor = bgColor;
165 }
166
167 /**
168 * @return the percentageOfWater
169 */
170 public int getPercentageOfWater() {
171 return percentageOfWater;
172 }
173
174 /**
175 * @param percentageOfWater the percentageOfWater to set
176 */
177 public void setPercentageOfWater(int percentageOfWater) {
178 this.percentageOfWater = percentageOfWater;
179 }
180 }