0

I basically want to make a line clickable, so I can remove the drawn line by right-clicking on it. Is there a way to draw lines like that?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
meliddu
  • 23
  • 5
  • 2
    Write a method to detect if a mouse click is within a certain distance of a line. Call that method with the line's coordinates and the x,y position of the right-click. – NormR May 30 '20 at 16:46
  • 1
    Or wrap the outline of a Line2D with a Path2D and use the path's `.contains(...)` method. Don't use a Component for this though. – DontKnowMuchBut Getting Better May 30 '20 at 16:53
  • Thanks for replies; @NormR that sounds a lot of work because I have also nodes on a panel, lines are drawn between them. And for second commenter, thanks for advice but I don't know how to do that. I'm new on here sorry I couldn't mention both of you – meliddu May 30 '20 at 16:56
  • *"I couldn't mention both of you"* That's by design, to encourage commenting to one person per comment. Make a second comment to notify @DontKnowMuchButGettingBetter (who suggested the way I'd do it). See also [this answer](https://stackoverflow.com/a/14575043/418556) to [Collision detection with complex shapes](https://stackoverflow.com/q/14574045/418556). It uses a `Shape` rather than a `Point` (which is simpler), but may give some hints. BTW is a 'node' a component, custom drawn..? – Andrew Thompson May 31 '20 at 04:10
  • Thanks for informing @AndrewThompson, my nodes are small jpanels on a white board that can draggable by user – meliddu May 31 '20 at 06:43
  • Make a [clickable line](https://stackoverflow.com/a/61835785/3992939) – c0der May 31 '20 at 13:30
  • @c0der thanks for the reply, so I can create a Line class which is a subclass of JComponent, and by overriding paintComponent function we specify it's shape, sizes and color. Did I get right? If so, am I going to be able to show them up by just calling panel.add(line) and panel.repaint() functions? – meliddu May 31 '20 at 14:26
  • Yes. It is a component and you can add it like any other component. Run a simple [test](https://repl.it/repls/CoolKhakiBots). – c0der May 31 '20 at 16:31
  • @c0der Got it, thanks a lot. May I hit you up back if I struggle in this again? – meliddu May 31 '20 at 18:27
  • Sure. If you have other questions post them and leave me a message – c0der Jun 01 '20 at 03:28
  • @c0der Hello again. Lines do show on jframe but I couldn't make them show up on a jpanel which covers the entire jframe. Here is the post: [link](https://stackoverflow.com/questions/62128874/custom-jcomponent-a-line-doesnt-show-up-on-jpanel) – meliddu Jun 01 '20 at 09:07

0 Answers0