• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 448
  • Last Modified:

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);
      }
      
}
0
godfatherocky
Asked:
godfatherocky
  • 8
  • 7
  • 2
1 Solution
 
InteractiveMindCommented:
Replace this line:

while (true)

with:

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

0
 
objectsCommented:
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.
0
 
InteractiveMindCommented:
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)
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
objectsCommented:
Did you want the random circles to keep changing, or to just generate a number of random circles and display them?
0
 
godfatherockyAuthor Commented:
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.
0
 
objectsCommented:
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.
0
 
objectsCommented:
And if you're going to want to move the circles you'll need to keep track of their details as member variables anyway.
0
 
godfatherockyAuthor Commented:
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


0
 
godfatherockyAuthor Commented:
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,
0
 
objectsCommented:
> Any ideas...:)

See my original comment, but instead of adding the circles in the thread add them in init().
0
 
objectsCommented:
something like:

public class DrawCircle extends Applet implements Runnable
{
   private Vector circles = new Vector();

    ...

     public void init()
     {
          setBackground (Color.black);
          for (int i=0; i < ncircles; i++)
          {
               
               int x = (int) (Math.random()*300); // width of applet
               int y = (int) (Math.random()*300); // height
               int radius = (int) (Math.random()*10 + 15);
               circles.add(new int[] { x, y, radius});
          }
     }

     ...

     public void paint(Graphics g)
     {
        for (int i=0; i<circles.size(); i++)
        {
           int[] circle = (int[]) circles.get(i);
           drawCircle(g, circle[0], circle[1], circle[2]);
        }
     }

     ...
0
 
godfatherockyAuthor Commented:
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.

0
 
godfatherockyAuthor Commented:
okay sorry just read your next part of the reply.... let me try it out and I'LL LET YOU KNOW :)
0
 
godfatherockyAuthor Commented:
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++)
0
 
objectsCommented:
that should be circles.size()
0
 
godfatherockyAuthor Commented:
Awsome dude.... YOU ROCK!!
Thanks a million.

Rocky.
0
 
objectsCommented:
no worries :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 8
  • 7
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now