8 Replies - 188 Views - Last Post: 05 February 2012 - 07:46 PM Rate Topic: -----

Topic Sponsor:

#1 godog1994  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 51
  • Joined: 17-October 11

constructing a target(circles) with gui.

Posted 05 February 2012 - 12:37 AM

I'm having trouble constructing a target using the gui. Right now I have something like this second picture.Attached ImageAttached Image (mine is the one with just 2 circles). I'm trying to achieve what the other looks like.
public void draw(Graphics g)
   {
   //largest
   int x = centerX - radius;
   int y = centerY - radius;
   int width = 2 * radius;
   int height = 2 * radius;
   Color myColor = new Color(0, 0, 255);
   g.setColor(myColor);
   g.fillOval(x, y, width, height);
   
   //2nd
   int secondX = centerX - radius/2;
   int secondY = centerY - radius/2;
   int secondWidth = radius ;
   int secondHeight = radius;
   g.setColor(Color.WHITE);
   g.fillOval(secondX, secondY, secondWidth, secondHeight);
I know I'm messing up in the secondWidth and height, but I'm confused about how to fix it. How would I make it so that the size of smaller circles increments down like the picture?

Is This A Good Question/Topic? 0
  • +

Replies To: constructing a target(circles) with gui.

#2 Mylo  Icon User is offline

  • D.I.C Regular

Reputation: 135
  • View blog
  • Posts: 493
  • Joined: 11-October 11

Re: constructing a target(circles) with gui.

Posted 05 February 2012 - 12:49 AM

Well it appears as if you want 4 circles, in this case I believe you can just increase the radius of the first circle to make it the same size as what you want. Then have sub circles have slightly less radius.

Only the radius has to change.

Or did you mean something else?

This post has been edited by Mylo: 05 February 2012 - 12:50 AM

Was This Post Helpful? 0
  • +
  • -

#3 jon.kiparsky  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1783
  • View blog
  • Posts: 3,359
  • Joined: 19-March 11

Re: constructing a target(circles) with gui.

Posted 05 February 2012 - 12:54 AM

Do the math by hand.

Suppose you want a set of circles centered on 500,500
You want ten circles, the first should be 10px in diameter, each of the surrounding ones should be 10px larger in diameter.

What are the parameters for the first (innermost) circle? The second? Work out the values for the successive circles, and draw them in a program using the literal values you've worked out. Add each circle as you work it out and check your work.

Working out values by hand is a great stimulus to finding rules: it's tedious and informative, so the activity both teaches and provides incentive to learn.
Was This Post Helpful? 1
  • +
  • -

#4 smohd  Icon User is offline

  • Critical Section
  • member icon



Reputation: 1644
  • View blog
  • Posts: 4,126
  • Joined: 14-March 10

Re: constructing a target(circles) with gui.

Posted 05 February 2012 - 01:01 AM

Other things to note in your code:
- <mistake>

- No need to to calculate the width and height, circle is a special oval with equal a and b. So one of them is enough for both diameters:
   int width = 2 * radius;
   int height = 2 * radius; //this is the same as the above one

Just pass the same thing for both parameters:
g.fillOval(x, y, width, width);

This post has been edited by smohd: 05 February 2012 - 01:05 AM

Was This Post Helpful? 2
  • +
  • -

#5 godog1994  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 51
  • Joined: 17-October 11

Re: constructing a target(circles) with gui.

Posted 05 February 2012 - 11:29 AM

View Postjon.kiparsky, on 05 February 2012 - 12:54 AM, said:

Do the math by hand.

Suppose you want a set of circles centered on 500,500
You want ten circles, the first should be 10px in diameter, each of the surrounding ones should be 10px larger in diameter.

What are the parameters for the first (innermost) circle? The second? Work out the values for the successive circles, and draw them in a program using the literal values you've worked out. Add each circle as you work it out and check your work.

Working out values by hand is a great stimulus to finding rules: it's tedious and informative, so the activity both teaches and provides incentive to learn.

Would you mind giving an example of what you mean? I understand but I'm just confused since it gets placed by the top left corner.
Was This Post Helpful? 0
  • +
  • -

#6 smohd  Icon User is offline

  • Critical Section
  • member icon



Reputation: 1644
  • View blog
  • Posts: 4,126
  • Joined: 14-March 10

Re: constructing a target(circles) with gui.

Posted 05 February 2012 - 11:33 AM

It is just simple, for example, if you want your outer circle to be of diameter 10, and every inner one to be decreased in diameter by 2, then just that math and you can use a loop.
Was This Post Helpful? 0
  • +
  • -

#7 jon.kiparsky  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1783
  • View blog
  • Posts: 3,359
  • Joined: 19-March 11

Re: constructing a target(circles) with gui.

Posted 05 February 2012 - 11:40 AM

Quote

Would you mind giving an example of what you mean?


Sure. A circle is traditionally defined by center and radius, right? Call that (x,y,r). However, swing locates elements by top left corner, as you say, so if you have a circle defined by (x,y,r) you need to translate it to drawOval's way of thinking, which is (x',y', width,height). Width and height are easy: they're just r. x' and y' are a little more complicated: they're x and y, translated from center to top left. However, that transformation is not difficult, since the top of the circle is going to be one radius away from the center, and the left edge of the circle is likewise one radius away from the center. If you keep in mind that the origin of this grid is at the top left (not the bottom left), it's obvious that both of these are subtractions, so x'=x-r and y'=y-r.

So a circle defined by (500,500,5) in Swing's terms is at (495,495,5,5)

Is that more clear?

This post has been edited by jon.kiparsky: 05 February 2012 - 11:40 AM

Was This Post Helpful? 2
  • +
  • -

#8 godog1994  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 51
  • Joined: 17-October 11

Re: constructing a target(circles) with gui.

Posted 05 February 2012 - 07:24 PM

View Postjon.kiparsky, on 05 February 2012 - 11:40 AM, said:

Quote

Would you mind giving an example of what you mean?


Sure. A circle is traditionally defined by center and radius, right? Call that (x,y,r). However, swing locates elements by top left corner, as you say, so if you have a circle defined by (x,y,r) you need to translate it to drawOval's way of thinking, which is (x',y', width,height). Width and height are easy: they're just r. x' and y' are a little more complicated: they're x and y, translated from center to top left. However, that transformation is not difficult, since the top of the circle is going to be one radius away from the center, and the left edge of the circle is likewise one radius away from the center. If you keep in mind that the origin of this grid is at the top left (not the bottom left), it's obvious that both of these are subtractions, so x'=x-r and y'=y-r.

So a circle defined by (500,500,5) in Swing's terms is at (495,495,5,5)

Is that more clear?

Thanks that really helped me understand it much better.
Was This Post Helpful? 0
  • +
  • -

#9 jon.kiparsky  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1783
  • View blog
  • Posts: 3,359
  • Joined: 19-March 11

Re: constructing a target(circles) with gui.

Posted 05 February 2012 - 07:46 PM

Glad to help.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1