Java Applet:为什么这种动画如此之快以及如何使其变慢

时间:2019-06-02 23:11:53

标签: java animation applet

我正在研究Java Applet,只是为了获得积分,我想看看我是否可以使自己的创作轨迹动起来。我使用了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;
}

}

(我知道代码很乱。我正在组织它。)

0 个答案:

没有答案