gpt4 book ai didi

java - 显示 JLabel 运动

转载 作者:行者123 更新时间:2023-12-02 06:40:18 42 4
gpt4 key购买 nike

我正在尝试创建一个简单的动画,其中一系列气泡围绕中心点旋转。我有一个动画版本,其中气泡在开始旋转之前从中心点扩散,效果很好,但是一旦我单击其中一个图像(引发动画),屏幕就会卡住一会儿,然后气泡出现在他们的最终位置,而不是显示他们所做的每一步。

到目前为止我所拥有的是:

    while(bubble[1].getDegree() != 270) 
{
long time = System.currentTimeMillis();

//the below if statement contains the function calls for
//the rotating bubble animations.
next();
draw();

// delay for each frame - time it took for one frame
time = (1000 / fps) - (System.currentTimeMillis() - time);

if (time > 0)
{
try
{
Thread.sleep(time);
}
catch(Exception e){}
}
}


public void draw()
{
for(int i = 1; i < bubble.length; i++)
{
iconLabel[i].setLocation(bubble[i].getX(), bubble[i].getY());
textLabel[i].setLocation((bubble[i].getX()+10),(bubble[i].getY()+10));
}

}

为了清楚起见,方法“next()”只是将气泡的位置更改到适当的位置,我知道这是可以正常工作的,因为我之前已经进行过动画工作,但是一旦我将动画实现到 JLabels,它就停止工作.

如有任何帮助,我们将不胜感激。

最佳答案

绘图被卡住,因为您阻塞了事件调度线程。绘制是在与 while 循环相同的线程中完成的,并且由于循环会阻止其运行时发生任何其他事情,因此 swing 只能在循环完成后才能进行绘制,因此仅绘制最后一个位置.

使用 Swing Timer相反:

timer = new Timer(delay, new ActionListener() {
public void actionPerformed(ActionEvent e) {
// whatever you need for the animation
updatePositions();
repaint();
}
});
timer.start();

然后在处理完您需要的所有帧后调用 timer.stop()

关于java - 显示 JLabel 运动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19210185/

42 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com