Link to home
Start Free TrialLog in
Avatar of godfatherocky
godfatherocky

asked on

Random Circles Problem

Hi,

The code below displays random circles continously in an endless loop flickering on screen.
The problem is that i don't wan't them to loop endlessly; I just want to display 6 - 10 circles of different sizes (random sizes), positioned randomly on screen.

So in summary
1. Random number of circles
2. of Random sizes
3. displayed at random positions on screen.

Please can som1 help
Thanx in advance.

Rocky.

The Code ------------------------------------------------------------------

import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;
public class DrawCircle extends Applet implements Runnable
{
      private Thread th;
      int radius = 0;
      int x=0, y=0;
      // double buffering
      private Image dbImage;
      private Graphics dbg;
      
      public void init() {
            setBackground (Color.black);
            x =0;
            y = 0;
            radius = 0;
      }
      
            public void start ()
      {
            th = new Thread(this);
            th.start ();
      }
      
      public void stop()
      {
            th.stop();
      }
      
      public void destroy()
      {
            th.stop();
      }

      
      public void run ()
      {
            Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
            while (true)
            {
                  
                  repaint();
                  
                  try
                  {
                        Thread.sleep(10);
                  }
                  catch (InterruptedException ex)
                  {
                        // do nothing
                  }
                  
                  Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
            }
      }
      
      
      public void update (Graphics g)
      {
            if (dbImage == null)
            {
                  dbImage = createImage (this.getSize().width, this.getSize().height);
                  dbg = dbImage.getGraphics ();
            }
            
            dbg.setColor (getBackground ());
            dbg.fillRect (0, 0, this.getSize().width, this.getSize().height);
            
            dbg.setColor (getForeground());
            paint (dbg);
            
            g.drawImage (dbImage, 0, 0, this);
            
      }
      
      public void paint(Graphics g) {
            
            for (int i=0; i < 15; i = i + 3) {
                  
                  x = (int) (Math.random()*300); // width of applet
                  y = (int) (Math.random()*300); // height
                  radius = (int) (Math.random()*10 + 15);
                  
                  drawCircle(g,x,y,radius);
                  
            }
      }
      
      private void drawCircle(Graphics g, int x, int y, int r) {
            g.setColor(Color.red);
            g.fillOval(x-r, y-r, 2*r, 2*r);
      }
      
}
Avatar of InteractiveMind
InteractiveMind
Flag of United Kingdom of Great Britain and Northern Ireland image

Replace this line:

while (true)

with:

for ( int i = 0; i < 10; i++ )

Avatar of Mick Barry
I'd suggest changing your approach a little and keeping a list of circles to display as member variables, and in your paint() method paint those circles.
Then in your thread add circles to that list.
Or, replace:

while (true)

with these lines:

Random rand = new Random();
int a = rand.nextInt(4+1);
a += 6;

for ( int i = 0; i < a; i++ )


That should then display a random number of circles (between 6 and 10)
Did you want the random circles to keep changing, or to just generate a number of random circles and display them?
Avatar of godfatherocky
godfatherocky

ASKER

Thanks for the quick replies guyz.. really appreciate your time.

InteractiveMind - Very clever thinking I must say but both your solutions somewhat just loops random circles and stop.

objects - I just wanted to generate a number of random circles and display them!

Just a note... I am aware that if I get rid of the "double buffereing" and "threads" in the program then the program will generate random circles and display them without looping... but I intend to later move these random cicles across the screen hence the class "implements runnable" therefore the "threads" are neccessary.

Hope this helps. Thanks for your replies again.

Rocky.
How you are doing it at the moment will result in a new set of random circles being generate every time the applet is repainted.
And if you're going to want to move the circles you'll need to keep track of their details as member variables anyway.
objects - Thanks for your reply. Yes, your right... how I am doing it at the moment will result in a new set of random circles being generate every time the applet is repainted....but that is the problem. I want to be able to paint random circles on screen whilst still using "implements runnable". The "implements runnable" is neccessary as later I intend to move the circles across the screen which will require repainting each time the circle moves its position.

Any ideas...:)

Thanks in advance.

Rock


objects - the answer to your next query - "And if you're going to want to move the circles you'll need to keep track of their details as member variables anyway."

I don't understand completely.. pls cna you elaborate

cheers,
> Any ideas...:)

See my original comment, but instead of adding the circles in the thread add them in init().
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Okay, from what I understood .. I took the following code and put it in the init() method

 for (int i=0; i < 15; i = i + 3) {
               x = (int) (Math.random()*300); // width of applet
               y = (int) (Math.random()*300); // height
               radius = (int) (Math.random()*10 + 15);
          }


............. and left this piece of code in the paint() method

drawCircle(g,x,y,radius);

but... this gives me only 1 random circles which coincidently I alredy know how to do.... Any ideas on how to genrate more circles

Cheers.

okay sorry just read your next part of the reply.... let me try it out and I'LL LET YOU KNOW :)
Right then... i get the following error...

java:24: operator < cannot be applied to int,java.util.Vector

For this line: for (int i=0; i < circles; i++)
that should be circles.size()
Awsome dude.... YOU ROCK!!
Thanks a million.

Rocky.
no worries :)