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
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
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.