get image of panel

currently I have no way of getting the current image icon of a panel that is placed.

Right now I am using getcomponent and figuring out its location to determine which image I used which isn’t the best way any ideas?

 for(lkm = 0; lkm < counter; lkm++)
                      {
                      ImagePanelList.get(lkm).addMouseListener(new MouseAdapter() {
                        	@Override
                        	public void mouseClicked(MouseEvent events) {
                        		int letz;
                        		char let;
                      		  String text;
                      		text = events.getComponent().toString();
                      		//gives me the panels location
                      		System.out.println(text);
                      		let = text.charAt(25);
                      		//get first number of location
                      		letz = Character.getNumericValue(let);
                      		letz = letz - 1;
                      		//minus one since my array list of images starts at 0
                        		if(cardpicked[letz] == false && place[0] == -1)
                          		{
                          			alPickedStringCardName.add(strCardName[letz]);

Did you know there is a Component.getBounds() method?

ok I will use that. another question I want to save this string how do I do that?

 public ImagePanel(String img) {
	    this(new ImageIcon(img).getImage());
}
 public String getImg() {
		  return img.toString();
		  }	 
	  }


doesnt work I want the imageicon string i suppose? how do I go about doing that

You want the String to become an image?
The String parameter in ImageIcon specifies the path to the image in your file system. The toString() method is available to all classes because Object is the superclass of all classes. There are no guarantees on what is returned in the String. Each class can have its own implementation of it. ImageIcon’s toString() returns the path to the image. Image’s toString() returns its name plus its hashcode.

Try actually reading the Javadocs.

ya i understand the difference. What I am trying to do is get the img path that was placed inside of a panel. So I dont have to keep track of them. I tried to do something with the path but I get null.

class ImagePanel extends JPanel {

	  private Image img;
	  private String path2;
	  
	  public ImagePanel(String path) {
		  Image image = new ImageIcon(path).getImage();
		  path2 = path;
	    //this(new ImageIcon(img).getImage());
	 
	  }
	  public ImagePanel(Image img) {
	    this.img = img;
	   
	    Dimension size = new Dimension(img.getWidth(null), img.getHeight(null));
	    setPreferredSize(size);
	    setMinimumSize(size);
	    setMaximumSize(size);
	    setSize(size);
	    setLayout(null);
	  }

	  public void paintComponent(Graphics g) {
	    g.drawImage(img, 0, 0, null);
	  }
	  
	  public String getImg() {
		  return path2;
		  }

	}

Haha you never actually assign img = image :wink:
Try doing “this(image)” inside your first constructor.
Never mind, this(…) needs to be your first call.
do this:


public ImagePanel(String path) {
    this(new ImageIcon(path).getImage());
    path2 = path;
}