Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Issues with selecting a drawn circle on graphics

  1. #1
    Junior Member
    Join Date
    Jun 2011
    Posts
    11
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Issues with selecting a drawn circle on graphics

    In my program I want to first draw two circles then be able to connect them by drawing a line between them. If the line is in each of the circles, the line will draw between them. If not, the line doesn't get drawn. How would I go about this? Would I get the graphics location the circle occupies by calculating the area of the circle? If so, how? Or is there another way?

    I've got the code that draws the circles but not sure if it would help find a solution. Let me know if so though and I'll post it.

    Thanks in advance.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Issues with selecting a drawn circle on graphics

    Can you explain what you are trying to do?
    If you are drawing two circles in a paint method and then you want to draw a line between the two circles, that should be very easy.
    Can you explain why drawing the line between the two circles is a problem?
    If the line is in each of the circles, the line will draw between them. If not, the line doesn't get drawn.
    If you are drawing the lines, how does this happen?
    Have you taken a piece of paper and drawn where the x and y and width and height of the circles are?
    Then compute where on that drawing you want to draw you line. From that you should be able to compute the x and y values to draw the line.

  3. The Following User Says Thank You to Norm For This Useful Post:

    Diplo (July 31st, 2011)

  4. #3
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Issues with selecting a drawn circle on graphics

    Why can you not just draw a line between the centre-points of the two circles?

  5. The Following User Says Thank You to dlorde For This Useful Post:

    Diplo (July 31st, 2011)

  6. #4
    Junior Member
    Join Date
    Jun 2011
    Posts
    11
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Issues with selecting a drawn circle on graphics

    The program is for a shortest path algorithm. The user can draw the nodes (circles) and connect them with vertexs (lines), then calculate the shortest path between them. If they click somewhere on the screen that's a circle, drag across and let go on another circle, the vertex will draw. How do I check whether the place they click is a circle though?

    The x and y values of the circle's centre will be stored, do I then calculate what other x and y values the circle occupies on the graphics? Hope I'm making sense!

  7. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Issues with selecting a drawn circle on graphics

    How do I check whether the place they click is a circle though?
    You have two x,y points: the mouse click and the center of the circle.
    You also have the radius of the circle. Test if the distance between those two points is less than the radius of the circle.
    There was an old Greek guy that had a theorem about right triangles that you could use to compute the distance. Something about squares of the sides of triangle.

  8. The Following User Says Thank You to Norm For This Useful Post:

    Diplo (July 31st, 2011)

  9. #6
    Junior Member
    Join Date
    Jun 2011
    Posts
    11
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Issues with selecting a drawn circle on graphics

    OK thanks Norm, thought there might be a quicker way like getPixels when it's being drawn. Will use your suggestion though.

Similar Threads

  1. java (circle)
    By coder.freak in forum Member Introductions
    Replies: 3
    Last Post: March 26th, 2011, 12:01 PM
  2. Selecting a specific list
    By sapzero in forum Java Theory & Questions
    Replies: 1
    Last Post: January 23rd, 2011, 12:06 PM
  3. Changing how a Window is drawn.
    By javapenguin in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 8th, 2011, 09:16 PM
  4. selecting even numbers from an array
    By Tate in forum What's Wrong With My Code?
    Replies: 7
    Last Post: November 29th, 2010, 08:03 AM
  5. Moving circle problem help
    By nobleisthyname in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 3rd, 2010, 11:43 AM