简体   繁体   中英

Repaint() not being called in Thread

I have a jFrame that is implementing a poker game. I have a thread so that the computer opponents take time with their moves. I've tried to implement it so that the Thread waits when a human turn comes up. Before I even put a human player in, though, the frame doesn't call repaint() . I've used the debugger in Netbeans to check this: it does get to the line where the frame calls repaint() , but for some reason it doesn't actually do it. Here's the code:

public void run() {

    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < players.size(); j++) {
            Card card = deck.draw();
            players.get(i).addToHand(card);
            output.append("Player " + players.get(i).getName() + " got a " + card + ".\n");
            System.out.println("Player " + players.get(i).getName() + " got a " + card + ".\n");
        }
    }

    while (true) {

        if (!players.isEmpty() && players.get(0) instanceof HumanPlayer)
            humansTurn = true;

        if (humansTurn) {
            synchronized (this) {
                try {
                    this.wait();
                } catch (InterruptedException e) {
                }
            }
        } else if (humanMoveMade) {
            playMove(humanMove, players.remove(0));
            humanMoveMade = false;
        }else {
            //unrelated code, then:

            debug.update();

            repaint();

            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
            }
        }
    }
}

Basically it's supposed to get to that else whenever the human player isn't making his or her move, and it does get to that repaint , but it never goes through for some reason. The window appears, but none of the components.

EDIT: I should also mention that the debug.update() method call before the repaint() is supposed to update information on another frame, but nothing is showing up in that window either...

I need to have this ready soon, so I really need some help with this. What is going on?

Ugh...sorry, false alarm. I had forgotten to check when this frame gets created. Turns out I had accidentally called the run() method instead of running the thread the way it normally is (I had tried to implement something different beforehand). After going back to making a new thread and calling start() , it works now.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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