请原谅我的天真,这是我第一次来这里,第一次处理java中的图形动画.我正在尝试完成一个沿着某种弧线移动的星形动画(试图在2d智能上模拟轨道).轨道动作与定时器一起用于为星星设置动画.
简单地说,我已经在jpanel的不同位置画了几颗星.恒星y位置的平移取决于恒星远离x轴下降的距离,该x轴初始化为300(jpanel的中心).恒星越接近衰落点,它们的y位置就会越小.当一颗星到达或通过面板的右侧(或离开视线)时,重置到其原始y位置的左侧(丑陋,我知道).我选择这样做,因为星星是随机放置的.我不能让所有的星星都以同样的dy开始,如果是这样的话,所有的星星都会沿着自己的弧线移动.
然而,当我运行它时,在第三次传球之后,所有星星的x位置变小(进入负范围并且在视野之外).欢迎任何关于更好地完成原始任务的建议.谢谢.
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
您传递弧线(或任何其他形状)并使用这些点来定位星形.
你可以在这里使用星星
http://java-sl.com/shapes.html