简体   繁体   English

在 JFrame 中平稳移动 JLabel

[英]Smoothly moving JLabel in a JFrame

I am trying to make a Slot Machine with a JFrame .我正在尝试使用JFrame制作老虎机

How do I move my JLabel with a smooth animation?如何使用平滑的 animation 移动我的JLabel What I have tried is that I am increasing the delay in the loop with every round, but that is to slow.我尝试过的是,我每轮都在增加循环中的延迟,但这很慢。

It should move at the beginning very fast and getting at the end very slow.它应该在开始时非常快速地移动并且在结束时非常缓慢。

Maybe anyone can help me to calculate the smooth better?也许有人可以帮助我更好地计算平滑度?

Here is my code of my Animation class extending Thread :这是我的Animation class 扩展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图片

Here's one way to create a slot machine GUI in Java Swing.这是在 Java Swing 中创建老虎机 GUI 的一种方法。

  1. Download slot machine images, like this one.下载老虎机图像,例如这个。 You can find many other images with Google.您可以使用 Google 找到许多其他图像。

老虎机图片

  1. Read the image file from a properties folder in your Java project into a BufferedImage .从 Java 项目的属性文件夹中将图像文件读取到BufferedImage中。

  2. Divide the BufferedImage into individual BufferedImage s.BufferedImage划分为单独的BufferedImage With this image, that would be 9 individual BufferedImage s.有了这张图片,那将是 9 个单独的BufferedImage

  3. Create a BufferedImage strip that's one image wide by nine images high.创建一个宽 9 个图像的BufferedImage条。

  4. Create the rotating image of a slot machine by animating the rotation of three or four copies of the BufferedImage strip.通过为BufferedImage条带的三个或四个副本的旋转设置动画来创建老虎机的旋转图像。 You draw this image on a JPanel in the JFrame .您在JFrame中的JPanel上绘制此图像。

  5. Keep track of the money spent and winnings on a control JPanel in the JFrame .JFrame中的控制JPanel上跟踪花费和赢得的钱。 The spin JButton would also be placed on the control JPanel .旋转JButton也将放置在控件JPanel上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM