在圆形路径中移动圆圈

时间:2015-11-16 11:47:06

标签: java swing java-2d

我在JPanel中绘制一个圆圈并使用Swing Timer来更新圆圈的x,y坐标。

如何在圆形路径中移动圆圈。

protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g.create();
    g2d.setColor(Color.RED);
    Shape planet = new Ellipse2D.Double(x, y, 20, 20);
    g2d.fill(planet);
    g2d.dispose();
}



public void actionPerformed(ActionEvent evt) {
    double R = 200;
    for (double t = 0; t < 2 * Math.PI; t += 0.01) {
        x = R * Math.cos(t) + 0;
        y = R * Math.sin(t) + 0;
        revalidate();
        repaint();
    }
}

2 个答案:

答案 0 :(得分:1)

你真的已经在那里了。

gulp.task('build', function () {
    return browserify({entries: 'main.js', extensions: ['.js'], debug: true})
    .transform(babelify.configure({
        presets: ["react"]
}))
    .bundle()
    .on("error", function (err) { console.log("Error : " + err.message); })
    .pipe(source('bundle.js'))
    .pipe(gulp.dest('dist'));
});

你所拥有的0表示圆的中心(x,y)。因此,在中心0,0和半径R2的另一个圆轨道的路径中旋转圆圈。轨道θ值(x = R * Math.cos(t) + 0; y = R * Math.sin(t) + 0; )每帧增加一次。

oTheta

答案 1 :(得分:0)

不需要循环。定时器将在指定的间隔后调用它并更新oTheta。

public void actionPerformed(ActionEvent evt) {
    oTheta += 0.01;
    x = radius * Math.cos(oTheta) + centerX;
    y = radius * Math.sin(oTheta) + centerY;
    revalidate();
    repaint();
}
相关问题