File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes java graphic problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "java graphic problem" Watch "java graphic problem" New topic

java graphic problem

Morgan mor

Joined: Nov 03, 2004
Posts: 11
I want to write two random methods.
1: I want to draw a rectangle and a ellipse 100 times
and the random method should give them different
location and different size.

2: I want to fill them whit different color by help of the
random method
How can I write these methods?

public void paint(Graphics g)
x = (int)((Math.random()
jessie puls

Joined: Jul 08, 2002
Posts: 3
something like this maybe?

you would probably want to set some maximum value for the randoms generated on the size and position of the shapes.
[ November 24, 2004: Message edited by: jessie puls ]
Naani Mui

Joined: Oct 03, 2004
Posts: 12
Hope the following code would do the one you expected.

import java.applet.*;
import java.awt.*;
import java.util.*;
<Applet Code="Randoms.class" width="600" height="450">
public class Randoms extends Applet implements Runnable
Thread t;
int rc,gc,bc;
Color c;

public void init()
t = new Thread(this);

public void run()
for ( int i = 0; i<100; i++)
catch(InterruptedException e)

public void paint(Graphics g){
rc = (int)Math.floor(Math.random() * 256);
gc = (int)Math.floor(Math.random() * 256);
bc = (int)Math.floor(Math.random() * 256);
c = new Color(rc,gc,bc);
g.fillRect( (int)Math.floor(Math.random()*400),(int)Math.floor(Math.random()*250),(int)Math.floor(Math.random()*200),(int)Math.floor(Math.random()*200));
rc = (int)Math.floor(Math.random() * 256);
gc = (int)Math.floor(Math.random() * 256);
bc = (int)Math.floor(Math.random() * 256);
c = new Color(rc,gc,bc);
g.fillOval( (int)Math.floor(Math.random()*400),(int)Math.floor(Math.random()*250),(int)Math.floor(Math.random()*200),(int)Math.floor(Math.random()*200));

Good luck.
I agree. Here's the link:
subject: java graphic problem
Similar Threads
Guessing Game
Shuffling an Array (without Repetition...)
Random, random
Need help understanding math.random method.
Can I distinguish JButtons?