Moving Shapes along an arc path with java graphics

核能气质少年 提交于 2020-01-03 23:06:55

问题


Pardon my naivety, its my first time here and first time dealing with animation of graphics in java. I'm trying to accomplish an animation of star shapes that moves along a sort of arc(trying to simulate an orbit on a 2d wise). The orbit Action is used with a Timer to animate the stars.

Simply put, I have drawn several stars at various positions in a jpanel. The translation of the stars y position depends on how far that star is away from the the x axis of decline which is initialized to 300(the center of the jpanel). The closer a star is to the point of decline, the less their y position is going to change. When a star reaches or passes the right side of the panel(or goes out of view), reset to the left side at its original y position(ugly, i know). I chose to do it this way since the stars are placed at random. I cant have all the stars start with the same dy, if it were so, all the stars would move along their own arc instead.

However, when I run this, after the third pass, the x positions of all the stars become smaller(into the negative ranges and out of view). Any suggestions for a better way to accomplish the original task are welcome. Thanks.

private Action orbit = new AbstractAction() {

    int declineAxis = 300; //if a stars top left x is greater than this, move downwards
    double distFromDecline;
    AffineTransform at = new AffineTransform();

    @Override
    public void actionPerformed(ActionEvent ae) {
        for (int i = 0; i < 12; i++) {
            distFromDecline = Math.abs(declineAxis - stars.getStar(i).getBounds().getCenterX());
            if (distFromDecline <= 50) {
                if (stars.getStar(i).getBounds().getX() < declineAxis) {
                    at.translate(5, -2);
                } else {
                    at.translate(5, 2);
                }
            } else if (distFromDecline <= 100 && distFromDecline > 50) {
                if (stars.getStar(i).getBounds().getX() < declineAxis) {
                    at.translate(5, -3);
                } else {
                    at.translate(5, 3);
                }
            } else if (distFromDecline <= 200 && distFromDecline > 100) {
                if (stars.getStar(i).getBounds().getX() < declineAxis) {
                    at.translate(5, -4);
                } else {
                    at.translate(5, 4);
                }
            } else if (distFromDecline >200) {
                if (stars.getStar(i).getBounds().getX() < declineAxis) {
                    at.translate(5, -5);
                } else {
                    at.translate(5, 5);
                }
            }
            stars.move(at, i);
        }
    }
};
public class Stars {

private int[] yOrigins;
private Path2D[] stars;
private Random rand = new Random();

public Stars(int n) {
    stars = new Path2D[n];
    yOrigins = new int[n];
    int dx = 700 / n;
    int x = 0;
    for (int i = 0; i < n; i++) {
        int y = rand.nextInt(401);
        generateStar(i, x, y);
        yOrigins[i] = y;
        x += dx;
    }
}

private void generateStar(int i, int x, int y) {
    stars[i] = new Path2D.Double();
    Path2D.Double cur = (Path2D.Double) stars[i];
    cur.moveTo(x, y);
    cur.lineTo(cur.getCurrentPoint().getX() + 6, y - 2);
    cur.lineTo(cur.getCurrentPoint().getX() + 2, cur.getCurrentPoint().getY() - 6);
    cur.lineTo(cur.getCurrentPoint().getX() + 2, cur.getCurrentPoint().getY() + 6);
    cur.lineTo(cur.getCurrentPoint().getX() + 6, cur.getCurrentPoint().getY() + 2);
    cur.lineTo(cur.getCurrentPoint().getX() - 6, cur.getCurrentPoint().getY() + 2);
    cur.lineTo(cur.getCurrentPoint().getX() - 2, cur.getCurrentPoint().getY() + 6);
    cur.lineTo(cur.getCurrentPoint().getX() - 2, cur.getCurrentPoint().getY() - 6);
    cur.closePath();
}

public void paintStars(Graphics2D g) {
    //super.paintComponent(g);
    g.setColor(new Color(246, 246, 255));
    for (int i = 0; i < stars.length; i++) {
        g.fill(stars[i]);
    }
}

public Shape getStar(int i) {
    return stars[i];
}

void move(AffineTransform at, int i) {
    stars[i] = (Path2D) stars[i].createTransformedShape(at);
    System.out.println(i+": " + stars[i].getBounds());
    if(stars[i].getBounds().getX()>700){
        at.translate(-(stars[i].getBounds().x+stars[i].getBounds().getWidth()), yOrigins[i]);
        stars[i] = (Path2D) at.createTransformedShape(stars[i]);
    }
}

}


回答1:


java.awt.geom.FlatteningPathIterator http://docs.oracle.com/javase/6/docs/api/java/awt/geom/FlatteningPathIterator.html

You pass your arc (or any another Shape) and use the points to position star.

You can use stars frm here http://java-sl.com/shapes.html




回答2:


WarpImage from the Sun/Oracle Java2D demo, java2d/demos/Images/WarpImage.java, is an appealing example of an animation that follows a CubicCurve2D using PathIterator. You might see if it offers any guidance.



来源:https://stackoverflow.com/questions/9238061/moving-shapes-along-an-arc-path-with-java-graphics

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!