Search code examples
javaarraysimageicon

ImageIcon array isn't populating button array with my Images


So here i've made a Grid 4x4 grid with buttons:

import java.awt.GridLayout;

import javax.swing.JPanel;

public class GameGrid extends JFrame {

JPanel pan = new JPanel();
ShapeButtons[] buttons = new ShapeButtons[16];


public GameGrid(){
    super("Shape Match");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    pan.setLayout(new GridLayout(4, 4 ,2 ,2));
    setResizable(true);
    setSize(500,500);
    for (int i = 0; i<16; i++){

        buttons[i] = new ShapeButtons();
        pan.add(buttons[i]);

    }

    add(pan);
    setVisible(true);

}

public static void main(String[] args){

    new GameGrid();
}

}

Now I've created a separate class as a repository for the images:

 import javax.swing.ImageIcon;
 import javax.swing.JButton;

public class ShapeButtons extends JButton{
ImageIcon[] Shapes = new ImageIcon[8];

public ShapeButtons(){


    Shapes[0] = new ImageIcon("src/images/Circle.jpg");
    Shapes[1] = new ImageIcon("src/images/Diamond.jpg");
    Shapes[2] = new ImageIcon("src/images/Square.jpg");
    Shapes[3] = new ImageIcon("src/images/rectangle.jpg");
    Shapes[4] = new ImageIcon("src/images/Ellipse.jpg");
    Shapes[5] = new ImageIcon("src/images/heart.jpg");
    Shapes[6] = new ImageIcon("src/images/star.jpg");
    Shapes[7] = new ImageIcon("src/images/triangle.jpg");
}

}

But they aren't populating the buttons with my images, they just appear blank. Any suggestions?


Solution

  • import javax.swing.ImageIcon;
    import javax.swing.JButton;
    
    
    public class ShapeButtons extends JButton{
    static ImageIcon[] Shapes = new ImageIcon[8];
      static {
        Shapes[0] = new ImageIcon("src/images/Circle.jpg");
            Shapes[1] = new ImageIcon("src/images/Diamond.jpg");
            Shapes[2] = new ImageIcon("src/images/Square.jpg");
            Shapes[3] = new ImageIcon("src/images/rectangle.jpg");
            Shapes[4] = new ImageIcon("src/images/Ellipse.jpg");
            Shapes[5] = new ImageIcon("src/images/heart.jpg");
            Shapes[6] = new ImageIcon("src/images/star.jpg");
            Shapes[7] = new ImageIcon("src/images/triangle.jpg");
            }
    
    public ShapeButtons(int picId){
        super ();//call the jButton cunstructor
    
         this.setIcon(shapes[picId]);// sets an icon the the Jbutton object.
         }
    
       public ShapeButtons(){
            super ();//call the jButton cunstructor            
             }
    
        }
    

    and your call to the first constructor should look like this.

     for (int i = 0; i<16; i++){
    
        buttons[i] = new ShapeButtons(someInteger);//for example
        pan.add(buttons[i]);
    
    }
    

    your call to the second constructor should look like this.

     for (int i = 0; i<16; i++){
    
        buttons[i] = new ShapeButtons();//for example
        pan.add(buttons[i]);
        buttons[i].setIcon(buttons[i].Shapes[someInteger])
    
    }