1 36 37 40 41 package java2d.demos.Arcs_Curves; 42 43 import java.awt.*; 44 import java.awt.geom.Ellipse2D ; 45 import java.awt.geom.Arc2D ; 46 import java.awt.geom.AffineTransform ; 47 import java2d.AnimatingSurface; 48 49 import static java.awt.Color .*; 50 51 52 55 public class Arcs extends AnimatingSurface { 56 57 private static String types[] = {"Arc2D.OPEN","Arc2D.CHORD","Arc2D.PIE"}; 58 59 private static final int CLOSE = 0; 60 private static final int OPEN = 1; 61 62 private static final int FORWARD = 0; 63 private static final int BACKWARD = 1; 64 private static final int DOWN = 2; 65 private static final int UP = 3; 66 67 private int aw, ah; private int x, y; 69 private int angleStart = 45; 70 private int angleExtent = 270; 71 private int mouth = CLOSE; 72 private int direction = FORWARD; 73 74 75 public Arcs() { 76 setBackground(WHITE); 77 } 78 79 80 public void reset(int w, int h) { 81 x = 0; y = 0; 82 aw = w/12; ah = h/12; 83 } 84 85 86 public void step(int w, int h) { 87 if (x+aw >= w-5 && direction == FORWARD ) direction = DOWN; 89 if (y+ah >= h-5 && direction == DOWN ) direction = BACKWARD; 90 if (x-aw <= 5 && direction == BACKWARD) direction = UP; 91 if (y-ah <= 5 && direction == UP ) direction = FORWARD; 92 93 if (mouth == CLOSE) { 95 angleStart -= 5; 96 angleExtent += 10; 97 } 98 if (mouth == OPEN) { 99 angleStart += 5; 100 angleExtent -= 10; 101 } 102 if (direction == FORWARD) { 103 x += 5; y = 0; 104 } 105 if (direction == DOWN) { 106 x = w; y += 5; 107 } 108 if (direction == BACKWARD) { 109 x -= 5; y = h; 110 } 111 if (direction == UP) { 112 x = 0; y -= 5; 113 } 114 if (angleStart == 0) 115 mouth = OPEN; 116 if (angleStart > 45) 117 mouth = CLOSE; 118 } 119 120 121 public void render(int w, int h, Graphics2D g2) { 122 123 g2.setStroke(new BasicStroke(5.0f)); 125 for (int i = 0; i < types.length; i++) { 126 Arc2D arc = new Arc2D.Float (i); 127 arc.setFrame((i+1)*w*.2, (i+1)*h*.2, w*.17, h*.17); 128 arc.setAngleStart(45); 129 arc.setAngleExtent(270); 130 g2.setColor(BLUE); 131 g2.draw(arc); 132 g2.setColor(GRAY); 133 g2.fill(arc); 134 g2.setColor(BLACK); 135 g2.drawString(types[i], (int)((i+1)*w*.2), (int)((i+1)*h*.2-3)); 136 } 137 138 Arc2D pieArc = new Arc2D.Float (Arc2D.PIE); 140 pieArc.setFrame(0, 0, aw, ah); 141 pieArc.setAngleStart(angleStart); 142 pieArc.setAngleExtent(angleExtent); 143 AffineTransform at = AffineTransform.getTranslateInstance(x, y); 144 switch (direction) { 145 case DOWN : at.rotate(Math.toRadians( 90)); break; 146 case BACKWARD : at.rotate(Math.toRadians(180)); break; 147 case UP : at.rotate(Math.toRadians(270)); 148 } 149 g2.setColor(BLUE); 150 g2.fill(at.createTransformedShape(pieArc)); 151 } 152 153 154 public static void main(String argv[]) { 155 createDemoFrame(new Arcs()); 156 } 157 } 158 | Popular Tags |