我正在尝试用JFrame
制作老虎机。
如何使用平滑动画移动JLabel
?我尝试过的是每次循环都会增加循环的延迟,但这很慢。
它应该在开始时移动得很快,而在结束时移动得很慢。
也许有人可以帮助我更好地计算平滑度?
这是我的Animation
类的代码,扩展了Thread
:
JLabel label; // thre Jlabel
int labelPosY, maxHeight; // label Y position and all Y positions in summary
Animate(JLabel label, int labelPosY, int maxHeight)
{
this.label = label;
this.labelPosY = labelPosY;
this.maxHeight = maxHeight; // for example: -6500
}
public void run()
{
int smooth; // smooth
for (int i = this.maxHeight; i <= 0; i++)
{
try
{
smooth = 100 - (Math.abs(i) / (Math.abs(this.maxHeight) / 100)); // getting percentage of whole moving process
Layout.setLabelPosY(this.label, this.labelPosY++); // changes Y position of JLabel
Thread.sleep(smooth); // waits the always changing process (1ms - 100ms)
}
catch (InterruptedException e) {e.printStackTrace();}
}
}
这是在Java Swing中创建老虎机GUI的一种方法。
将图像文件从Java项目的属性文件夹中读取到BufferedImage
中。
将BufferedImage
划分为各个BufferedImage
。使用此图像,将是9个单独的BufferedImage
。
创建一个BufferedImage
条,其宽度为一幅图像,高为九幅图像。
通过设置BufferedImage
条的三或四个副本的动画动画来创建投币机的旋转图像。您可以在JPanel
的JFrame
上绘制此图像。
跟踪JPanel
中控件JFrame
上的花费和彩金。旋转JButton
也将放置在控件JPanel
上。