views:

61

answers:

3

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.

+1  A: 

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)

Jens Schauder
could you please provide a link to scenegraph. I haven't heard it before. Can I draw ellipse from that on a swing GUI.
Yatendra Goel
+1  A: 

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:

  1. Store the Ellipse2D objects from which the ellipses were drawn from in a List.
  2. Register a MouseListener on the Swing component where the user is to click on.
  3. From the MouseEvents generated from the mouse clicks, determine the location at which the mouse was clicked (using MouseEvent.getPoint), and check if the mouse click occurred in any of the Ellipse2Ds contained in the aforementioned List, using the Ellipse2D.contains method.
coobird
+1  A: 

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.

trashgod