在JFrame中平滑移动JLabel

问题描述 投票:0回答:1

我正在尝试用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();}
    }
}

Image

java image swing animation jlabel
1个回答
0
投票

这是在Java Swing中创建老虎机GUI的一种方法。

  1. 下载像这样的老虎机映像。您可以在Google上找到许多其他图像。

Slot machine images

  1. 将图像文件从Java项目的属性文件夹中读取到BufferedImage中。

  2. BufferedImage划分为各个BufferedImage。使用此图像,将是9个单独的BufferedImage

  3. 创建一个BufferedImage条,其宽度为一幅图像,高为九幅图像。

  4. 通过设置BufferedImage条的三或四个副本的动画动画来创建投币机的旋转图像。您可以在JPanelJFrame上绘制此图像。

  5. 跟踪JPanel中控件JFrame上的花费和彩金。旋转JButton也将放置在控件JPanel上。

© www.soinside.com 2019 - 2024. All rights reserved.