By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,080 Members | 1,845 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,080 IT Pros & Developers. It's quick & easy.

Keep randoming, until button pressed

100+
P: 144
hi,
this might be a programming logic problem

the scenario is,
i have a label with imageIcon and two buttons, A and B.
the label (with imageIcon) is representing the value of a randomed integer from 1 until 6 (a dice actually).

when a user pressed button A, the image of the label will keep changing
as the dice is randomed.
it will be stopped when user pressed button B.

i've tried using recursion, but it's failed as it raise the StackOverflow exception.
should i use something like a sleep function?

is there any other solution, i hope a simple solution (no threading things)

thank you.
May 22 '08 #1
Share this Question
Share on Google+
2 Replies


Expert 10K+
P: 11,448
i've tried using recursion, but it's failed as it raise the StackOverflow exception.
should i use something like a sleep function?

is there any other solution, i hope a simple solution (no threading things)

thank you.
Recursion is not the way to go here but you bet you have to use a bit of threading
here: one thing has to keep on rolling the die; another thing has to display a die
value and yet another thing has to be alive to listen to a button press to make the
first thing stop.

Swing is a single thread of execution. This thread listens to events and draws
everything on the screen; if you lock it up in a loop to roll the die it can't do its
tasks anymore and the screen 'freezes'. That is not what you want.

But things aren't that difficult if you design it well. Build a simple class Die that
does this:

Expand|Select|Wrap|Line Numbers
  1. public class Die {
  2.    ...
  3.    public Die() { ... }
  4.    ...
  5.    public roll() {
  6.       // randomly produce a number 1, 2, ... 6
  7.       // and show it in the label
  8.    }
  9. }
  10.  
Here is a class Roller that rolls the Die continuously; it sleeps 'ms' milliseconds
between rolls:

Expand|Select|Wrap|Line Numbers
  1. public class Roller implements Runnable {
  2.    private long ms;
  3.    volatile private boolean stop;
  4.    public Roller(long ms, Die die) { 
  5.       this.ms= ms;
  6.       this.die= die;
  7.     }
  8.    public void stop() { this.stop= true; }
  9.    public void run() {
  10.       while (!stop) {
  11.          die.roll();
  12.          try {
  13.             Thread.sleep(ms);
  14.          }
  15.          catch (InterruptedException ie) { /* shouldn't happen */ }
  16.       }
  17.    }
  18. }
  19.  
This object rolls a die, sleeps and repeats the same over and over again until
its 'stop' variable is set to 'true'.

Note that I made this Roller class implement the Runnable interface so it can
be handed to a Thread that can run it when the thread is started. The second
button should be passed this Roller as well so it can stop it when the button
is pressed.

The first button should create a Roller, pass it to the second button and hand
the roller to a freshly created Thread and start the thread.

I'm about sure that you can manage from here.

kind regards,

Jos
May 22 '08 #2

100+
P: 144
Recursion is not the way to go here but you bet you have to use a bit of threading
here: one thing has to keep on rolling the die; another thing has to display a die
value and yet another thing has to be alive to listen to a button press to make the
first thing stop.

Swing is a single thread of execution. This thread listens to events and draws
everything on the screen; if you lock it up in a loop to roll the die it can't do its
tasks anymore and the screen 'freezes'. That is not what you want.

But things aren't that difficult if you design it well. Build a simple class Die that
does this:

Expand|Select|Wrap|Line Numbers
  1. public class Die {
  2.    ...
  3.    public Die() { ... }
  4.    ...
  5.    public roll() {
  6.       // randomly produce a number 1, 2, ... 6
  7.       // and show it in the label
  8.    }
  9. }
  10.  
Here is a class Roller that rolls the Die continuously; it sleeps 'ms' milliseconds
between rolls:

Expand|Select|Wrap|Line Numbers
  1. public class Roller implements Runnable {
  2.    private long ms;
  3.    volatile private boolean stop;
  4.    public Roller(long ms, Die die) { 
  5.       this.ms= ms;
  6.       this.die= die;
  7.     }
  8.    public void stop() { this.stop= true; }
  9.    public void run() {
  10.       while (!stop) {
  11.          die.roll();
  12.          try {
  13.             Thread.sleep(ms);
  14.          }
  15.          catch (InterruptedException ie) { /* shouldn't happen */ }
  16.       }
  17.    }
  18. }
  19.  
This object rolls a die, sleeps and repeats the same over and over again until
its 'stop' variable is set to 'true'.

Note that I made this Roller class implement the Runnable interface so it can
be handed to a Thread that can run it when the thread is started. The second
button should be passed this Roller as well so it can stop it when the button
is pressed.

The first button should create a Roller, pass it to the second button and hand
the roller to a freshly created Thread and start the thread.

I'm about sure that you can manage from here.

kind regards,

Jos
Great! thanks Jos, it works!
May 23 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.