gpt4 book ai didi

Java 动画 : Rotating Image

转载 作者:搜寻专家 更新时间:2023-11-01 02:17:06 25 4
gpt4 key购买 nike

我有一个非常简单的 Java 动画任务。我需要创建一个基本的“Wheel of Fortune Applet”。基本上将显示的是一个滚轮和一个按钮。当按下那个按钮时,我希望它选择一个随机的度数(比如在 720-3600 的范围内)并将轮子旋转那么多度数。然后我将使用一些逻辑将该学位数字转换为货币值(value)。我的问题出在动画中,如何让图像以恒定速度旋转 x 度数?有 Swing 功能吗?非常感谢您的帮助,除此之外,我现在不需要了解任何有关 Java 动画的知识。

最佳答案

我假设您了解如何旋转图像一次。如果没有,您可能可以通过快速谷歌搜索找到它。

您需要的是一个为您旋转它的后台进程。它是这样工作的:

/**
* Warning - this class is UNSYNCHRONIZED!
*/
public class RotatableImage {
Image image;
float currentDegrees;

public RotateableImage(Image image) {
this.image = image;
this.currentDegrees = 0.0f;
this.remainingDegrees = 0.0f;
}

public void paintOn(Graphics g) {
//put your code to rotate the image once in here, using current degrees as your rotation
}

public void spin(float additionalDegrees) {
setSpin(currentDegrees + additionalDegrees);
}

public void setSpin(float newDegrees) {
currentDegrees += additionalDegrees;
while(currentDegrees < 0f) currentDegrees += 360f;
while(currentDegrees >= 360f) currentDegrees -= 360f;
}

}

public class ImageSpinner implements Runnable {
RotateableImage image;
final float totalDegrees;
float degrees;
float speed; // in degrees per second
public ImageSpinner(RotatableImage image, float degrees, float speed) {
this.image = image;
this.degrees = degrees;
this.totalDegrees = degrees;
this.speed = speed;
}

public void run() {
// assume about 40 frames per second, and that the it doesn't matter if it isn't exact
int fps = 40;
while(Math.abs(degrees) > Math.abs(speed / fps)) { // how close is the degrees to 0?
float degreesToRotate = speed / fps;
image.spin(degreesToRotate);
degrees -= degreesToRotate;
/* sleep will always wait at least 1000 / fps before recalcing
but you have no guarantee that it won't take forever! If you absolutely
require better timing, this isn't the solution for you */
try { Thread.sleep(1000 / fps); } catch(InterruptedException e) { /* swallow */ }
}
image.setSpin(totalDegrees); // this might need to be 360 - totalDegrees, not sure
}

}

关于Java 动画 : Rotating Image,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5047967/

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