stumped
stumped

Reputation: 3293

How to draw a circle within a circle?

I'm trying to get a bunch of small circles that have varying shades of green to be drawn within a big circle to get a "bush" look, but I can't figure out how to get all the small circles within the shape of a big circle. I can only figure out how to get it within a rectangle.

public void paintComponent(Graphics g)
   {
      super.paintComponent(g);

      for(int i = 0; i < 1000; i++){
         int redV = (int) ((Math.random() * 100) + 27);
         g.setColor(new Color(red, red + 31, red - 15));

         int x = (int) ((Math.random() * 400) + 150);
         int y = (int) ((Math.random() * 500) + 200);

         g.fillOval(x, y, 50, 50);
      }
   }

Upvotes: 0

Views: 2276

Answers (3)

Jool
Jool

Reputation: 1785

Choose the point that should be the center of the big circle, and draw the big circle relative to that (e.g. using java.awt.geom.Ellipse2D).

You can then use the center of the big circle and its radius to position the other smaller circles relative to that also, inside the circumference.

Upvotes: 0

jbx
jbx

Reputation: 22148

I guess you have to do some geometry here, and verify whether the x and y coordinates generated randomly are within your circle. As you said, within a rectangle is easy (because you just check that x > left, x+50 < right, y > top, y+50 < bottom), however for a circle you have to use the equation of a circle and check that (x,y) and (x+50,y+50) are within it before actually doing the fillOval().

I think you have a simple way out by using the Java 2D Shape.contains(), which is implemented by Ellipse2D. So essentially you create an instance of Ellipse2D.Double or Ellipse2D.Float for the greater circle, and then just call contains() each time you generate the coordinates to check they are within it before drawing them.

Upvotes: 3

awolfe91
awolfe91

Reputation: 1647

I think you can just change the Color slightly, and increment/decrement x, y, width, and height slightly to get them to be within the older circle. The new oval should be painted over the old one.

Upvotes: 0

Related Questions