如何在while循环中调用repaint?

xtupzzrd  于 2021-06-30  发布在  Java
关注(0)|答案(0)|浏览(318)

在任何情况下,我都不能调用repain()方法,除了java.util.timer,它是无用的bcos,它的速度会加快或减慢,而且不是常数。我看到有人说要在分离线程中使用while循环,所以我这样做了,但仍然有效,所有计数都已完成(使用system.out.println(当前更改变量)检查),但repaint()仍然没有被调用?有人能帮我吗,我在这件事上坚持了两个月,不能继续学习别的东西了
下面是我的循环类的外观,我想要的只是关于如何调用repaint的建议,仅此而已:)

public class loop extends drawka implements Runnable{
public  void gameStarter(){
repaint();
spikePos-=3;
if(spikePos<-30) {
    spikePos=800;
}
if(jumpDelay ==5) {
    if(jump == true) {

        switch(jumpPhase) {
        case 1: cubePos-=20;break;
        case 2: cubePos-=40;break;
        case 3: cubePos-=44;break;
        case 4: cubePos-=32;break;
        case 5: cubePos-=4;break;
        case 6: cubePos+=4;break;
        case 7: cubePos+=32;break;
        case 8: cubePos+=44;break;
        case 9: cubePos+=40;break;
        case 10:    
            cubePos+=20;
            this.jumping=false;
        break;
        }

    jumpPhase++;
    jumpDelay=0;
    }
} else {
jumpDelay++;
}

System.out.println(this.test+" updates from start");
this.test++;
}

public void loopCode() {
    long lastTime = System.nanoTime();
    double amountOfTicks = 60.0;
    double ns = 1000000000 / amountOfTicks;
    double delta = 0;
    long timer = System.currentTimeMillis();
    int frames = 0;
    boolean running = true;
    while(running) {
        long now = System.nanoTime();
        delta += (now - lastTime) / ns;
        lastTime = now;
        if(delta >= 1) {
            update();
            delta--;
            draw();
            frames++;
        }
}
}

public void startLoop() {   
    loop threadObj = new loop();
    Thread thread = new Thread(threadObj);
    thread.start();
}

private void draw() {
    gameStarter();
}

private void update() {
    System.out.println("update");

}

@Override
public void run() {
    loopCode(); 
}
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题