Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Graph with randomly moving dot

843807Oct 3 2004 — edited Oct 3 2004
I have a question. First off I am confused about Math.Random.


Will the code below generate a random number 0 to 3 right? Ok so if I wanted a dot on a graph to randlomly move north, east, south, or west a certain number of times (i.e. like in a loop of 100), how do I incorporate that code in with swtich statements to make them move? I might use case 0 for moving north, 1 for moving east and so on. So to move north I need to subtract y 30 pixels from y and for east add 30 pixels to x. My code for the graph and the dot are also below.

.....so I have to start with the dot in the middle of the grid and go from there. Can someone give me some pointers on how to get this started? I know what I need to use, I just don't understand how you know what to include where and where to start I guess. Thanks so much in advance!!

Oh so you know the reason I have to you x and y and know where its moving and how far is because in the end I have a code that distinguishes how far the dot has gone and so on. Thanks again! Oh an sorry for the other post with this information...it didn't come out where you could read everything. Sorry about that!!

//Random Number 0 - 3 
int i=3;
       (int)Math.round((Math.random()*i)); 


//draw a grid 10 by 10 with each square 30 pixes wide
	for ( int i = 10; i < 310; i += 30 )
		for ( int j = 10; j < 310; j+= 30)
			g.drawRect( i, j, 30, 30 );


//draw a dot that is 6 by 6 pixels that represents an ant
	g.setColor( Color.RED );
	g.fillOval( x-3, y-3, 6, 6 );
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 31 2004
Added on Oct 3 2004
9 comments
173 views