简体   繁体   English

使用java图形沿弧形路径移动形状

[英]Moving Shapes along an arc path with java graphics

Pardon my naivety, its my first time here and first time dealing with animation of graphics in java. 请原谅我的天真,这是我第一次来这里,第一次处理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). 我正在尝试完成一个沿着某种弧线移动的星形动画(试图在2d智能上模拟轨道)。 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. 简单地说,我已经在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). 恒星y位置的平移取决于恒星远离x轴下降的距离,该x轴初始化为300(jpanel的中心)。 The closer a star is to the point of decline, the less their y position is going to change. 恒星越接近衰落点,它们的y位置就会越小。 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). 当一颗星到达或通过面板的右侧(或离开视线)时,重置到其原始y位置的左侧(丑陋,我知道)。 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. 我不能让所有的星星都以同样的dy开始,如果是这样的话,所有的星星都会沿着自己的弧线移动。

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). 然而,当我运行它时,在第三次传球之后,所有星星的x位置变小(进入负范围并且在视野之外)。 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]);
    }
}

} }

java.awt.geom.FlatteningPathIterator http://docs.oracle.com/javase/6/docs/api/java/awt/geom/FlatteningPathIterator.html 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 您可以在http://java-sl.com/shapes.html使用星星

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 . WarpImage从Sun /甲骨文的Java2D演示java2d/demos/Images/WarpImage.java ,是遵循一个动画的一个吸引人的例子CubicCurve2D使用PathIterator You might see if it offers any guidance. 您可能会看到它是否提供任何指导。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM