我用这个代码制作轮盘赌时遇到了麻烦.当我点击“旋转!”时,目标是旋转轮子.按钮.我通过创建一个for循环来完成此操作,该循环应该将轮的状态从true更改为false,这会更改方向.当足够快地完成时,应该产生运动的错觉.
我遇到的问题是:尽管我放置了重绘(),但是在完成for循环之后我的轮子才重新粉刷.所以,它只旋转一个滴答声.
这是我的ActionListener的一些示例代码:
public class spinListener implements ActionListener
{
RouletteWheel wheel;
int countEnd = (int)(Math.random()+25*2);
public spinListener(RouletteWheel w)
{
wheel = w;
}
public void actionPerformed(ActionEvent e)
{
for (int i = 0; i
{
try
{
Thread.sleep(100);
if (wheel.getStatus() == true)
{
wheel.setStatus(false);
repaint();
}
if (wheel.getStatus() == false)
{
wheel.setStatus(true);
repaint();
}
}
catch (InterruptedException ex)
{
Logger.getLogger(WheelBuilder.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
更新:我发现了问题.以下是我为遇到类似问题的人所做的更改.
public class spinListener implements ActionListener
{
Timer tm = new Timer(100, this);
int count = 0;
public void actionPerformed(ActionEvent e)
{
tm.start();
changeWheel();
}
public void changeWheel()
{
int countEnd = (int)(Math.random()+20*2);
if (count < countEnd)
{
wheel.setStatus(!wheel.getStatus());
repaint();
count++;
}
}
}