Search code examples
javajtablepacman

Refreshing JTable step by step in Java


I am doing a Pacman game using A* algorithm in Java. I searched a lot of questions. I found a solution for one step. But I want to refresh my table in while block and my solution is just refreshing JTable according to the last step(just showing the result) in while(calculated all steps). But I want to refresh and show Pacman's places(location) step by step in while block. It has to look like Pacmans are moving. But I couldn't. My codes is below:

btnStartGame.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {
        //randomly placement of Pacmans
        table_1.setValueAt(yellowPacman, locationyellowX, locationyellowY);
        ...calculating heuristics
        //after calculation
        newXYellow =locationyellowX;
        newYYellow =locationyellowY+1;
        nodeYellow = 10 * newXYellow + newYYellow;
        while(heuristics != zero){  
            ...enemy pacmans' movement
            //after enemy pacmans' movement calculating yellow pacman's movement            
            if((newXYellow>=0 && newXYellow<10 && newYYellow>=0 && newYYellow<10) &&  !wallList.contains(nodeYellow)){
                //calculate heuristic again
                manhattanDistance[0][0] = Math.abs(newXYellow-locationblackX[0])+
                              Math.abs(newYYellow-locationblackX[0]);
                manhattanDistance[0][1] = Math.abs(newXYellow-locationblackX[1])+
                              Math.abs(newYYellow-locationblackX[1]);
                manhattanDistance[0][2] = Math.abs(newXYellow-locationblackX[2])+
                              Math.abs(newYYellow-locationblackX[2]);
                fyellow[0] = manhattanDistance[0][0] + manhattanDistance[0][1] + manhattanDistance[0][2];
                selectedNodeXYellow = newXYellow;
                selectedNodeYYellow = newYYellow;
                timer2.start();//updated    
                while(delay != 0)
                    delay--;
                delay = 4000; 
                locationyellowX = selectedNodeXYellow;
                locationyellowY = selectedNodeYYellow;
                nodeYellow = 10 * selectedNodeXYellow+ selectedNodeYYellow;
                timer3.start();//updated
                while(delay != 0)
                    delay--;
                delay = 10000; 
            }//ending if
        }//ending while
    }
}//ending action    
timer2 = new Timer(ONE_SECOND, new ActionListener() {
    public void actionPerformed(ActionEvent evt) {
        table_1.setValueAt(null, locationyellowX, locationyellowY);//I wanted to delete old moves                
        timer2.stop();
    }    
});
timer3 = new Timer(ONE_SECOND, new ActionListener() {
    public void actionPerformed(ActionEvent evt) {
        table_1.setValueAt(yellowIcon, locationyellowX, locationyellowY);//here I want to show each moves step by step in while block                
        timer3.stop();
    }    
});

UPDATE 1 : Delay was just an idea. Maybe algorithm is calculated too quickly and timers cannot be fired. But it didn't work and also does not timer suppose to be fired at each one second? still I've seen the last values of JTable.


Solution

  • I suspect your main issue here is that you need to understand how the various threads in a Java GUI application work. Specifically, if you have code executing in the event dispatch thread then there will be no updates to the UI while that code is executing. In your case you are making multiple updates to a JTable and then returning control to the display which will then show the last value.

    The solution here is to introduce a timer element that executes each step of your algorithm in each tick of the timer and then returns control to display the result. Check the javax.swing.Timer class for details. I also suggest you read the sections 'concurrency in Swing' and 'How to use Swing Timers' in the Java tutorials.