0

I have a JPanel which has a GridLayout of (5,7 ) and I am trying to add an image to a specific grid cell eg : (3,1) . On researching , I realised there is a way and followed it.

Compilable source code

package testing;

import java.io.*;
import java.util.*;
import java.security.*;
import javax.xml.bind.DatatypeConverter;
import java.lang.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;

public class Testing extends JPanel {

    public static class TestPanel extends JPanel {
        TestPanel() {
            int i = 5; // num of rows
            int j = 7; // num of cols

            PanelHolder = new JPanel[i][j];
            setLayout(new GridLayout(i, j));

            for (int m = 0; m < i; m++) {
                for (int n = 0; n < j; n++) {
                    PanelHolder[m][n] = new JPanel();
                    add(PanelHolder[m][n]);
                }
            }

            File file = new File("./src/testing/Ace_Club_1_1.png");
            File file2 = new File("./src/testing/Ace_Diamond_1_1.png");

            try {
                image = ImageIO.read(file);
                image2= ImageIO.read(file2);
            } catch (IOException ie) {

            }

            ImagePanel ip = new ImagePanel(image);
            PanelHolder[3][1].add(ip); // doesnt add according to cell grid coordinates
            ImagePanel ip2 = new ImagePanel(image2);
             PanelHolder[3][3].add(ip2);

        }

        JPanel[][] PanelHolder;

        JLabel DeckLabel;
        JPanel DeckPanel;
        ImageIcon Deckimg;

        private BufferedImage image;
        BufferedImage image2;

    }


    public static class ImagePanel extends JPanel {
        ImagePanel(BufferedImage image) {
            this.i = image;

        }


        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.drawImage(i, 0, 0, getWidth(), getHeight(), this);

        }

        BufferedImage i;
    }

    public static void main(String[] args) {
        TestPanel tp = new TestPanel();

        JFrame frame = new JFrame();

        frame.add(tp);
        frame.pack();
        frame.setVisible(true);

    }

}

For some unknown reason , I cant add my image to a specific cell grid and it appears in the wrong grid cell , i also cant add more than 1 image.

You can download the images from here

EDIT: edited the source code to include Andrew Thompson answer to add more than 1 image but still doesnt work

This is the output after adding the second image on the advice of Andrew Thompson

enter image description here

Community
  • 1
  • 1
Computernerd
  • 7,378
  • 18
  • 66
  • 95
  • *"..it appears in the wrong grid cell"* Which grid cell did it appear in? 1,3? – Andrew Thompson Apr 23 '14 at 10:35
  • BTW - one approach to this is to fill the `GridLayout` with a `JButton[][]` or `JLabel[][]` and set an icon for the relevant component. – Andrew Thompson Apr 23 '14 at 10:38
  • @AndrewThompson it appeared in grid cell (5,3) , you can compile the code i posted – Computernerd Apr 23 '14 at 11:15
  • *"you can compile the code i posted"* Sure, but I can't run it and have it work the way you see it, because I don't have the image. One way to get image(s) for an example is to *hot-link* to the images seen in [this answer](http://stackoverflow.com/a/19209651/418556). – Andrew Thompson Apr 23 '14 at 11:41
  • @AndrewThompson Done , I have already linked the images – Computernerd Apr 23 '14 at 13:56
  • *"I have already linked the images"* No, I didn't say to 'link' to the images, I said to *hot-link* to the images. I won't go through some nonsense involving downloading image(s) to a particular place in order to test the code, access them by URL directly in the code. – Andrew Thompson Apr 23 '14 at 14:24

1 Answers1

1
ImagePanel ip = new ImagePanel(image);
PanelHolder[3][1].add(ip); // doesnt add according to cell grid coordinates
PanelHolder[3][3].add(ip); // only 1 image appears 

1 component can only appear in one place in a GUI. If you need to display 2, create 2.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433