java - Java Applet:为什么这个动画这么快,我怎样才能让它变慢
问题描述
我正在开发一个 Java 小程序,只是为了加分,我想看看我是否可以为我所做的事情的轨迹制作动画。我得到了它draw()
的功能来制作我想要的动画,但是动画真的很快,你几乎看不到它。我尝试增加线程进程中的毫秒数,但没有任何区别。希望我的问题是有道理的。我在下面包含了我的代码。
public class Slinkey extends Applet implements Runnable {
private static final long serialVersionUID = 4803949557086825455L;
private Thread animation;
private boolean stopSlinky;
@Override
public void start() {
stopSlinky = false;
animation = new Thread(this);
animation.start();
}
public void paint(Graphics g) { // This can probably be ignored.
Graphics2D g2 = (Graphics2D) g;
int x = 10;
int y = 10;
int w = 100;
int h = 100;
int k = 0;
int otherX = 0;
double xpos = 0, ypos = 10;
System.out.println(xpos + " " + ypos);
Ellipse2D.Double c1 = new Ellipse2D.Double(ypos, ypos, w, h);
for (k = 0; k < 155; k += 5) {
xpos = x + k;
ypos = getYStart(xpos);
System.out.println(xpos + " " + ypos);
c1.setFrame(xpos, ypos, w, h); // (10, 10) (1325, 650)
g2.draw(c1);
}
for (int i = 160; i < 775; i++) {
xpos = x + i;
otherX++;
ypos = getYMain(otherX);
if (ypos < 652) {
System.out.println("Y Coordinate: " + xpos + " " + ypos);
c1.setFrame(xpos, ypos, w, h);
g2.draw(c1);
}
}
for (int j = 775; j < 1325; j++) {
xpos = x + j;
otherX++;
ypos = (0.7) * (getYMain(otherX - 500) + 400);
if (ypos < 652) {
System.out.println("Y Coordinate: " + xpos + " " + ypos);
c1.setFrame(xpos, ypos, w, h);
g2.draw(c1);
}
}
}
public static double getYStart(double x) {
return (0.029) * (Math.pow(x - 10, 2) + 10);
}
public static double getYMain(double x) {
return ((0.005) * Math.pow(x - 350, 2) + 300);
}
@Override
public void run() {
while (true) {
if (stopSlinky) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public void stop() {
stopSlinky = true;
animation = null;
}
}
(我知道代码很乱。我正在整理它。)
解决方案
推荐阅读
- c++ - 特定情况的分段错误
- python - 指定 aws ECS/Fargate 容器依赖项无法部署
- google-analytics - 为什么 Uncaught SyntaxError: Unexpected token ','?
- java - Java中的类,对象,方法和再次询问循环的问题
- python - 找不到使用 selenium 按下的按钮
- c++ - 更有效地在微控制器上对 C++ 进行基准测试
- html - Flexbox 忽略父类
- python - 如何将安装在一台机器上的 Anaconda env 传输到服务器?
- r - R 中的老鼠 - 我如何理解这个命令的作用?
- javascript - 在Javascript中重建嵌套对象