Using Timer to repaint in the fixed time then continuing calculation - java

Currently, I am making a Java program for graph visualization Prim's algorithm in finding minimum spanning tree.
Here is the image of my program's output
while(condition){
//Find the min vertex and min edges
Vertex vertex = findMinVertex();
Edge[] edges = findMinEdges();
//Then, for each vertex and edges I found, I will change the color of
//them and pause the program for 3 seconds, so user can see how
//algorithm works.
repaintAndPause(3000);
}
.
.
private void repaintAndPause(int time){
long start = System.currentTimeMillis();
long end = start + speed;
//Here is the timer for repainting.
Timer timer = new Timer(speed, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e){
GraphPanel.this.repaint();
}
});
timer.setRepeats(false);
timer.setDelay(0);
timer.start();
//And here is for pausing the program, a while loop without any commands.
while(System.currentTimeMillis() < end){}
}
However, I don't know why but the program doesn't work. Yes, there are the pauses of program but, all the edges and vertices are just changed the color at the end of program. They aren't changed every 3 seconds.
Could someone please tell me where I did wrong?
Thank you and hope you have a nice day!

Could someone please tell me where I did wrong?
Yes. You are putting a busy-loop in the Event Dispatching Thread.
while(System.currentTimeMillis() < end){}
You code reads:
do some calculation (busy)
when done, post a "repaint" message, to redraw the panel when not busy
continue being very busy doing nothing for 3 seconds
continue being busy by repeating steps 1 through 4.
The Event Dispatching Thread never finishes processing the first "event" until the end of the algorithm, after the while (condition) loop finally finishes.
You want:
Timer timer = new Timer(speed, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
/* Code to perform one step of Prim's algorithm here */
/* Change edge/vertex colour for step */
/* Call timer.stop(), once done */
GraphPanel.this.repaint();
}
});
timer.setRepeats(true);
timer.setDelay(3000);
timer.start();
On every tick of the timer (once every 3 seconds), one step of the algorithm is performed.
Note this means that each step of the algorithm must run with any partial results stored into class members, so the the next step will be able to retrieve all the information it needs to continue. Stack variables can only be used inside one step; they cannot be used to hold inter-step values.
You could rework the algorithm to use a SwingWorker to run the calculation in its own background thread, and publish intermediate results when computed. The EDT could then repaint as intermediate results are produced. With a Thread#sleep() call, this background thread could delay production of intermediate results to once per 3 seconds.
Alternately, you could run the algorithm, and store multiple copies of the output, once for each 'step'. Then your Panel timer could simply show the output of the next step.

Related

Breaking recursion from listeners

