I have painted a BufferedImage on a JPanel with the following code.
protected void paintComponent(Graphics g) {
if (image != null) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
double x = (getWidth() - scale * imageWidth) / 2;
double y = (getHeight() - scale * imageHeight) / 2;
AffineTransform at = AffineTransform.getTranslateInstance(x, y);
at.scale(scale, scale);
g2.drawRenderedImage(image, at);
}
}
How can I add a mouse click listener to that image? Additionally, I want to get the click coordinate of the image, not the JPanel.
Add a
MouseListener
to the pane as per normal.In the
mouseClicked
method check to see if thePoint
is within the rectangle of the image...