Changeset 14581 in josm for trunk


Ignore:
Timestamp:
2018-12-22T02:33:25+01:00 (5 years ago)
Author:
Don-vip
Message:

fix recent sonar issues

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  
    22package org.openstreetmap.josm.gui.animation;
    33
    4 import java.awt.Color;
    54import java.awt.Graphics;
    6 import java.awt.Polygon;
    75import java.util.ArrayList;
    86import java.util.List;
    9 import java.util.Random;
    107
    118/**
     
    1714public class ChristmasExtension implements AnimationExtension {
    1815
    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-15
    25     private static final int averageFallSpeed = 4;     // 2-6
    26     private static final int averageRotationSpeed = 2; // 1-3
    27 
    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 ratio
    33      *
    34      * @param origSize original size
    35      * @param currentSize current size
    36      * @param from starting value
    37      * @param to ending value
    38      * @return interpolated value
    39      */
    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 black
    150                 }
    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 black
    158                 }
    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                     //noop
    175                     break;
    176             }
    177             maxRadiusX = radiusX;
    178             maxRadiusY = radiusY;
    179         }
    180     }
    181 
    182     private int w;
    183     private int h;
    18416    private final List<Star> stars = new ArrayList<>(50);
    185 
    186     ChristmasExtension(int w, int h) {
    187         adjustForSize(w, h);
    188     }
    18917
    19018    @Override
     
    20028    @Override
    20129    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));
    20531        while (stars.size() > count) {
    20632            stars.remove(stars.size() - 1);
    20733        }
    20834        while (stars.size() < count) {
    209             stars.add(new Star());
     35            stars.add(new Star(w, h));
    21036        }
    21137    }
  • trunk/src/org/openstreetmap/josm/gui/animation/NoExtension.java

    r14578 r14581  
    1717    @Override
    1818    public void adjustForSize(int w, int h) {
     19        // No-op
    1920    }
    2021
    2122    @Override
    2223    public void animate() {
     24        // No-op
    2325    }
    2426
    2527    @Override
    2628    public void paint(Graphics g) {
     29        // No-op
    2730    }
    2831}
Note: See TracChangeset for help on using the changeset viewer.