0

Possible Duplicate:
ImageIcons on JButton are not showing up in Runnable JAR file

I am trying to add an image to a gui in java for a chess game:

JPanel theBoard;
    JLabel aPiece;
    JLayeredPane layeredPane;

    public ChessGameGUI() 
    {
        Dimension size = new Dimension(480, 480);

        layeredPane = new JLayeredPane();
        getContentPane().add(layeredPane);
        layeredPane.setPreferredSize(size);

        theBoard = new JPanel();
        layeredPane.add(theBoard, JLayeredPane.DEFAULT_LAYER);
        GridLayout chessBoardLayout = new GridLayout(8,8);
        theBoard.setLayout(chessBoardLayout);
        theBoard.setPreferredSize(size);
        theBoard.setBounds(0, 0, size.width, size.height);

        boolean drawWhite = false;

        // setup initial squares
        for(int x = 0; x < 8; x++)
        {
            for(int y = 0; y < 8; y++)
            {
                BorderLayout squareBorder = new BorderLayout();
                JPanel aChessSquare = new JPanel(squareBorder);
                theBoard.add(aChessSquare);

                if(drawWhite) aChessSquare.setBackground(Color.white);
                else aChessSquare.setBackground(Color.green);

                if(y == 7);
                else drawWhite = !drawWhite;
            }
        }
    }

This piece of code sets up the checkered-patterned board just fine, but when I go to add individual pieces, nothing happens:

for(int x = 0; x < 8; x++)
        {
            for(int y = 0; y < 8; y++)
            {
                if(board[x][y]._isOccupied)
                {
                    System.out.println("found a piece at x = " + x + " y = " + y);

                    //ImageIcon pieceImage = new ImageIcon("Images/wPawn.jpg");
                    ImageIcon pieceImage = findPieceImage(board, x, y);

                    JLabel pieceToDraw = new JLabel(pieceImage);
                    JPanel curComponent = (JPanel)theBoard.getComponent(7 * x + y);
                    curComponent.add(pieceToDraw);
                }
            }
        }

The Images folder is contained in the src folder. Anyone know why this wouldn't work?

Community
  • 1
  • 1
thebiglebowski11
  • 1,451
  • 10
  • 41
  • 76
  • 3
    I think this 'cannot find `ImageIcon` with `String`' matter has been discussed twice in the last 48 hrs, maybe 3 times. – Andrew Thompson Jan 31 '13 at 05:56
  • 2
    So, where's the image store? On the filesystem or embedded within the application? `ImageIcon(String)` is passing a file reference to the API. Is this where the image is stored? Perhaps [this](http://stackoverflow.com/questions/14596487/imageicons-on-jbutton-are-not-showing-up-in-runnable-jar-file/14596578#14596578) might help shed some light on the problem. – MadProgrammer Jan 31 '13 at 05:59
  • @MadProgrammer I already voted up that answer, so will have to settle with +1 for the comment/link. – Andrew Thompson Jan 31 '13 at 06:02
  • @AndrewThompson I could copy and paste the answer again...but that would just be wrong ... – MadProgrammer Jan 31 '13 at 06:04
  • @MadProgrammer I just used that question as the possible duplicate. I don't know if that is 'wrong', but am comforted that 4 people have to agree with me before it has any effect. ;) – Andrew Thompson Jan 31 '13 at 06:06
  • @AndrewThompson Only need 3 now ;) – MadProgrammer Jan 31 '13 at 06:07
  • This [answer](http://stackoverflow.com/a/9866659/1057230), might also be of some interest to you. – nIcE cOw Jan 31 '13 at 16:45

0 Answers0