Using swing timers from actionListeners

S

stinkinrich88

Hello. I have the following class:

import javax.swing.*;
import java.awt.event.*;

class Die extends JLabel implements ActionListener
{
private int counter;
private Timer dieTm = new Timer(70, this);
private int dieValue;
private ImageIcon[] dieImg = new ImageIcon[6];

public Die()
{
ClassLoader cldr = this.getClass().getClassLoader();
for(int i = 1; i<7; i++)
dieImg[i-1] = new ImageIcon(cldr.getResource(i +".gif"));
}

public int doRoll()
{
setVisible(true);
counter = 0;
dieTm.start();

while(dieTm.isRunning());

dieValue = (int)Math.ceil((Math.random()*6));

setIcon(dieImg[dieValue-1]);
return dieValue;
}

public void actionPerformed(ActionEvent e)
{
setIcon(dieImg[(int)Math.ceil((Math.random()*6))-1]);
counter++;
System.out.println(counter);
if (counter == 10) dieTm.stop();
}

}

That should animate a dice rolling and stop at a value. It works fine
when I call it within the code, but when I call it from an action
Listener the timer's action listener doesn't run, but the timer
isRunning returns true so it ends up in an infinite While loop waiting
for it to finish.

Any ideas? Thanks!
 
D

Daniel Pitts

Hello. I have the following class:

import javax.swing.*;
import java.awt.event.*;

class Die extends JLabel implements ActionListener
{
private int counter;
private Timer dieTm = new Timer(70, this);
private int dieValue;
private ImageIcon[] dieImg = new ImageIcon[6];

public Die()
{
ClassLoader cldr = this.getClass().getClassLoader();
for(int i = 1; i<7; i++)
dieImg[i-1] = new ImageIcon(cldr.getResource(i +".gif"));
}

public int doRoll()
{
setVisible(true);
counter = 0;
dieTm.start();

while(dieTm.isRunning());

dieValue = (int)Math.ceil((Math.random()*6));

setIcon(dieImg[dieValue-1]);
return dieValue;
}

public void actionPerformed(ActionEvent e)
{
setIcon(dieImg[(int)Math.ceil((Math.random()*6))-1]);
counter++;
System.out.println(counter);
if (counter == 10) dieTm.stop();
}

}

That should animate a dice rolling and stop at a value. It works fine
when I call it within the code, but when I call it from an action
Listener the timer's action listener doesn't run, but the timer
isRunning returns true so it ends up in an infinite While loop waiting
for it to finish.

Any ideas? Thanks!

do you ever call dieTime.start()?

Can you give use an <sscce> that we can use to recreate the problem?
<http://physci.org/codes/sscce>
 
D

Daniel Pitts

Hello. I have the following class:

import javax.swing.*;
import java.awt.event.*;

class Die extends JLabel implements ActionListener
{
private int counter;
private Timer dieTm = new Timer(70, this);
private int dieValue;
private ImageIcon[] dieImg = new ImageIcon[6];

public Die()
{
ClassLoader cldr = this.getClass().getClassLoader();
for(int i = 1; i<7; i++)
dieImg[i-1] = new ImageIcon(cldr.getResource(i +".gif"));
}

public int doRoll()
{
setVisible(true);
counter = 0;
dieTm.start();

while(dieTm.isRunning());

dieValue = (int)Math.ceil((Math.random()*6));

setIcon(dieImg[dieValue-1]);
return dieValue;
}

public void actionPerformed(ActionEvent e)
{
setIcon(dieImg[(int)Math.ceil((Math.random()*6))-1]);
counter++;
System.out.println(counter);
if (counter == 10) dieTm.stop();
}

}

That should animate a dice rolling and stop at a value. It works fine
when I call it within the code, but when I call it from an action
Listener the timer's action listener doesn't run, but the timer
isRunning returns true so it ends up in an infinite While loop waiting
for it to finish.

Any ideas? Thanks!

Ah, I see the problem...

doRoll blocks until dieTm.stop() is called... The problem is, that
the Timer event won't be able to be dispatched until doRoll returns,
which means that actionPerformed won't be called, which means
dieTm.stop() won't be called, which means... DEADLOCK!

public int doRoll() {
setVisible(true);
counter = 0;
dieValue = (int)Math.ceil((Math.random()*6));
dieTm.start();
return dieValue;
}

public void actionPerformed(ActionEvent e) {
setIcon(dieImg[(int)Math.ceil((Math.random()*6))-1]);
counter++;
System.out.println(counter);
if (counter == 10) {
dieTm.stop();
setIcon(dieImg[dieValue-1]);
return dieValue;
}
}
 
S

stinkinrich88

Hello. I have the following class:
import javax.swing.*;
import java.awt.event.*;
class Die extends JLabel implements ActionListener
{
private int counter;
private Timer dieTm = new Timer(70, this);
private int dieValue;
private ImageIcon[] dieImg = new ImageIcon[6];
public Die()
{
ClassLoader cldr = this.getClass().getClassLoader();
for(int i = 1; i<7; i++)
dieImg[i-1] = new ImageIcon(cldr.getResource(i +".gif"));
}
public int doRoll()
{
setVisible(true);
counter = 0;
dieTm.start();

dieValue = (int)Math.ceil((Math.random()*6));
setIcon(dieImg[dieValue-1]);
return dieValue;
}
public void actionPerformed(ActionEvent e)
{
setIcon(dieImg[(int)Math.ceil((Math.random()*6))-1]);
counter++;
System.out.println(counter);
if (counter == 10) dieTm.stop();
}

That should animate a dice rolling and stop at a value. It works fine
when I call it within the code, but when I call it from an action
Listener the timer's action listener doesn't run, but the timer
isRunning returns true so it ends up in an infinite While loop waiting
for it to finish.
Any ideas? Thanks!

Ah, I see the problem...

doRoll blocks until dieTm.stop() is called... The problem is, that
the Timer event won't be able to be dispatched until doRoll returns,
which means that actionPerformed won't be called, which means
dieTm.stop() won't be called, which means... DEADLOCK!

public int doRoll() {
setVisible(true);
counter = 0;
dieValue = (int)Math.ceil((Math.random()*6));
dieTm.start();
return dieValue;

}

public void actionPerformed(ActionEvent e) {
setIcon(dieImg[(int)Math.ceil((Math.random()*6))-1]);
counter++;
System.out.println(counter);
if (counter == 10) {
dieTm.stop();
setIcon(dieImg[dieValue-1]);
return dieValue;
}

}

Ahhh! Genius! Thank you very much for both of your help! It works
well!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top