[prog] Seeking and appropriate Java component
Meredydd
meredydd at everybuddy.com
Wed May 5 11:30:04 EST 2004
Uhhm...both AWT and Swing Labels allow you to change their contents
fairly easily, with the setText() method.
You might also want to look at the way you're running each particle in a
single thread - threads have nontrivial overhead, especially when you
do a lot of context-switching. In addition, if you want to keep your
timings consistent, you're going to have to do a lot of synchronisation
between your various threads, because there's no guarantee that they'll
all run at the same rate. Is there a particular reason you can't just
have the one thread, and do all the particles with that?
As for random - traditionall, random number generators produce numbers
in the range 0 <= x < maxvalue, and java.lang.Random is no exception.
rng.nextInt(10) produces numbers in the range 0-9, so rng.nextInt(10)-5
produces -5 to +4, which accounts for your bias. Try rng.nextInt(11)-5
Meredydd
On Thursday 06 May 2004 10:11, Sue Stones 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
More information about the Programming
mailing list