I'm trying to break a special case that makes this code recursive.
I have a Javafx game where there are human and computer players each play when it's his turn and there can be many rounds.
A computer is supposed to play automatically and move to the next player immediately and show no direct indication to the UI (but it's possible to review what it did afterwards).
The problem is in the case where there are only computer players, we will come here the moment the currentBoardPane was loaded, enter the condition since all players are computers, set the board of the next player, and then without finishing the call, call this same function again:
currentBoardPane.addListener((e) -> {
if(gameManager.checkIfCurrentPlayerIsComputer()){
gameManager.playAutoMovesForCurrentPlayer();
gameManager.setNextPlayer(); // it does current player property = next player
//update board on scene
currentBoardPaneIndex = ((currentBoardPaneIndex + 1) % gameManager.getPlayers().size());
currentBoardPane.setValue(boardPanes.get((currentBoardPaneIndex))); //this is a recursive call
}
});
Instead of this, if I subscribe a listener to the currentPlayer property in GameManager then I still need to call setNextPlayer() from that listener which is again recursive.
I can make a special case if all players are a computer, then run the game from a while(true){} instead of listeners and binds but there has to be a better way to break this recursion.
Is there a way to not get into recursion while still having listeners and binds?
Notes:
currentBoardPane signifies the current game board on the screen and it's an ObjectProperty.
Making the following assumptions about your code:
Everything is currently running on the FX Application Thread
The currentBoardPane.setValue(...) causes the UI to update (so you update the UI each move)
then a "quick and dirty" way to do this is:
currentBoardPane.addListener((e) -> {
if(gameManager.checkIfCurrentPlayerIsComputer()){
gameManager.playAutoMovesForCurrentPlayer();
//update board on scene
Platform.runLater(() -> {
gameManager.setNextPlayer(); // it does current player property = next player
currentBoardPaneIndex = ((currentBoardPaneIndex + 1) % gameManager.getPlayers().size());
currentBoardPane.setValue(boardPanes.get((currentBoardPaneIndex))); //this is a recursive call
});
}
});
This delegates the updates to a new Runnable, schedules that runnable to execute on the FX Application Thread, and exits the handler immediately. Thus the call to currentBoardPane.setValue(...) is executed later and is no longer recursive.
In fact, if you do just a little more work:
private final Executor aiExecutor = Executors.newSingleThreadExecutor();
// ...
currentBoardPane.addListener((e) -> {
if(gameManager.checkIfCurrentPlayerIsComputer()){
Task<Void> makeMoveTask = new Task<Void>() {
#Override
protected Void call() {
gameManager.playAutoMovesForCurrentPlayer();
return null ;
}
};
makeMoveTask.setOnSucceeded(e -> {
//update board on scene
gameManager.setNextPlayer(); // it does current player property = next player
currentBoardPaneIndex = ((currentBoardPaneIndex + 1) % gameManager.getPlayers().size());
currentBoardPane.setValue(boardPanes.get((currentBoardPaneIndex))); //this is a recursive call
});
aiExecutor.execute(makeMoveTask);
}
});
then this is exactly the code you would use if computing the move took enough time that it would not be acceptable to block the UI while it was happening. (And if computing the move takes very little time, this will still work just fine.) This assumes that playAutoMovesForCurrentPlayer() doesn't update the UI.

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

Game timer won't stop

I am trying to set a timer to randomly set the frame in the screen, but then, depending on the boolean, set it back to normal. I have already tried .cancel() but it doesn't work. How could I stop this timer?:
if(e.getSource()==impossible){
impossiblity=!impossiblity;
System.out.println(impossiblity);
if(!impossiblity){
new Timer().schedule(new TimerTask() {
public void run() {
int randWidth=(int)((dim.width-gameFrame.getSize().width) * Math.random());
int randHeight=(int)((dim.height-gameFrame.getSize().height) * Math.random());
gameFrame.setLocation(randWidth, randHeight);//sets location to center
}
}, 1000, 1000);
} else gameFrame.setLocation(dim.width/2-gameFrame.getSize().width/2, 0);//sets Location to center of screen
}
right now it does set the gameframe back to normal but then goes right back to randomizing
In general trying to cancel timers or other threads from outside the thread is a bad idea and leads to all sorts of problems.
Your best bet is to set some sort of flag or counter that the TimerTask checks when it comes to run.

Making moving motion with labels using threads in java

I'm having a problem I'm making a pool game and I need the ballos to react when I simulate a hit, the program works like this, you click the direction and power to hit the ball and the click go, the go button is in the GUI class where my labels are created, the button calls a method from my main class that recieves the parameter and then with a while in it, changes the X and Y of the ball till the power is reduced to 0 and then stops, the code is working, but the ball moves until the while stops. So the while works and when the power int is 0 the while goes out and then the new X,Y are painted.
This is the funcion that the button calls, the button sends all the parameters
public void golpe(int pbola, int pvelocidad, String pdireccion, JLabel[] listalabels) throws InterruptedException{
listabolas[pbola].setVelocidad(pvelocidad);
listabolas[pbola].setDireccion(pdireccion);
while (listabolas[pbola].getVelocidad() > 0) {
moverBola(pbola, listalabels);
//System.out.println(listabolas[pbola].getPosX());
//System.out.println(listabolas[pbola].getPosY());
Thread.sleep(500);
//This line is supposed to change the X and Y of the object over and over
//but only does it till the end
listalabels[pbola].setLocation(listabolas[pbola].getPosX(), listabolas[pbola].getPosY());
}
}
Here is the function moverbola(), only copied one "if" so that the code doesn't look to big
private void moverBola(int pbola, JLabel[] listalabels) {
if (listabolas[pbola].getDireccion().equals("SE")) {
int pposX = listabolas[pbola].getPosX();
listabolas[pbola].setPosX(pposX + 1);
int pposY = listabolas[pbola].getPosY();
listabolas[pbola].setPosY(pposY + 1);
}
Swing is a single threaded framework. That is, all interactions with UI are expected to occur from within a single thread, known as the Event Dispatching Thread.
Any action that blocks this thread, will prevent the EDT from updating the screen or processing any new events.
Your while-loop is blocking the EDT, preventing it from painting any updates until after the while-loop is completed.
Take a look at Concurrency in Swing for more details.
There are a number of approaches you could take...
You could use a Thread, but this causes problems as you need to ensure that any changes you make to the UI are re-synced back to the EDT and this can become messy...
For example
You could use a javax.swing.Timer that ticks at a regular interval and you would update any internal parameters from within it's assigned ActionListener. Because the tick events occur within the EDT, it is save to update the screen from within it.
For example
You could use a SwingWorker to run the task in the background. It has methods for re-syncing updates back to the EDT, but might be a little over kill for your purposes...
Updated with a possible Timer example
Caveat- It is very hard to produce a reasonable example with only a code snippet, but, something like this might work
public void golpe(final int pbola, int pvelocidad, String pdireccion, final JLabel[] listalabels) throws InterruptedException{
listabolas[pbola].setVelocidad(pvelocidad);
listabolas[pbola].setDireccion(pdireccion);
Timer timer = new Timer(40, new ActionListener() {
public void actionPerformed(ActionEvent evt) {
if (listabolas[pbola].getVelocidad() == 0) {
((Timer)evt.getSource()).stop();
} else {
moverBola(pbola, listalabels);
}
}
});
timer.setRepeats(true);
timer.start();
}

How to change a JLabel's text in a loop without switching so fast the user can't see it?

I want to create a simple clock using Java. The code is so simple that I will give an example:
for(int i=0;i<=60;i++)
jLabel11.setText( Integer.toString(i) );
The problem is while I'm running my program the result didn't show each update in sequence.
It show only the 60 digit immediately, without showing the change from 1 to 2 to 3 ...
How can i fix this problem?
The problem is that changes to the UI should run on the event dispatch thread, but blocking this loop (and blocking the UI) will stop the screen from repainting. Instead, use a timer to perform regular updates, e.g.
Timer timer = new Timer();
ActionListener updater = new ActionListener()
{
int count;
public void actionPerformed(ActionEvent event) {
jLabel11.setText( Integer.toString(count++) );
if (count==60)
timer.stop();
}
}
timer.setDelay(100);
timer.addActionListener(updater);
timer.start();
See the Sun Tutorial - How to use Swing Timers.

Categories