Menu
Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
Java
Using swing timers from actionListeners
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
<blockquote data-quote="stinkinrich88" data-source="post: 2015194"><p>Hello. I have the following class:</p><p></p><p>import javax.swing.*;</p><p>import java.awt.event.*;</p><p></p><p>class Die extends JLabel implements ActionListener</p><p>{</p><p> private int counter;</p><p> private Timer dieTm = new Timer(70, this);</p><p> private int dieValue;</p><p> private ImageIcon[] dieImg = new ImageIcon[6];</p><p></p><p> public Die()</p><p> {</p><p> ClassLoader cldr = this.getClass().getClassLoader();</p><p> for(int i = 1; i<7; i++)</p><p> dieImg[i-1] = new ImageIcon(cldr.getResource(i +".gif"));</p><p> }</p><p></p><p> public int doRoll()</p><p> {</p><p> setVisible(true);</p><p> counter = 0;</p><p> dieTm.start();</p><p></p><p> while(dieTm.isRunning());</p><p></p><p> dieValue = (int)Math.ceil((Math.random()*6));</p><p></p><p> setIcon(dieImg[dieValue-1]);</p><p> return dieValue;</p><p> }</p><p></p><p> public void actionPerformed(ActionEvent e)</p><p> {</p><p> setIcon(dieImg[(int)Math.ceil((Math.random()*6))-1]);</p><p> counter++;</p><p> System.out.println(counter);</p><p> if (counter == 10) dieTm.stop();</p><p> }</p><p></p><p>}</p><p></p><p>That should animate a dice rolling and stop at a value. It works fine</p><p>when I call it within the code, but when I call it from an action</p><p>Listener the timer's action listener doesn't run, but the timer</p><p>isRunning returns true so it ends up in an infinite While loop waiting</p><p>for it to finish.</p><p></p><p>Any ideas? Thanks!</p></blockquote><p></p>
[QUOTE="stinkinrich88, post: 2015194"] 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! [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
Java
Using swing timers from actionListeners
Top