• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JPanel actionListener to determine click on diagonal drawLine

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a bunch of rectangles on a JPanel, and lines that connect some of the rectangles together. It represents a network map. I wrote the actionListener that handles the clicks on the rectangles. I simply map the x-y coordinates along with the height and width, then grab the points where the click occurred. Now I need a way to figure out if the click was on the lines that connect the rectangles. (Many of these lines are not vertical or horizontal, they make a diagonal across the screen. I am thinking I have to compute all the points the line hits by finding its slope of the line. Is there an easier way to do this? Any help would be greatly appreciated.
 
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Line2D and Rectangle2D apis have tantalizing methods that you can explore/experiment with for these kinds of things. For example, you could create a strategy based on one of the intersects methods with the help of a small Rectangle (instance member) in your event code. Center it over the coordinates of the mouse click and ask if it intersects any of the lines with intersectsLine(Line2D l) from the Rectangle2D api. Or you could ask the lines with intersects(Rectangle2D r) from the Line2D api.
reply
    Bookmark Topic Watch Topic
  • New Topic