Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2007

Tip: Looking for answers? Try searching our database.

Using swing timers from actionListeners

Thread view: 
stinkinrich88@googlemail.com - 28 Feb 2007 16:01 GMT
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!
Daniel Pitts - 01 Mar 2007 01:27 GMT
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>
Daniel Pitts - 01 Mar 2007 01:33 GMT
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!


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.