1 36 37 40 41 package java2d.demos.Lines; 42 43 import static java.awt.Color .*; 44 import java.awt.*; 45 import java.awt.geom.*; 46 import java.awt.font.TextLayout ; 47 import java2d.AnimatingSurface; 48 49 50 53 public class LineAnim extends AnimatingSurface { 54 55 private static int caps[] = { BasicStroke.CAP_BUTT, 56 BasicStroke.CAP_SQUARE, BasicStroke.CAP_ROUND}; 57 private static int joins[] = { BasicStroke.JOIN_MITER, 58 BasicStroke.JOIN_BEVEL, BasicStroke.JOIN_ROUND}; 59 private static Color colors[] = {GRAY, PINK, LIGHT_GRAY}; 60 private static BasicStroke bs1 = new BasicStroke(1.0f); 61 private static final int CLOCKWISE = 0; 62 private static final int COUNTERCW = 1; 63 64 private Line2D lines[] = new Line2D[3]; 65 private int rAmt[] = new int[lines.length]; 66 private int direction[] = new int[lines.length]; 67 private int speed[] = new int[lines.length]; 68 private BasicStroke strokes[] = new BasicStroke[lines.length]; 69 private GeneralPath path; 70 private Point2D[] pts; 71 private float size; 72 private Ellipse2D ellipse = new Ellipse2D.Double(); 73 74 75 public LineAnim() { 76 setBackground(WHITE); 77 } 78 79 public void reset(int w, int h) { 80 size = (w > h) ? h/6f : w/6f; 81 for (int i = 0; i < lines.length; i++) { 82 lines[i] = new Line2D.Float(0,0,size,0); 83 strokes[i] = new BasicStroke(size/3, caps[i], joins[i]); 84 rAmt[i] = i * 360/lines.length; 85 direction[i] = i%2; 86 speed[i] = i + 1; 87 } 88 89 path = new GeneralPath(); 90 path.moveTo(size, -size/2); 91 path.lineTo(size+size/2, 0); 92 path.lineTo(size, +size/2); 93 94 ellipse.setFrame(w/2-size*2-4.5f,h/2-size*2-4.5f,size*4,size*4); 95 PathIterator pi = ellipse.getPathIterator(null, 0.9); 96 Point2D[] points = new Point2D[100]; 97 int num_pts = 0; 98 while ( !pi.isDone() ) { 99 float[] pt = new float[6]; 100 switch ( pi.currentSegment(pt) ) { 101 case FlatteningPathIterator.SEG_MOVETO: 102 case FlatteningPathIterator.SEG_LINETO: 103 points[num_pts] = new Point2D.Float(pt[0], pt[1]); 104 num_pts++; 105 } 106 pi.next(); 107 } 108 pts = new Point2D[num_pts]; 109 System.arraycopy(points, 0, pts, 0, num_pts); 110 } 111 112 113 public void step(int w, int h) { 114 for (int i = 0; i < lines.length; i++) { 115 if (direction[i] == CLOCKWISE) { 116 rAmt[i] += speed[i]; 117 if (rAmt[i] == 360) { 118 rAmt[i] = 0; 119 } 120 } else { 121 rAmt[i] -= speed[i]; 122 if (rAmt[i] == 0) { 123 rAmt[i] = 360; 124 } 125 } 126 } 127 } 128 129 130 public void render(int w, int h, Graphics2D g2) { 131 132 ellipse.setFrame(w/2-size,h/2-size,size*2,size*2); 133 g2.setColor(BLACK); 134 g2.draw(ellipse); 135 136 for (int i = 0; i < lines.length; i++) { 137 AffineTransform at = AffineTransform.getTranslateInstance(w/2,h/2); 138 at.rotate(Math.toRadians(rAmt[i])); 139 g2.setStroke(strokes[i]); 140 g2.setColor(colors[i]); 141 g2.draw(at.createTransformedShape(lines[i])); 142 g2.draw(at.createTransformedShape(path)); 143 144 int j = (int) ((double) rAmt[i]/360 * pts.length); 145 j = (j == pts.length) ? pts.length-1 : j; 146 ellipse.setFrame(pts[j].getX(), pts[j].getY(), 9, 9); 147 g2.fill(ellipse); 148 } 149 150 g2.setStroke(bs1); 151 g2.setColor(BLACK); 152 for (int i = 0; i < pts.length; i++) { 153 ellipse.setFrame(pts[i].getX(), pts[i].getY(), 9, 9); 154 g2.draw(ellipse); 155 } 156 } 157 158 159 public static void main(String argv[]) { 160 createDemoFrame(new LineAnim()); 161 } 162 } 163 | Popular Tags |