# Using GUI to create a circle

Rhyeca Riley

Greenhorn

Posts: 20

1

posted 4 months ago

Hi everyone was wondering if I could get some input on how to get this code to do what I want. For this assignment we are supposed to have the circle start at this first mouse click and then be able to drag and release on how big we want the circle. My code creates a circle(oval) but it does not start in the center like it should. Should I not be using point1.x, point1.y? (The points are not given but are based off of mouse click)

Thank you for all the help as usual! Finding this place has really helped me understand more about programming, and allows me to not get so frustrated on my own!

Thank you for all the help as usual! Finding this place has really helped me understand more about programming, and allows me to not get so frustrated on my own!

posted 4 months ago

- 1

Nice code formatting and indentation, have a cow for that! And welcome to the Ranch.

Rhyeca Riley

Greenhorn

Posts: 20

1

posted 4 months ago

- 1

See the doc for drawOval()

Draws the outline of an oval. The result is a circle or ellipse that fits within the rectangle specified by the x, y, width, and height arguments.

Where x,y is the upper left corner of the bounding rectangle.

You'll have to take your point1 and point2 and compute the bounding rectangle for the circle with the center at point1. Then use the rectangle when you call drawOval().

Draws the outline of an oval. The result is a circle or ellipse that fits within the rectangle specified by the x, y, width, and height arguments.

Where x,y is the upper left corner of the bounding rectangle.

You'll have to take your point1 and point2 and compute the bounding rectangle for the circle with the center at point1. Then use the rectangle when you call drawOval().

Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.

Rhyeca Riley

Greenhorn

Posts: 20

1

posted 4 months ago

so just making sure I am understanding what you are saying, is I need to first do a separate method for rectangle and then input that into drawOval?

Carey Brown wrote:See the doc for drawOval()

Draws the outline of an oval. The result is a circle or ellipse that fits within the rectangle specified by the x, y, width, and height arguments.

Where x,y is the upper left corner of the bounding rectangle.

You'll have to take your point1 and point2 and compute the bounding rectangle for the circle with the center at point1. Then use the rectangle when you call drawOval().

so just making sure I am understanding what you are saying, is I need to first do a separate method for rectangle and then input that into drawOval?

Rob Camick

Ranch Hand

Posts: 2714

10

Fred Kleinschmidt

Bartender

Posts: 493

3

posted 4 months ago

- 1

g.drawOval( x1, y1, x2, y2 ) draws an oval that fits inside the rectangle with diagonal corners at (x1,y1) and (x2,y2).

So for your circle you need to calculate the box that will enclose your circle, where the center of the box (and the circle) is at point1, and the radius of the circle is the distance from point1 to point2 (thus the width and height of the box are twice the radius of the circle).

So for your circle you need to calculate the box that will enclose your circle, where the center of the box (and the circle) is at point1, and the radius of the circle is the distance from point1 to point2 (thus the width and height of the box are twice the radius of the circle).

posted 4 months ago
Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.

- 1

A method you might find useful:

Math.hypot(x,y)

Math.hypot(x,y)

posted 4 months ago

Note that the method is drawOval(x,y,width,height), not as Fred posted.
Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.

- 1

Fred Kleinschmidt wrote:g.drawOval( x1, y1, x2, y2 ) draws an oval that fits inside the rectangle with diagonal corners at (x1,y1) and (x2,y2).

So for your circle you need to calculate the box that will enclose your circle, where the center of the box (and the circle) is at point1, and the radius of the circle is the distance from point1 to point2 (thus the width and height of the box are twice the radius of the circle).

Note that the method is drawOval(x,y,width,height), not as Fred posted.

Fred Kleinschmidt

Bartender

Posts: 493

3

Rhyeca Riley

Greenhorn

Posts: 20

1

posted 4 months ago
Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.

- 1

I think "radius" describes the intent better than "distance", but that's just my choice.

Still think a quick look at the API doc for hypot() would be helpful.

Now, using radius you should be able to compute the upper-left x,y coordinates relative to point1, and the width and height.

Edit: Don't use pow() to square something, just multiply it by itself. This is more efficient.

Still think a quick look at the API doc for hypot() would be helpful.

Now, using radius you should be able to compute the upper-left x,y coordinates relative to point1, and the width and height.

Edit: Don't use pow() to square something, just multiply it by itself. This is more efficient.

It is sorta covered in the JavaRanch Style Guide. |