Results 1 to 4 of 4
  1. #1
    DrKilljoy is offline Member
    Join Date
    Jun 2010
    Posts
    28
    Rep Power
    0

    Default 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.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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: Having difficulty adding graphics to a JPanel

    His recommendation was valid then, but is even more valid for this problem.
    Last edited by Fubarable; 11-20-2010 at 01:11 AM.

  3. #3
    DrKilljoy is offline Member
    Join Date
    Jun 2010
    Posts
    28
    Rep Power
    0

    Default

    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();
    			}
    		};
    Here's the AI routine:

    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"
    	}
    Here's the code for the wait() method:

    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();
    	}
    I've double-checked, and i can verify that all of the methods the AI uses to control the game have repaint calls at the end, which should be executed every time the methods are called.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. Making calls from pc
    By ilyaufo in forum Sun Java Wireless Toolkit
    Replies: 1
    Last Post: 05-17-2010, 04:38 PM
  2. To populate the data processed into a .csv or excel file
    By jaiminparikh in forum Advanced Java
    Replies: 2
    Last Post: 02-26-2009, 11:56 PM
  3. Controlling method calls
    By bugger in forum New To Java
    Replies: 2
    Last Post: 01-04-2008, 01:14 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •