简体   繁体   English

Java - 具有 thread.sleep 的多个线程的不同绘制

[英]Java - Different paint with multiple threads that have thread.sleep

I'm newbie and I'm trying to make a game but than I don't understand how to use repaint() from different paint with different threads too.我是新手,我正在尝试制作游戏,但我不明白如何使用具有不同线程的不同涂料的 repaint() 。 One thread with thread.sleep and the other one doesn't have.一个线程有 thread.sleep 而另一个没有。

Here's my piece of code :这是我的一段代码:

GamePanel :游戏面板:

public class GamePanel extends JPanel implements MouseListener, MouseMotionListener{

EnemyEngine enemyE = new EnemyEngine();

public GamePanel() {

    new Thread(new Runnable() {

        @Override
        public void run() {
            while(true){            
                repaint();
            }
        }
    }).start();

    new Thread(new Runnable() {

        @Override
        public void run() {
            while(true){            
                enemyE.update();
                enemyE.repaint();

                try { 
                    Thread.sleep(1000/10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }).start();
}
public void paintComponent(Graphics g) {
    // board painting
}

}

EnemyEngine:敌人引擎:

public class EnemyEngine extends JPanel{

Vector<Enemy> enemyVect = new Vector<>();

Random rand = new Random();

public void paintComponent(Graphics g){
    for (Enemy enemy : enemyVect) {
        enemy.render(g);
    }
}

public void update() { 
    for (Enemy enemy : enemyVect) {
        enemy.move();
    }
}

}

I have already search on the internet but it still didn't work...or maybe I'm the stupid one :/我已经在互联网上搜索过,但仍然没有用……或者我可能是个愚蠢的人:/

Please help me senpai请帮助我前辈

I don't understand clearly your issue about repaint?我不太明白你关于重绘的问题? If you want to refresh ui immediately, why don't you use paintImmediately(0, 0, getWidth(), getHeight());如果你想立即刷新ui,为什么不使用paintImmediately(0, 0, getWidth(), getHeight());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM