1

Quick question I have a java file called Board.java which creates and places chess pieces on it, but I have a problem currently it only makes the chess board itself but does not assign the pieces

I assume I am not linking the png's right

Here is a screenshot of the my setup I am using Netbeans to build the game I have screenshot it so you can see where the images are stored and how I have so far written the program

Also before you ask i am not getting errors when I run the program it just pops up an empty chess board with not pieces

enter image description here

Also here is the Java file

Board.java

package chessgame;

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import static javax.swing.WindowConstants.DISPOSE_ON_CLOSE;

public class Board extends JFrame  {

    JLayeredPane layeredPane;
    JPanel chessBoard;
    JLabel chessPiece;
    int xAdjustment;
    int yAdjustment;
    int startX;
    int startY;
    int initialX;
    int initialY;
    JPanel panels;
    JLabel pieces;

    public  Board(){
        Dimension boardSize = new Dimension(700, 700);

        //  This is a Layered Pane for this application
        layeredPane = new JLayeredPane();
        getContentPane().add(layeredPane);
        layeredPane.setPreferredSize(boardSize);

        //Add a chess board to the Layered Pane
        chessBoard = new JPanel();
        layeredPane.add(chessBoard, JLayeredPane.DEFAULT_LAYER);
        chessBoard.setLayout( new GridLayout(8, 8) );
        chessBoard.setPreferredSize( boardSize );
        chessBoard.setBounds(0, 0, boardSize.width, boardSize.height);

        for (int i = 0; i < 64; i++) {
            JPanel square = new JPanel( new BorderLayout() );
            chessBoard.add( square );

            int row = (i / 8) % 2;
            if (row == 0)
                square.setBackground( i % 2 == 0 ? Color.white : Color.gray );
            else
                square.setBackground( i % 2 == 0 ? Color.gray : Color.white );
        }
        // Setting up the Initial Chess board.
        for(int i=8;i < 16; i++){
            pieces = new JLabel( new ImageIcon("WhitePawn.png") );
            panels = (JPanel)chessBoard.getComponent(i);
            panels.add(pieces);
        }
        pieces = new JLabel( new ImageIcon("WhiteRook.png") );
        panels = (JPanel)chessBoard.getComponent(0);
        panels.add(pieces);
        pieces = new JLabel( new ImageIcon("WhiteKnight.png") );
        panels = (JPanel)chessBoard.getComponent(1);
        panels.add(pieces);
        pieces = new JLabel( new ImageIcon("WhiteKnight.png") );
        panels = (JPanel)chessBoard.getComponent(6);
        panels.add(pieces);
        pieces = new JLabel( new ImageIcon("WhiteBishup.png") );
        panels = (JPanel)chessBoard.getComponent(2);
        panels.add(pieces);
        pieces = new JLabel( new ImageIcon("WhiteBishup.png") );
        panels = (JPanel)chessBoard.getComponent(5);
        panels.add(pieces);
        pieces = new JLabel( new ImageIcon("WhiteKing.png") );
        panels = (JPanel)chessBoard.getComponent(3);
        panels.add(pieces);
        pieces = new JLabel( new ImageIcon("WhiteQueen.png") );
        panels = (JPanel)chessBoard.getComponent(4);
        panels.add(pieces);
        pieces = new JLabel( new ImageIcon("WhiteRook.png") );
        panels = (JPanel)chessBoard.getComponent(7);
        panels.add(pieces);
        for(int i=48;i < 56; i++){
            pieces = new JLabel( new ImageIcon("BlackPawn.png") );
            panels = (JPanel)chessBoard.getComponent(i);
            panels.add(pieces);
        }
        pieces = new JLabel( new ImageIcon("PieceImages/BlackRook.png") );
        panels = (JPanel)chessBoard.getComponent(56);
        panels.add(pieces);
        pieces = new JLabel( new ImageIcon("BlackKnight.png") );
        panels = (JPanel)chessBoard.getComponent(57);
        panels.add(pieces);
        pieces = new JLabel( new ImageIcon("BlackKnight.png") );
        panels = (JPanel)chessBoard.getComponent(62);
        panels.add(pieces);
        pieces = new JLabel( new ImageIcon("BlackBishup.png") );
        panels = (JPanel)chessBoard.getComponent(58);
        panels.add(pieces);
        pieces = new JLabel( new ImageIcon("BlackBishup.png") );
        panels = (JPanel)chessBoard.getComponent(61);
        panels.add(pieces);
        pieces = new JLabel( new ImageIcon("BlackKing.png") );
        panels = (JPanel)chessBoard.getComponent(59);
        panels.add(pieces);
        pieces = new JLabel( new ImageIcon("BlackQueen.png") );
        panels = (JPanel)chessBoard.getComponent(60);
        panels.add(pieces);
        pieces = new JLabel( new ImageIcon("BlackRook.png") );
        panels = (JPanel)chessBoard.getComponent(63);
        panels.add(pieces);
    }
}

Thanks for reading any help is welcomed

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
Jamiex304
  • 242
  • 1
  • 7
  • 26

3 Answers3

5

Yes, you are incorrectly adding the piece images.

    new JLabel(new ImageIcon(getClass().getResource("/chessgame/PieceImages/WhitePawn.png"));

You should use that format for each of the pieces you have to add. The reason it works like this is because when you package your program in a JAR, it must be able to read from INSIDE the JAR. This allows us to get the class resource from the package indicated ("chessgame.PieceImages").

You may also want to consider using the NetBeans Swing Designer, which may be a much easier way to go about this chess program of yours.

Sameer Puri
  • 987
  • 8
  • 20
  • 1
    +1 all the way up to recommending the form editor. This is a nit pick on behalf, but I'd always recommend learning to code layouts/UI's by hand to start with (apart from learning useful skills and techniques, you're more likely to come across other peoples code which handed coded, especially if you're in a team)...but that's me... – MadProgrammer Nov 09 '14 at 23:19
  • @MadProgrammer I would agree with you on that one. – Sameer Puri Nov 09 '14 at 23:35
1

ImageIcon(String) assumes that the image you are loading is contained within a flat file on the disk, based on the screen shot, you images are embedded resources within your application context (they get put into the resulting Jar along with your code).

In this case, you need to use Class#getResource to load them, for example

new ImageIcon(getClass().getResource("/chessgame/PieceImages/WhiteRook.png"));

I'd also encourage you to use ImageIO of ImageIcon, as ImageIO will throw an exception if something goes wrong while loading the image (like it can't be found or read) and will block until the image is fully realised/loaded.

See Reading/Loading an Image for more details

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
-1

The path your are using should be relative to your root source directory :

pieces = new JLabel( new ImageIcon("chessgame/PieceImages/BlackRook.png") );

You can browse with a file explorer (not eclipse, it hides this) to the "bin" folder to understand how files are generated here.

Florent
  • 391
  • 1
  • 12
  • From the [JavaDocs](https://docs.oracle.com/javase/7/docs/api/javax/swing/ImageIcon.html#ImageIcon(java.lang.String)) - *"Creates an ImageIcon from the specified **file**"* - the attached screen shot clearly shows the images stored within the `src` package of project, making them no longer accessible from the file system, but now embedded within the application context... – MadProgrammer Nov 09 '14 at 23:18
  • You are right. I think it would have worked with Eclipse though (I had not seen it was netbeans), as Eclipse starts the program from the bin folder. But not the rightful way to do. – Florent Nov 09 '14 at 23:46