?
Solved

Random Circles Problem

Posted on 2005-04-06
17
Medium Priority
?
443 Views
Last Modified: 2012-05-05
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
Comment
Question by:godfatherocky
  • 8
  • 7
  • 2
17 Comments
 
LVL 25

Expert Comment

by:InteractiveMind
ID: 13721726
Replace this line:

while (true)

with:

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

0
 
LVL 92

Expert Comment

by:objects
ID: 13721734
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
 
LVL 25

Expert Comment

by:InteractiveMind
ID: 13721739
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 92

Expert Comment

by:objects
ID: 13721754
Did you want the random circles to keep changing, or to just generate a number of random circles and display them?
0
 

Author Comment

by:godfatherocky
ID: 13722132
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
 
LVL 92

Expert Comment

by:objects
ID: 13722165
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
 
LVL 92

Expert Comment

by:objects
ID: 13722198
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
 

Author Comment

by:godfatherocky
ID: 13722223
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
 

Author Comment

by:godfatherocky
ID: 13722235
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
 
LVL 92

Expert Comment

by:objects
ID: 13722263
> Any ideas...:)

See my original comment, but instead of adding the circles in the thread add them in init().
0
 
LVL 92

Accepted Solution

by:
objects earned 2000 total points
ID: 13722295
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
 

Author Comment

by:godfatherocky
ID: 13722329
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
 

Author Comment

by:godfatherocky
ID: 13722332
okay sorry just read your next part of the reply.... let me try it out and I'LL LET YOU KNOW :)
0
 

Author Comment

by:godfatherocky
ID: 13722367
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
 
LVL 92

Expert Comment

by:objects
ID: 13722386
that should be circles.size()
0
 

Author Comment

by:godfatherocky
ID: 13722396
Awsome dude.... YOU ROCK!!
Thanks a million.

Rocky.
0
 
LVL 92

Expert Comment

by:objects
ID: 13722477
no worries :)
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month8 days, 16 hours left to enroll

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question