Java 牛顿';s光盘旋转程序太慢。有没有办法加速定时器?此外,动画也会闪烁。有办法解决吗?

Java 牛顿';s光盘旋转程序太慢。有没有办法加速定时器?此外,动画也会闪烁。有办法解决吗?,java,performance,animation,timer,Java,Performance,Animation,Timer,我想用Java编写一个程序,以高速旋转牛顿圆盘,使其看起来是白色的。但是timer类的最小延迟仅为1毫秒。我能以某种方式加快速度吗(也许我能以某种方式将0.084之类的值传递为延迟?)?图像也会闪烁。有办法摆脱它吗?代码如下: import java.awt.*; import javax.swing.*; import java.awt.event.*; public class NDisc extends JPanel implements ActionListener { Timer a;

我想用Java编写一个程序,以高速旋转牛顿圆盘,使其看起来是白色的。但是timer类的最小延迟仅为1毫秒。我能以某种方式加快速度吗(也许我能以某种方式将0.084之类的值传递为延迟?)?图像也会闪烁。有办法摆脱它吗?代码如下:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class NDisc extends JPanel implements ActionListener
{
Timer a;
double angle=0;//To store value of angle of rotation in radians
ImageIcon c;
Image dbImage;
Graphics dbg;
public NDisc()
{
a=new Timer(1,this);//Change delay here to increase or decrease the speed of rotation
c=new ImageIcon("ND4.png");
a.start();
}
public void paintComponent(Graphics g)
{
Graphics2D g2=(Graphics2D)g;
super.paintComponent(g);
g2.rotate(angle,300,300);//300,300 is the center of the screen which I need as the point about which the disc rotates
c.paintIcon(this, g2,100,100);//Image dimensions in (300,300) and JFrame size in main() is (600,600), so I paint the image at (100,100) thus making the center of the JFrame and the image coincide 
angle+=0.001;//To increment angle of rotation for the animation to take place
}
public void actionPerformed(ActionEvent e)
{
repaint();
}
public static void main(String args[])
{
NDisc obj2=new NDisc();
JFrame obj=new JFrame("Physics");
obj.add(obj2);
obj.setSize(600,600);
obj.setLocationRelativeTo(null);
obj.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
obj.setResizable(true);
obj.setVisible(true);
}
}

你搞错了。对于动画,您实际上不需要超过~25 fps。您需要的是调整动画之间所做的功

简单地替换

angle+=0.001;
通过一些可配置的东西,你就完成了。但让我们做得更好:

angle = speed * System.currentTimeMillis() - startTime;

即使你不能保持计时器给出的速度,这也能起作用。缺点是你不能停止时间。但这也有一个简单的解决方案(询问您是否需要)。

大多数动画的速度大约为每秒23帧。NTSC是30。渐进式显示为60。一秒钟有1000毫秒。请解释为什么你认为你需要亚毫秒精度?最后,在
NDisc
构造函数中使用双缓冲(
setDoubleBuffered(true)
)来解决闪烁问题。当您正确缩进代码时,我将给您10次重复(这是一次向上投票)。现在的样子,很难读懂。