[prog] Seeking and appropriate Java component

Lisa W. Eriksen lisaer at stud.ntnu.no
Wed May 5 12:13:43 EST 2004


On Wed, 5 May 2004 11:58:53 +0200
"Lisa W. Eriksen" <lisaer at stud.ntnu.no> wrote:

> 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 
> 
> 

Oh, but where are my manners? 
A short introduction might be in place: 
I'm a 24 y.o. girl, studying Comp Sci at NUST (NTNU) in Norway. I'm specializing in software development. Born in Norway, lived in different parts of Norway all my life. 
I follow the discussions on some of the linuxchix lists, but I've not actually participated in the time (a year or so) I've been subscribing to the lists. 

And: To get the int value of a double (there must be an easier way..? please enlighten me!) I usually do: 
Double d = new Double(the_double_value); 
int i = d.intValue(); 

-Lisa W. Eriksen 


More information about the Programming mailing list