Changeset 14581 in josm
- Timestamp:
- 2018-12-22T02:33:25+01:00 (6 years ago)
- Location:
- trunk/src/org/openstreetmap/josm/gui/animation
- Files:
-
- 2 added
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/gui/animation/ChristmasExtension.java
r14578 r14581 2 2 package org.openstreetmap.josm.gui.animation; 3 3 4 import java.awt.Color;5 4 import java.awt.Graphics; 6 import java.awt.Polygon;7 5 import java.util.ArrayList; 8 6 import java.util.List; 9 import java.util.Random;10 7 11 8 /** … … 17 14 public class ChristmasExtension implements AnimationExtension { 18 15 19 ChristmasExtension() {20 this(0, 0);21 }22 23 private static final Random seed = new Random();24 private static final int averageStarWidth = 10; // stars will be 5-1525 private static final int averageFallSpeed = 4; // 2-626 private static final int averageRotationSpeed = 2; // 1-327 28 private static final Color WATER_LIVE_COLOR = new Color(80, 131, 160);29 30 /**31 * Interpolation is root ratio is r= (currentSize / origSize)32 * then value to-from is interpolated from to to from according to ratio33 *34 * @param origSize original size35 * @param currentSize current size36 * @param from starting value37 * @param to ending value38 * @return interpolated value39 */40 public static double interpol(double origSize, double currentSize, double from, double to) {41 return (currentSize / origSize) * (to - from) + from;42 }43 44 private class Star {45 46 private int radiusX;47 private int radiusY;48 private int maxRadiusX;49 private int maxRadiusY;50 private int centerX;51 private int centerY;52 private final int fallSpeed;53 private final boolean orientation;54 private final int[] originalColor = new int[3];55 private final int[] color = new int[originalColor.length];56 private int direction;57 private final boolean haveEight;58 59 Star() {60 createRadiuses();61 haveEight = seed.nextBoolean();62 this.centerX = seed.nextInt(w + 1);63 this.centerY = seed.nextInt(h + 1);64 this.fallSpeed = averageFallSpeed / 2 + seed.nextInt(averageFallSpeed / 2);65 this.orientation = seed.nextBoolean();66 this.direction = -(averageRotationSpeed / 2 + seed.nextInt(averageRotationSpeed / 2));67 if (seed.nextInt(4) == 0) {68 originalColor[0] = Color.yellow.getRed();69 originalColor[1] = Color.yellow.getGreen();70 originalColor[2] = Color.yellow.getBlue();71 } else {72 originalColor[0] = WATER_LIVE_COLOR.getRed();73 originalColor[1] = WATER_LIVE_COLOR.getGreen();74 originalColor[2] = WATER_LIVE_COLOR.getBlue();75 }76 }77 78 public void paint(Graphics g) {79 Color c = g.getColor();80 g.setColor(new Color(color[0], color[1], color[2]));81 Polygon p = createPolygon();82 if (haveEight) {83 int min1 = Math.min(radiusX, radiusY);84 int min2 = min1 / 2;85 g.fillRect(centerX - min2, centerY - min2, min1, min1);86 }87 g.fillPolygon(p);88 g.setColor(c);89 }90 91 private void animate() {92 centerY += fallSpeed;93 if (orientation) {94 radiusX += direction;95 if (radiusX <= -direction) {96 direction = -direction;97 radiusX = direction;98 }99 if (radiusX >= maxRadiusX) {100 direction = -direction;101 radiusX = maxRadiusX;102 }103 interpolateColors(radiusX, maxRadiusX);104 } else {105 radiusY += direction;106 if (radiusY <= -direction) {107 direction = -direction;108 radiusY = direction;109 }110 if (radiusY >= maxRadiusY) {111 direction = -direction;112 radiusY = maxRadiusY;113 }114 interpolateColors(radiusY, maxRadiusY);115 }116 if (centerY > h + radiusX * 2 || centerY > h + radiusY * 2) {117 createRadiuses();118 this.centerX = seed.nextInt(w + 1);119 this.centerY = -radiusY * 2;120 }121 }122 123 private int createRadius() {124 return averageStarWidth / 2 + seed.nextInt(averageStarWidth);125 }126 127 private Polygon createPolygon() {128 int min = Math.min(radiusX, radiusY) / 3;129 Polygon p = new Polygon();130 p.addPoint(centerX - radiusX, centerY);131 p.addPoint(centerX - min, centerY - min);132 p.addPoint(centerX, centerY - radiusY);133 p.addPoint(centerX + min, centerY - min);134 p.addPoint(centerX + radiusX, centerY);135 p.addPoint(centerX + min, centerY + min);136 p.addPoint(centerX, centerY + radiusY);137 p.addPoint(centerX - min, centerY + min);138 return p;139 }140 141 private void interpolateColors(int is, int max) {142 for (int i = 0; i < originalColor.length; i++) {143 int fadeMin;144 if (centerY < 0) {145 fadeMin = 0;146 } else if (centerY > h) {147 fadeMin = 255;148 } else {149 fadeMin = (int) interpol(h, centerY, 255, 0); // from white to black150 }151 int fadeMax;152 if (centerY < 0) {153 fadeMax = 0;154 } else if (centerY > h) {155 fadeMax = originalColor[i];156 } else {157 fadeMax = (int) interpol(h, centerY, originalColor[i], 0); // from color to black158 }159 color[i] = (int) interpol(max, is, fadeMin, fadeMax);160 }161 }162 163 private void createRadiuses() {164 this.radiusX = createRadius();165 this.radiusY = radiusX;166 switch (seed.nextInt(3)) {167 case 0:168 radiusX = radiusX + (2 * radiusX) / 3;169 break;170 case 1:171 radiusY = radiusY + (2 * radiusY) / 3;172 break;173 case 2:174 //noop175 break;176 }177 maxRadiusX = radiusX;178 maxRadiusY = radiusY;179 }180 }181 182 private int w;183 private int h;184 16 private final List<Star> stars = new ArrayList<>(50); 185 186 ChristmasExtension(int w, int h) {187 adjustForSize(w, h);188 }189 17 190 18 @Override … … 200 28 @Override 201 29 public final void adjustForSize(int w, int h) { 202 this.w = w; 203 this.h = h; 204 int count = w / (2 * (averageStarWidth + 1)); 30 int count = w / (2 * (Star.averageStarWidth + 1)); 205 31 while (stars.size() > count) { 206 32 stars.remove(stars.size() - 1); 207 33 } 208 34 while (stars.size() < count) { 209 stars.add(new Star( ));35 stars.add(new Star(w, h)); 210 36 } 211 37 } -
trunk/src/org/openstreetmap/josm/gui/animation/NoExtension.java
r14578 r14581 17 17 @Override 18 18 public void adjustForSize(int w, int h) { 19 // No-op 19 20 } 20 21 21 22 @Override 22 23 public void animate() { 24 // No-op 23 25 } 24 26 25 27 @Override 26 28 public void paint(Graphics g) { 29 // No-op 27 30 } 28 31 }
Note:
See TracChangeset
for help on using the changeset viewer.