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!
On Feb 28, 8:01 am, "stinkinric...@googlemail.com"
<stinkinric...@googlemail.com> wrote:
> Hello. I have the following class:
>
[quoted text clipped - 46 lines]
>
> 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>
On Feb 28, 8:01 am, "stinkinric...@googlemail.com"
<stinkinric...@googlemail.com> wrote:
> Hello. I have the following class:
>
[quoted text clipped - 46 lines]
>
> 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;
}
}
stinkinrich88@googlemail.com - 01 Mar 2007 09:29 GMT
On Feb 28, 8:33 pm, "Daniel Pitts" <googlegrou...@coloraura.com>
wrote:
> On Feb 28, 8:01 am, "stinkinric...@googlemail.com"
>
[quoted text clipped - 77 lines]
>
> }
Ahhh! Genius! Thank you very much for both of your help! It works
well!