FileDocCategorySizeDatePackage
Arcs.javaAPI DocExample4881Wed Aug 08 15:13:48 BST 2001demos.Arcs_Curves

Arcs

public class Arcs extends AnimatingSurface
Arc2D Open, Chord & Pie arcs; Animated Pie Arc.

Fields Summary
private static String[]
types
private static final int
CLOSE
private static final int
OPEN
private static final int
FORWARD
private static final int
BACKWARD
private static final int
DOWN
private static final int
UP
private int
aw
private int
ah
private int
x
private int
y
private int
angleStart
private int
angleExtent
private int
mouth
private int
direction
Constructors Summary
public Arcs()



      
        setBackground(Color.white);
    
Methods Summary
public static voidmain(java.lang.String[] argv)

        createDemoFrame(new Arcs());
    
public voidrender(int w, int h, java.awt.Graphics2D g2)


        g2.setStroke(new BasicStroke(5.0f));
      // Draw Arcs
        for (int i = 0; i < types.length; i++) {
            Arc2D arc = new Arc2D.Float(i);
            arc.setFrame((i+1)*w*.2, (i+1)*h*.2, w*.17, h*.17);
            arc.setAngleStart(45);
            arc.setAngleExtent(270);
            g2.setColor(Color.blue);
            g2.draw(arc);
            g2.setColor(Color.gray);
            g2.fill(arc);
            g2.setColor(Color.black);
            g2.drawString(types[i], (int)((i+1)*w*.2), (int)((i+1)*h*.2-3));
        }

      // Draw Animated Pie Arc
        Arc2D pieArc = new Arc2D.Float(Arc2D.PIE);
        pieArc.setFrame(0, 0, aw, ah);
        pieArc.setAngleStart(angleStart);
        pieArc.setAngleExtent(angleExtent);
        AffineTransform at = AffineTransform.getTranslateInstance(x, y);
        switch (direction) {
            case DOWN : at.rotate(Math.toRadians(90)); break;
            case BACKWARD : at.rotate(Math.toRadians(180)); break;
            case UP : at.rotate(Math.toRadians(270));
        }
        g2.setColor(Color.blue);
        g2.fill(at.createTransformedShape(pieArc));
    
public voidreset(int w, int h)

        x = 0; y = 0;
        aw = w/12; ah = h/12;
    
public voidstep(int w, int h)

      // Compute direction
        if (x+aw >= w-5 && direction == FORWARD)
            direction = DOWN;
        if (y+ah >= h-5 && direction == DOWN)
            direction = BACKWARD;
        if (x-aw <= 5 && direction == BACKWARD)
            direction = UP;
        if (y-ah <= 5 && direction == UP)
            direction = FORWARD;

      // compute angle start & extent
        if (mouth == CLOSE) {
            angleStart -= 5;
            angleExtent += 10;
        }
        if (mouth == OPEN) {
            angleStart += 5;
            angleExtent -= 10;
        }
        if (direction == FORWARD) {
            x += 5; y = 0;
        }
        if (direction == DOWN) {
            x = w; y += 5;
        }
        if (direction == BACKWARD) {
            x -= 5; y = h;
        }
        if (direction == UP) {
            x = 0; y -= 5;
        }
        if (angleStart == 0)
            mouth = OPEN;
        if (angleStart > 45)
            mouth = CLOSE;