drew moore
drew moore

Reputation: 32670

GUI not updating properly

I have just finished a project that involves creates a maze and programming three "robots" to move through it using different strategies. The classes involved in my implementation are:

Finally, I have an entry-point with a main method that contains only the following:

    Maze m = new Maze();
    MazeFrame mf = new MazeFrame(m);
    RightHandRobot mr = new RightHandRobot(m, mf);
    mr.go(m);

This works perfectly - the maze is displayed on the GUI and I can watch the robot moving through it until he reaches the end - the desired effect.

My problem is this: On the top of the JFrame are three buttons, each with an ActionListener that creates an instances of one of the robot types and calls its go method, which causes it to move until it is at the "finish cell". When I click one of these buttons, the robot is created and begins moving (which I can verify by println statements executed on each move), but I cannot see the robot moving on the GUI. The GUI is only update when the robot reaches the finish (i.e. when the go() method completes), at which point the entire path taken by the robot is shown.

Why does the GUI update properly when the robot is created by the main method, but not when it is created by the JFrame class?

I'm not sure what code you'll need to see to help me with this, so let me know what you need to see.

Upvotes: 1

Views: 124

Answers (1)

trashgod
trashgod

Reputation: 205775

Use a javax.swing.Timer to pace the animation, as shown in this FleetPanel. At each tick, the timer's listener should render only running robots.

image

Upvotes: 2

Related Questions