Using swing timers from actionListeners

Discussion in 'Java' started by stinkinrich88@googlemail.com, Feb 28, 2007.

  1. Guest

    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!
     
    , Feb 28, 2007
    #1
    1. Advertising

  2. Daniel Pitts Guest

    On Feb 28, 8:01 am, ""
    <> wrote:
    > 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>
     
    Daniel Pitts, Mar 1, 2007
    #2
    1. Advertising

  3. Daniel Pitts Guest

    On Feb 28, 8:01 am, ""
    <> wrote:
    > 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;
    }
    }
     
    Daniel Pitts, Mar 1, 2007
    #3
  4. Guest

    On Feb 28, 8:33 pm, "Daniel Pitts" <>
    wrote:
    > On Feb 28, 8:01 am, ""
    >
    >
    >
    > <> wrote:
    > > 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;
    > }
    >
    > }


    Ahhh! Genius! Thank you very much for both of your help! It works
    well!
     
    , Mar 1, 2007
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Yin99
    Replies:
    2
    Views:
    1,691
    sanjay manohar
    Jan 27, 2005
  2. Replies:
    0
    Views:
    299
  3. S.T
    Replies:
    2
    Views:
    583
  4. Ananda Jayeshwara (Arut Selvan)

    Timers using hash tables

    Ananda Jayeshwara (Arut Selvan), Jan 9, 2009, in forum: C Programming
    Replies:
    1
    Views:
    452
    CBFalconer
    Jan 10, 2009
  5. Replies:
    0
    Views:
    76
Loading...

Share This Page