First, I came to Java as a programmer on a site. In JavaScript, all you need to do to add a mousemove, mouseover, or click event is the addEventListener function. From my limited experience with Java, you cannot just implement the MouseListener interface from any object.
Basically, I'm still a JPanel that draws some shapes (a CustomShape object with a paint method) that have x / y / width / height values. I want to add some type of mouse listener to the shape object so that I can fire move / roll / click events for the shape. Just implementing the MouseListener interface for a CustomShape object does not work (apparently for obvious reasons). I looked at how to create custom event listeners, but it seems like creating a custom mouse listener is possible.
In the end, I resorted to adding a mouse listener to the JPanel, and then iterating over all the objects in the form. If a βlistenerβ was added to the shape object, and the mouse coordinates confirmed the mouse event, he fired the method. Initially, it was great, but as the application got more developed, it really got confused. Also, I would never be able to copy form objects / interfaces to another application without copying the code pool.
As a simple illustration: (the actual code is quite large)
Interface CustomShape{ int width, height, x, y; void paint(Graphics g); } public class StarShape implements CustomShape{ int width, height, x, y; public StarShape(){ width = 100; height = 100; x = 50; y = 50; } void paint(Graphics g){ g.setColor(Color.black); g.draw(new Rectangle(x,y,width,height)); } } public class Main extends JPanel{ StarShape check = new StarShape(); public Main(){ } @Override public void paintComponent(Graphics g){ super.paintComponent(g); check.paint(g); } }
So, I was wondering if there is a clean way to implement some type of mouse listener for a "drawn" form.