| 1 |
/* |
| 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.parts.extensions; |
| 38 |
|
| 39 |
import java.awt.Color; |
| 40 |
import java.awt.Graphics; |
| 41 |
import java.awt.Polygon; |
| 42 |
import java.util.ArrayList; |
| 43 |
import java.util.List; |
| 44 |
import java.util.Random; |
| 45 |
import net.sourceforge.jnlp.splashscreen.impls.defaultsplashscreen2012.BasePainter; |
| 46 |
import net.sourceforge.jnlp.splashscreen.impls.defaultsplashscreen2012.ErrorPainter; |
| 47 |
|
| 48 |
public class ChristmasExtension implements SplashExtension { |
| 49 |
|
| 50 |
@Override |
| 51 |
public Color getBackground() { |
| 52 |
return Color.black; |
| 53 |
} |
| 54 |
|
| 55 |
@Override |
| 56 |
public Color getTextColor() { |
| 57 |
return Color.DARK_GRAY; |
| 58 |
} |
| 59 |
|
| 60 |
@Override |
| 61 |
public Color getPluginTextColor() { |
| 62 |
return new Color(30, 30, 30); |
| 63 |
} |
| 64 |
|
| 65 |
ChristmasExtension() { |
| 66 |
this(0, 0); |
| 67 |
} |
| 68 |
private static final Random seed = new Random(); |
| 69 |
private static final int avarege_star_width = 10; //stars will be 5-15 |
| 70 |
private final int avarege_fall_speed = 4; //2-6 |
| 71 |
private final int avarege_rotation_speed = 2; //1-3 |
| 72 |
|
| 73 |
private class Star { |
| 74 |
|
| 75 |
private int radiusX; |
| 76 |
private int radiusY; |
| 77 |
private int maxRadiusX; |
| 78 |
private int maxRadiusY; |
| 79 |
private int centerX; |
| 80 |
private int centerY; |
| 81 |
private final int fallSpeed; |
| 82 |
private final boolean orientation; |
| 83 |
private final int[] originalColor = new int[3]; |
| 84 |
private final int[] color = new int[originalColor.length]; |
| 85 |
private int direction; |
| 86 |
private final boolean haveEight; |
| 87 |
|
| 88 |
public Star() { |
| 89 |
createRadiuses(); |
| 90 |
haveEight = seed.nextBoolean(); |
| 91 |
this.centerX = seed.nextInt(w + 1); |
| 92 |
this.centerY = seed.nextInt(h + 1); |
| 93 |
this.fallSpeed = avarege_fall_speed / 2 + seed.nextInt(avarege_fall_speed / 2); |
| 94 |
this.orientation = seed.nextBoolean(); |
| 95 |
this.direction = -(avarege_rotation_speed / 2 + seed.nextInt(avarege_rotation_speed / 2)); |
| 96 |
if (seed.nextInt(4) == 0) { |
| 97 |
originalColor[0] = Color.yellow.getRed(); |
| 98 |
originalColor[1] = Color.yellow.getGreen(); |
| 99 |
originalColor[2] = Color.yellow.getBlue(); |
| 100 |
} else { |
| 101 |
originalColor[0] = BasePainter.WATER_LIVE_COLOR.getRed(); |
| 102 |
originalColor[1] = BasePainter.WATER_LIVE_COLOR.getGreen(); |
| 103 |
originalColor[2] = BasePainter.WATER_LIVE_COLOR.getBlue(); |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
public void paint(Graphics g, Color forceColor1, Color forceColor2) { |
| 108 |
Color c = g.getColor(); |
| 109 |
if (forceColor1 == null || forceColor2 == null) { |
| 110 |
g.setColor(new Color(color[0], color[1], color[2])); |
| 111 |
} else { |
| 112 |
g.setColor(ErrorPainter.interpolateColor(h, centerY, forceColor1, forceColor2)); |
| 113 |
} |
| 114 |
Polygon p = createPolygon(); |
| 115 |
if (haveEight) { |
| 116 |
int min1 = Math.min(radiusX, radiusY); |
| 117 |
int min2 = min1 / 2; |
| 118 |
g.fillRect(centerX - min2, centerY - min2, min1, min1); |
| 119 |
} |
| 120 |
g.fillPolygon(p); |
| 121 |
g.setColor(c); |
| 122 |
} |
| 123 |
|
| 124 |
private void animate() { |
| 125 |
centerY += fallSpeed; |
| 126 |
if (orientation) { |
| 127 |
radiusX += direction; |
| 128 |
if (radiusX <= -direction) { |
| 129 |
direction = -direction; |
| 130 |
radiusX = direction; |
| 131 |
} |
| 132 |
if (radiusX >= maxRadiusX) { |
| 133 |
direction = -direction; |
| 134 |
radiusX = maxRadiusX; |
| 135 |
} |
| 136 |
interpolateColors(radiusX, maxRadiusX); |
| 137 |
} else { |
| 138 |
radiusY += direction; |
| 139 |
if (radiusY <= -direction) { |
| 140 |
direction = -direction; |
| 141 |
radiusY = direction; |
| 142 |
} |
| 143 |
if (radiusY >= maxRadiusY) { |
| 144 |
direction = -direction; |
| 145 |
radiusY = maxRadiusY; |
| 146 |
} |
| 147 |
interpolateColors(radiusY, maxRadiusY); |
| 148 |
} |
| 149 |
if (centerY > h + radiusX * 2 || centerY > h + radiusY * 2) { |
| 150 |
createRadiuses(); |
| 151 |
this.centerX = seed.nextInt(w + 1); |
| 152 |
this.centerY = -radiusY * 2; |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
private int createRadius() { |
| 157 |
return avarege_star_width / 2 + seed.nextInt(avarege_star_width); |
| 158 |
} |
| 159 |
|
| 160 |
private Polygon createPolygon() { |
| 161 |
int min = Math.min(radiusX, radiusY) / 3; |
| 162 |
Polygon p = new Polygon(); |
| 163 |
p.addPoint(centerX - radiusX, centerY); |
| 164 |
p.addPoint(centerX - min, centerY - min); |
| 165 |
p.addPoint(centerX, centerY - radiusY); |
| 166 |
p.addPoint(centerX + min, centerY - min); |
| 167 |
p.addPoint(centerX + radiusX, centerY); |
| 168 |
p.addPoint(centerX + min, centerY + min); |
| 169 |
p.addPoint(centerX, centerY + radiusY); |
| 170 |
p.addPoint(centerX - min, centerY + min); |
| 171 |
return p; |
| 172 |
} |
| 173 |
|
| 174 |
private void interpolateColors(int is, int max) { |
| 175 |
for (int i = 0; i < originalColor.length; i++) { |
| 176 |
int fadeMin; |
| 177 |
if (centerY < 0) { |
| 178 |
fadeMin = 0; |
| 179 |
} else if (centerY > h) { |
| 180 |
fadeMin = 255; |
| 181 |
} else { |
| 182 |
fadeMin = (int) ErrorPainter.interpol(h, centerY, 255, 0); //from white to black |
| 183 |
} |
| 184 |
int fadeMax; |
| 185 |
if (centerY < 0) { |
| 186 |
fadeMax = 0; |
| 187 |
} else if (centerY > h) { |
| 188 |
fadeMax = originalColor[i]; |
| 189 |
} else { |
| 190 |
fadeMax = (int) ErrorPainter.interpol(h, centerY, originalColor[i], 0); //from color tho black |
| 191 |
} |
| 192 |
color[i] = (int) ErrorPainter.interpol(max, is, fadeMin, fadeMax); |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
private void createRadiuses() { |
| 197 |
this.radiusX = createRadius(); |
| 198 |
this.radiusY = radiusX; |
| 199 |
switch (seed.nextInt(3)) { |
| 200 |
case (0): |
| 201 |
radiusX = radiusX + (2 * radiusX) / 3; |
| 202 |
break; |
| 203 |
case (1): |
| 204 |
radiusY = radiusY + (2 * radiusY) / 3; |
| 205 |
break; |
| 206 |
case (2): |
| 207 |
//noop |
| 208 |
break; |
| 209 |
} |
| 210 |
maxRadiusX = radiusX; |
| 211 |
maxRadiusY = radiusY; |
| 212 |
} |
| 213 |
} |
| 214 |
private int w; |
| 215 |
private int h; |
| 216 |
private List<Star> stars = new ArrayList<Star>(50); |
| 217 |
|
| 218 |
ChristmasExtension(int w, int h) { |
| 219 |
adjustForSize(w, h); |
| 220 |
} |
| 221 |
|
| 222 |
@Override |
| 223 |
public void paint(Graphics g, BasePainter b) { |
| 224 |
for (ChristmasExtension.Star star : stars) { |
| 225 |
Color forceColor1 = null; |
| 226 |
Color forceColor2 = null; |
| 227 |
if (b instanceof ErrorPainter){ |
| 228 |
forceColor1 = b.getBackgroundColor(); |
| 229 |
forceColor2 = b.getWaterColor(); |
| 230 |
} |
| 231 |
star.paint(g, forceColor1, forceColor2); |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
@Override |
| 236 |
public void animate() { |
| 237 |
for (ChristmasExtension.Star star : stars) { |
| 238 |
star.animate(); |
| 239 |
|
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
@Override |
| 244 |
public final void adjustForSize(int w, int h) { |
| 245 |
this.w = w; |
| 246 |
this.h = h; |
| 247 |
int count = w / (2 * (avarege_star_width + 1)); |
| 248 |
while (stars.size() > count) { |
| 249 |
stars.remove(stars.size() - 1); |
| 250 |
} |
| 251 |
while (stars.size() < count) { |
| 252 |
stars.add(new Star()); |
| 253 |
|
| 254 |
} |
| 255 |
|
| 256 |
} |
| 257 |
} |