首页 > 解决方案 > 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;
}

}

(我知道代码很乱。我正在整理它。)

标签: javaanimationapplet

解决方案


推荐阅读