1 30 31 package com.jgoodies.animation.renderer; 32 33 import java.awt.Color ; 34 import java.awt.Graphics2D ; 35 import java.awt.Shape ; 36 import java.awt.geom.GeneralPath ; 37 import java.awt.geom.Point2D ; 38 import java.util.Random ; 39 40 import com.jgoodies.animation.AnimationRenderer; 41 42 43 52 public final class FanRenderer implements AnimationRenderer { 53 54 private static final Random random = new Random (System.currentTimeMillis()); 55 56 57 private final Triangle[] triangles; 58 59 private Point2D origin; 60 private double rotation; 61 62 63 public FanRenderer(Triangle[] triangles) { 64 this.triangles = triangles; 65 } 66 67 68 public FanRenderer(int triangleCount, Color baseColor) { 69 this(createSectors(triangleCount, baseColor)); 70 } 71 72 73 public static Triangle[] createSectors(int count, Color baseColor) { 74 Triangle[] result = new Triangle[count]; 75 double sectorAngle = Math.PI * 2 / count; 76 77 for (int i = 0; i < count; i++) { 78 double rotation = i * sectorAngle + (random.nextFloat() - 0.5) * Math.PI/10; 79 double angle = sectorAngle * (0.2 + random.nextFloat() * 0.4); 80 result[i] = new Triangle(rotation, angle, nextColor(baseColor)); 81 } 82 return result; 83 } 84 85 private static Color nextColor(Color baseColor) { 86 float[] hsb = new float[3]; 87 Color.RGBtoHSB(baseColor.getRed(), baseColor.getGreen(), baseColor.getBlue(), hsb); 88 float brightness = 0.8f + random.nextFloat() * 0.2f; 89 return Color.getHSBColor(hsb[0], hsb[1], brightness); 90 } 91 92 93 public Point2D getOrigin() { return origin; } 94 public void setOrigin(Point2D origin) { this.origin = origin; } 95 public double getRotation() { return rotation; } 96 public void setRotation(double rotation) { this.rotation = rotation; } 97 98 99 public void render(Graphics2D g2, int width, int height) { 100 double radius = Math.sqrt(width * width + height * height); 101 102 Point2D p = getOrigin() != null ? getOrigin() : getDefaultOrigin(width, height); 103 104 g2.translate(p.getX(), p.getY()); 105 g2.rotate(rotation); 106 for (int i = 0; i < triangles.length; i++) { 107 triangles[i].render(g2, radius); 108 } 109 g2.rotate(-rotation); 110 g2.translate(-p.getX(), -p.getY()); 111 } 112 113 114 private Point2D getDefaultOrigin(int width, int height) { 115 return new Point2D.Double (width * 0.75, height * 0.75); 116 } 117 118 119 121 122 private static class Triangle { 124 125 private final double aRotation; 126 private final double angle; 127 private final Color color; 128 129 private Triangle(double rotation, double angle, Color color) { 130 this.aRotation = rotation; 131 this.angle = angle; 132 this.color = color; 133 } 134 135 private static Shape createPolygon(double rotation, double angle, double radius) { 136 double startAngle = rotation - angle / 2; 137 double stopAngle = startAngle + angle; 138 double hypothenusis = radius / Math.cos(angle / 2); 139 140 float x0 = 0.0f; 141 float y0 = 0.0f; 142 float x1 = (float) (x0 - hypothenusis * Math.cos(startAngle)); 143 float y1 = (float) (y0 - hypothenusis * Math.sin(startAngle)); 144 float x2 = (float) (x0 - hypothenusis * Math.cos(stopAngle)); 145 float y2 = (float) (y0 - hypothenusis * Math.sin(stopAngle)); 146 147 GeneralPath polygon = new GeneralPath (GeneralPath.WIND_EVEN_ODD, 3); 148 polygon.moveTo(x0, y0); 149 polygon.lineTo(x1, y1); 150 polygon.lineTo(x2, y2); 151 polygon.closePath(); 152 153 return polygon; 154 } 155 156 void render(Graphics2D g2, double radius) { 157 g2.setColor(color); 158 g2.fill(createPolygon(aRotation, angle, radius)); 159 } 160 } 161 162 } | Popular Tags |