473,465 Members | 1,899 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Keep randoming, until button pressed

144 New Member
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
2 2064
JosAH
11,448 Recognized Expert MVP
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
thesti
144 New Member
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

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

Similar topics

2
by: jb | last post by:
Hello, I need to know which button was pressed in the submit , i tried reading the vaule of submit it the validateDate function but it returns 'undefined' value ; I do this in asp all the time, Not...
8
by: Syed Ali | last post by:
Hello, I have 1 HTML form with 4 submit buttons and 10 textfield entry areas. If submit button1 is pressed I need to make sure that all 10 textfield entries have been filled before submitting...
3
by: James McGivney | last post by:
I have a project in VS.NET using C# I have a series of buttons on an aspx page. When one of the buttons is pressed, a panel becomes visible and allows the user to enter and edit data. I want to...
14
by: tshad | last post by:
I posted this on the asp.net group, also. I wasn't sure whether this was an asp.net problem or a javascript problem. I have a page that was originally created from a program I found on the net...
4
by: karenmiddleol | last post by:
I have the following form the user enters the From and to period and presses the Submit button and the form fields are cleared once the submit button is pressed. Is there a way I can keep the...
19
by: darrel | last post by:
On my vb.net page, I have 4 sets of inputs + form buttons. example: Search: (GO) Zip: (GO) County: (GO) County: (GO) The problem is if I go to the page, type in a zip code, and hit...
3
by: Jean | last post by:
Hello, I´m a new user to C# and I have a question. How to make my application emulate a key pressed? Not just the key press, but keep the key pressed? For example, let´s say that I want to emulate...
4
by: 200dogz | last post by:
Hi guys, Just a quick and simple question: i'm trying to have a loading image displayed when a submit button is pressed. Basically what I have at this stage is an asp:image with its...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.