I have made an Ellipse with the help of java.awt.geom.Ellipse2D
Now, I want that whenever user clicks on that ellipse, an event is generated so that I can listen to that event and peform subsequent tasks based on the ellipse which generated that event.
I'm going to assume this is a question asking a way to listen to mouse clicks which are made on an ellipse drawn on some Swing component using
Graphics2D.draw
.The simple answer is, there is no way to generate mouse events from graphics drawn on a surface.
However, here's an alternative approach:
Ellipse2D
objects from which the ellipses were drawn from in aList
.MouseListener
on the Swing component where the user is to click on.MouseEvent
s generated from the mouse clicks, determine the location at which the mouse was clicked (usingMouseEvent.getPoint
), and check if the mouse click occurred in any of theEllipse2D
s contained in the aforementionedList
, using theEllipse2D.contains
method.Here is a simple example of an object drawing program that demonstrates click, drag and multiple selection. Also consider JGraph, which is a much more advanced library for graph visualization.
I don't think this is possible without lots of handcoded stuff (letting the canvas or whatever, listen to mouse events, and calculating yourself if the ellipse was clicked on).
If you want to do more like that consider scenegraph. With that the ellipse would be an object in its own right and you can register event listeners.
Edit as response to comment:
Scenegraph: https://scenegraph.dev.java.net/ google for more resources: scenegraph java And yes. Scenegraph ist part of the JavaFX stuff, but works nicely with pure Java (no FX)