I found which image I clicked?

Scenario: I have one JFrame and a JPanel added to this JFrame . on this panel, I drew 3 images using:

 public void paint(graphics g) { g.drawImage(img1,100,100,null); g.drawImage(img2,200,200,null); g.drawImage(img3,300,300,null); } 

I implemented a MouseListener interface for listening to clicks. Now I want that whenever I click on any of these images, my output (on the command line using System.out.println(); ) should be the object of the image that I clicked?

Please explain to me if this is possible and how?

+4
source share
2 answers

Well, first of all you draw all your images in (0,0), are you sure you want to do this? If you do this, you can click the point that belongs to all of your images (es, 0,0).

By the way, inside your MouseListener you have this method:

 public void mouseClicked(MouseEvent e) { Point point = e.getPoint(); } 

specify the coordinate of your click relative to the component that you are listening to. So you just need to check if the point you are clicking is inside the image area. You can do the following:

 Rectangle imageBounds = new Rectangle(x,y,image_width, image_height); if (imageBounds.contains(point)){ //point is inside given image } 

where x, y is the coordinate in which you draw your image using the drawImage method (0,0 in your case) and image_width, image_height is the size of your image.

EDIT:

There is an alternative to the solution discussed above. As suggested by Hovercraft Full Of Eels, you can do the following:

  • create a jlabel for every image you want to display
  • use JLabel setIcon () to specify the image that will be displayed on each label.
  • Add your tags to your JPanel
  • add a mouse listener to each jlabel

This approach is of great benefit: you do not have to worry about mouse coordinates, because each JLabel has a relative mouse listener. The only thing you should consider is the following:

using the Component instead of drawing your images, you cannot absolutize them, but you must use the appropriate LayoutManager to build your JLabel.

+7
source

This is what I did for a simple card game I was working on:

  • Optional: create an object to represent your clickable image. He should know this (x, y), width / height and how to draw himself on the screen.
  • Keep a link to all of your clicked images in the collection (e.g. List).
  • Create a method to determine if the user's click position is within your clickable image (I put this in the object created by C # 1).
  • In mouseClicked (or other applicable methods in your listener) go to your image collection and call wasClicked () , which might look something like this:

     public boolean wasClicked( int x, int y ) { return( x > getX() && x < ( getX() + getWidth() ) && y > getY() && y < ( getY() + getHeight() ) ); } 

Your implementation may vary. Alternatively, you can also extend the JComponent and add a MouseListener to this, but for reasons that I can't remember at the moment, the above worked better for me in my case.

0
source

Source: https://habr.com/ru/post/1379892/


All Articles