File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes JPanel actionListener to determine click on  diagonal drawLine Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JPanel actionListener to determine click on  diagonal drawLine" Watch "JPanel actionListener to determine click on  diagonal drawLine" New topic
Author

JPanel actionListener to determine click on diagonal drawLine

Chris Carney
Greenhorn

Joined: Sep 28, 2004
Posts: 7
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.
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JPanel actionListener to determine click on diagonal drawLine
 
Similar Threads
Java 2D Shape classes (design question)
java swings with 2D graphics
save drawing
How to reset jpanel...
creating gifs