Results 1 to 4 of 4
- 11-20-2010, 01:17 AM #1
Member
- Join Date
- Jun 2010
- Posts
- 28
- Rep Power
- 0
repaint() calls sometimes not being processed
I'm making a turn-based game and was doing a test of a simple AI "dummy" player. I gave it a simple task, just to move the cursor around a bit, open the pause menu, and end its turn. To give the AI turns a bit more "animated" look, I required it to basically go through the same control flow as a human player would, by calling the methods tied to bound keys and telling it to wait for a short period between issuing commands. For example, a human player presses the left key. If he is allowed to move that time, the key's actionPerformed() method calls the leftKey() method. The AI, upon moving the cursor left, also calls the leftKey() method, but doesn't need to verify it, because the program assumes it's the AI turn. The methods for all of these keybindings end in repaint() calls.
This works fine for a human player; the action's made, the JPanel's updated, and everything proceeds normally. However, on the AI player's turn, it seems that the repaint() commands are ignored. When I run the program with myself as player 0 and the AI as player 1, and I end my turn, the JPanel basically "freezes" until the AI's turn is over, then immediately updates when it's my turn again. Looking at the program and some print tests, the AI's doing its job fine - the changes just aren't showing up.
Why is this happening, and what's the best way to fix it? I'd greatly appreciate any help.
-
We can only guess what the problem is as you're not provided us with code.
Having said that, you're not calling Thread.sleep on the main Swing thread, the EDT are you? If so, don't do this as it will cause the whole app to sleep. Better to use a javax.swing.Timer (Swing Timer) instead. The tutorials will show you how to do this.
Edit: Also, please look up "Concurrency in Swing" as Darryl recommended you do in his last reply to you in this thread: http://www.java-forums.org/new-java/...cs-jpanel.html
His recommendation was valid then, but is even more valid for this problem.Last edited by Fubarable; 11-20-2010 at 02:11 AM.
- 11-20-2010, 11:20 PM #3
Member
- Join Date
- Jun 2010
- Posts
- 28
- Rep Power
- 0
Hmm, alright, here's what should be all of the relevant code:
When the player ends their turn, the following runnable is called:
Java Code:endturn = new Runnable() { public void run() { mc.miscon.nextTurn(); // Updates various battlefield data costUpdate(); // Same cursorcommand = "battleidle"; // Determines what various cursor commands should do drawspecial = "battleidle"; // Tells JPanel to just draw the battlefield drawmenu = "nothing"; // closes the menu checkMessageDisplay(); // See if the system has any messages to display for this turn repaint(); aiturn = aiUpdate(); // aiturn is a boolean describing if the current player is an AI if (aiturn) aiRoutine(); } };
Java Code:public void aiRoutine() { wait(2000); // Pause before moving while (cursor[0] > 0) { // Move cursor to left end of screen leftKey(); wait(80); } while (cursor[1] > 0) { // Move cursor to top end of screen upKey(); wait(80); } wait(800); pause(); // Opens the pause menu wait(200); select(); // Selects first choice in pause menu, which is "End turn" }
Java Code:public void wait(int ms) { final int towait = ms; Runnable waitrunner = new Runnable(){ @Override public void run() { try { Thread.sleep(towait); } catch (InterruptedException e) { e.printStackTrace(); } } }; waitrunner.run(); }
-
Yeah, you're blocking the EDT. Read on Concurrency in Swing to see what you're doing wrong and why it's freezing your app.
Similar Threads
-
Making calls from pc
By ilyaufo in forum Sun Java Wireless ToolkitReplies: 1Last Post: 05-17-2010, 05:38 PM -
To populate the data processed into a .csv or excel file
By jaiminparikh in forum Advanced JavaReplies: 2Last Post: 02-27-2009, 12:56 AM -
Controlling method calls
By bugger in forum New To JavaReplies: 2Last Post: 01-04-2008, 02:14 PM
Bookmarks