[prog] Seeking and appropriate Java component
Lisa W. Eriksen
lisaer at stud.ntnu.no
Wed May 5 11:58:53 EST 2004
On Thu, 6 May 2004 19:11:05 +1000
Sue Stones <suzo at spin.net.au> wrote:
> I am writting a program in Java (for a concurrent Programming assignment). It
> is a graphical application that simulates the movement of some "particles" ie
> brownian motion type of thing. Each particle is run as a separate thread,
> and it is posible to remove particles and create new ones.
>
> I want to be able to state somewhere, how many particles there are. I have
> put in a panel and tried to use a label, but there are 2 problems. (i) the
> label is created before the threads (ii) I don't know how to update a label.
>
> Does anyone know of any Java components that are like a lable but which can be
> updated?
>
> Searching hasn't produced a list of possible componenets, so I don't quite
> know where to look.
>
> Another problem with the program, is that all the particles drift to the north
> west. I am using "Random" to produce the movements, but clearly "Random is
> biased. Any one know the solution to this? Here are the calls to Random.
>
> protected final Random rng = new Random();
>
> public synchronized void move() {
> x += rng.nextInt(10) - 5;
> y += rng.nextInt(20) - 10;
> } // move
>
> sue
> _______________________________________________
> Programming mailing list
> Programming at linuxchix.org
> http://mailman.linuxchix.org/mailman/listinfo/programming
Hi,
I assume you are talking about a javax.swing.JLabel? If that's the case, then a call to myLabel.setText(newNumberOfParticles) should do it.
If it's not a JLabel, then please tell me, I'd love to help! :) This is the first time I've had the chance to try to help anyone on the list..
Concerning the Random: Could you use java.lang.Math.random() instead?
(The API says: "static double random() :Returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0.")
At my uni Java exercises we used Math.random() to get the random number we wanted. As you might know, to get an int from a double you simply use Double's intValue(). If we wanted a number higher than 1, we multiplied the resulting random double (between 0 and 1) with a number which created a range that we wanted.
An example:
/*
* Math.random() generates a double between 0 and 1
* Multiply the random double by 10 to get a range of 0-10 (or 9.9 to be exact, I think)
* Double's intValue() then casts the double to an int
*/
public synchronized void move() {
x += (Math.random()*10).intValue() - 5; // Since I don't know why you subtract 5 and 10, I kept them.
y += (Math.random()*20).intValue() - 10;
} // move
I've never used Random before, so I'm not quite sure if I've understood it correctly, but the API states that "If two instances of Random are created with the same seed, and the same sequence of method calls is made for each, they will generate and return identical sequences of numbers." The Random() constructor uses the current time (millis) as a seed.
To me it seems that this means you should generate a new Random (with a different seed) for every particle.
Hope some of this has helped!
-Lisa
More information about the Programming
mailing list