我想用Java制作一个程序来高速旋转牛顿光盘,使其呈现白色。但定时器类的最小延迟仅为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);
}
}
答案 0 :(得分:1)
你错了。对于动画,你真的需要不超过~25 fps。您需要的是调整动画之间的工作。
简单地替换
angle+=0.001;
通过可配置的东西,你就完成了。但是,让我们做得更好:
angle = speed * System.currentTimeMillis() - startTime;
即使您无法保持计时器给出的速度,这也能正常工作。缺点是你无法停止时间。但这也有一个简单的解决方案(问你是否需要它)。