I have a Java application containing five buttons:
i) Square ii) Rectangle iii) Circle iv) Triangle iv) Clear
Clicking on any of the four buttons will result in the respective shape being drawn on the drawing canvas. This is the code used to draw the shape:
The drawing canvas class inherits from JPanel. The shapes which are clicked by the user and are drawn to the drawing canvas are stored in an ArrayList called Shapes.
Now in the drawing canvas class I have a number of mouse listeners. In the MousePressed Event, I want to detect what was clicked.
This is what I used for the buttons:
The naming of the buttons was achieved using the setName method of the JButton class.
However, the Graphics class does not have such a method. How can I detect that one of the shapes has been clicked on the canvas please (once it has been drawn)?
To keep track of what was drawn, you should keep track of the coordinates of the shapes being drawn into the canvas in a Collection such as an ArrayList and do a check based on this array to the mouse location of the click.