• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Using GUI to create a circle

 
Rhyeca Riley
Greenhorn
Posts: 20
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Liutauras Vilda
Bartender
Pie
Posts: 2809
112
BSD VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nice code formatting and indentation, have a cow for that! And welcome to the Ranch.
 
Rhyeca Riley
Greenhorn
Posts: 20
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:Nice code formatting and indentation, have a cow for that! And welcome to the Ranch.


woohooo my first cow!
 
Carey Brown
Bartender
Posts: 1965
24
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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().
 
Carey Brown
Bartender
Posts: 1965
24
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hint: Think radius.
 
Rhyeca Riley
Greenhorn
Posts: 20
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 2675
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read the drawOval(...) API. It does not take two points as the method parameters.

It takes a starting point and the width/height of the oval. So you need to calculate the width/height using the two Points.
 
Fred Kleinschmidt
Bartender
Posts: 417
3
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Carey Brown
Bartender
Posts: 1965
24
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A method you might find useful:
Math.hypot(x,y)
 
Carey Brown
Bartender
Posts: 1965
24
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 417
3
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course you are right - I just repeated the OP's code without referring to the documentation. Always a bad idea...
 
Rhyeca Riley
Greenhorn
Posts: 20
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

this is what I have added, when I try to write

it does not work. I understand why I need to put radius but I am just having trouble "seeing" it right now. I will continue to work on it and appreciate all the help!
 
Carey Brown
Bartender
Posts: 1965
24
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rhyeca Riley
Greenhorn
Posts: 20
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you everyone who pushed me in the right directions in how to write this program. All the help is really appreciated!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic