Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2006

Tip: Looking for answers? Try searching our database.

Questions about how to draw some random squares

Thread view: 
digibooster@gmail.com - 19 Oct 2006 02:22 GMT
Hi, I am currently working on a program that draw buildings with some
random windows.

I figured out how to draw the buildings by set up the drawRect(x, y,
width, height), but I still have questions about how to randomize
windows (square shape) within those buildings. Do I need to setup
ranges for the windows? What is the method for setup random squares?
Any suggestion or comments are welcomed. Thank you
Luc The Perverse - 19 Oct 2006 05:02 GMT
> Hi, I am currently working on a program that draw buildings with some
> random windows.
[quoted text clipped - 4 lines]
> ranges for the windows? What is the method for setup random squares?
> Any suggestion or comments are welcomed. Thank you

Your question doesn't make sense

If you want a random square, choose a random width, and a random upper left
corner position.   If you want it to "look like" something - then you need
some kind of limit on the randomness.

--
LTP

:)
digibooster@gmail.com - 19 Oct 2006 07:54 GMT
Maybe I didn't make myself clear. Here is the thing, I need to design a
class that represents couple buildings. Within those buildings, I need
to put some random lightened windows (yellow squares). The random part
really got me.

> > Hi, I am currently working on a program that draw buildings with some
> > random windows.
[quoted text clipped - 15 lines]
>
> :)
Andrew Thompson - 19 Oct 2006 08:13 GMT
> Maybe I didn't make myself clear.

The original explanation managed to confuse at least two
people, lucky for you that one of them bothered to express
that, huh?   ;-)

>...Here is the thing, I need to design a
> class that represents couple buildings. Within those buildings, I need
> to put some random lightened windows (yellow squares).

That is much better.

>...The random part really got me.

You need either an instance of the Random class, or
Math.random(), to produce random numbers.
Math.random() is the easier to use..

 boolean darkWindow = ( Math.random()>0.7 ) // gone to bed!

HTH

Andrew T.
digibooster@gmail.com - 19 Oct 2006 08:42 GMT
> You need either an instance of the Random class, or
> Math.random(), to produce random numbers.
> Math.random() is the easier to use..
>
>   boolean darkWindow = ( Math.random()>0.7 ) // gone to bed!

Could you explain it in detail a little bit. I am still confused.
Andrew Thompson - 19 Oct 2006 08:58 GMT
> > You need either an instance of the Random class, or
> > Math.random(), to produce random numbers.
[quoted text clipped - 3 lines]
>
> Could you explain it in detail ..

Not here.  This group is for people that can read javadocs
and ask specific questions with code examples(*)..

>...a little bit. I am still confused.

Try comp.lang.java.help for more detailed help.

* That is not the charter, merely my personal view.
Anybody that disagrees with my personal view can feel
free to answer your question (here) themselves.

Andrew T.
digibooster@gmail.com - 19 Oct 2006 09:02 GMT
Thank you. I already had the feelings that I don't belong here, at
least for now. :D Appreciate your time though
> > > You need either an instance of the Random class, or
> > > Math.random(), to produce random numbers.
[quoted text clipped - 16 lines]
>
> Andrew T.
Luc The Perverse - 19 Oct 2006 09:35 GMT
> Try comp.lang.java.help for more detailed help.
>
> * That is not the charter, merely my personal view.
> Anybody that disagrees with my personal view can feel
> free to answer your question (here) themselves.

I think your view is generally accepted

--
LTP

:)
Luc The Perverse - 19 Oct 2006 09:32 GMT
> Maybe I didn't make myself clear. Here is the thing, I need to design a
> class that represents couple buildings. Within those buildings, I need
> to put some random lightened windows (yellow squares). The random part
> really got me.

Thank you for quoting, but please do not top post (notice how I put my reply
below your quoted text).

Ah - so you are drawing buildings, presumably with a number of windows, and
you want to make some of the windows appear to have a light on inside, and
you want the selection of which windows are lighted to be random?   (If this
is not the case, then none of the rest of the post will apply)

That is easy - but you are not randomly placing boxes (for the placement of
the boxes is quite uniform), you are randomly selecting a color for the
windows, which are boxes which already exist, even if they are not drawn
yet.

Select which color you want the windows to be, and select what percentage
dark, and what percentage light you want to have.

Now - do you want it to be strict, or do you want it to be loose?  True
random would mean, even if 20% of the windows, on average, were supposed to
be light; it would be theoretically possible that all the windows would be
light at once, even if ridiculously unlikely.

To do that is easy (pseudocode)
   if(rand()>0.20)
       color is dark
   else
       color is yellow

Where rand is a PRNG which returns numbers betwen 0 and 1.  (I think you can
use java.util.Random.nextDouble) Now if you want exactly a percentage, or
number of the windows to be lighted at once, you will have to introduce some
kind of memory.  Select windows randomly from a pool, and remove from the
pool as you select them.  The chosen windows will be light, the rest dark.
You may be able to do a similar thing overwriting already drawn windows.
For this I would suggest a PRNG which returns between 0 and some very large
number, and just mod by the number of items you have left in the list, this
will give you a result between 0 and (n-1) where n is the number of items
[left] in the list.

Here is some [pseudo] code (mostly java)

Window[] allMyWindows = new Window[numWins];
int count = numWins;
               //initialize windows to something meaningful
int numColored = count * 10 / 50;   //20 percent
Window[] colored = new Window[numColored];
Window[] uncolored = new Window[count - numColored];
for(int i = 0;i<numColored;i++){
   int n = intRand() %  count;
   count--;
   colored[i] = allMyWindows [n];
               //swap
   allMyWindows [n] = allMyWindows [count];
   allMyWindows [count] = colored[i];
}
for(int i = 0;i<count;i++)
   uncolored[i] = allMyWindows [i];
for(Window c : colored)
   DrawWindow(c, Yellow);
for(Window c : uncolored)
   DrawWindow(c, Grey);

Where intRand is a PRNG as described before which returns an integer between
0 and X (where X is quite large)   I believe you can use
java.util.Random.nextInt() for something like this

I'm sure there are other ways, but to me these are the two most
staightforward ways to go about it.

Note:  There are many powerful tools in java.   You may be able to do
everything I did using collections in very few lines of code.  You could
probably shuffle a collection of windows and then just color the first X
number of windows, and then the rest are grey

--
LTP

:)
Mark Jeffcoat - 19 Oct 2006 10:08 GMT
> Maybe I didn't make myself clear. Here is the thing, I need to design a
> class that represents couple buildings. Within those buildings, I need
> to put some random lightened windows (yellow squares). The random part
> really got me.

Are you saying that you wish someone would reply with a pointer
to java.util.Random? Or were you hoping for java.lang.Math.random()?

Because the only other answer I can think of is at
    http://catb.org/~esr/faqs/smart-questions.html 

Signature

Mark Jeffcoat
Austin, TX



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.