user3155582
user3155582

Reputation: 3

2d icon arrays JAVA and printing out in JOptionpane box

How would I create a 2d icon array and print it out in a JOptionPane box. so Far I have this, but when I print it out it shows a bunch of BlockEmpty.png

public class iconarray{

public static void main (String [] args)


{
    Icon blockempty = new ImageIcon("BlockEmpty.png");

    Icon Board [] [] = new Icon [8] [8];
    String GameBoard = "";
    for (int count2 = 2; count2 <= 7; count2++)
    {

        for (int count3 = 1; count3 <= 7; count3++)
        {
            Board[count2][count3] = blockempty;
         }
    }
    for (int count2 = 2; count2 <= 7; count2++)
     {
        for (int count3 = 1; count3 <= 7; count3++)
        {
            GameBoard = GameBoard + Board[count2][count3];
        }
        GameBoard = GameBoard + "\n"; 
    }
     JOptionPane.showMessageDialog(null, "", "Connect 4", JOptionPane.PLAIN_MESSAGE, blockempty);
}

}

Upvotes: 0

Views: 146

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347234

In order to display a Icon or Image, you first need some way to render it. Icons and Images don't have the means to render them selves (per se), but require another component that can render them.

Something else a lot of people forget, is JOptionPane is capable of display components.

For example:

enter image description here

Icon brick = new ImageIcon(BoardOption.class.getResource("/images.jpg"));
JPanel wall = new JPanel(new GridLayout(8, 8, 0, 0));
JLabel bricks[][] = new JLabel[8][8];

for (int x = 0; x < 8; x++) {
    for (int y = 0; y < 8; y++) {
        bricks[y][x] = new JLabel(brick);
        wall.add(bricks[y][x]);
    }
}

JOptionPane.showMessageDialog(null, wall, "Another brick in the wall", JOptionPane.PLAIN_MESSAGE, null);

Take a look at How to use icons for more details.

Upvotes: 1

Related Questions