移动形状以及与Java的图形圆弧路径 [英] Moving Shapes along an arc path with java graphics
问题描述
原谅我的幼稚,它的我第一次来这里,并首次处理在Java的图形动画。我试图完成沿一种弧星移动形状的动画(试图在2D明智的模拟轨道)。轨道操作使用带有定时器的动画明星。
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.
简单地说,我曾经在一个JPanel不同的位置绘制几个明星。星星y位置的转换取决于恒星有多远是从被初始化为300(在JPanel的中心)下降的x轴的路程。越接近明星是下降了点,少的y位置将会改变。当星形达到或通过在面板的右侧(或熄灭的视图),复位到左侧的原始y坐标(难看,我知道)。我选择了做这种方式,因为明星们随意摆放。我不能拥有所有的恒星一样DY启动,如果是这样,所有的星星会沿着自己的弧线,而不是移动
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.
然而,当我运行此,第三关后,所有的星星的x位置变小(进入负范围和出的视图)。一个更好的方式来完成原任务的任何建议都欢迎。谢谢你。
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]);
}
}
}
推荐答案
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.
您可以使用星这里FRM的
http://java-sl.com/shapes.html
You can use stars frm here http://java-sl.com/shapes.html
这篇关于移动形状以及与Java的图形圆弧路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!