So I'm making a chess board game for fun. I got the board part working, now I need the pieces to show up. I've tried placing them on a JButton, and that worked... sort of... the button texture(?) took over the board tiles. I did it with a JLabel yesterday, and that worked... sort of... the icon was tiled within the JLabel or something like that because the top half of the pawn was in the picture below the full size picture. This program is just to set the board and place the pieces. Just before this post, I was looking at this. This didn't work for me, but it IS what I'm looking for.
package Chess;
import javax.swing.*;
import java.util.*;
import java.util.logging.Level;
import java.awt.*;
import java.awt.event.*;
import javax.swing.border.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.*;
public class Board extends Game
{
public void createUI()
{
JFrame board = new JFrame("ADG");
JPanel places = new JPanel(new GridLayout(8,8));
JPanel[][] tiles = new JPanel[8][8];
ImageIcon pawnWhite = new ImageIcon("H:/ChessPieces/PawnWhite");
ImageIcon pawnBlack = new ImageIcon("H:/ChessPieces/PawnBlack");
ImageIcon kingBlack = new ImageIcon("H:/ChessPieces/KingBlack");
ImageIcon kingWhite = new ImageIcon("H:/ChessPieces/KingWhite");
ImageIcon queenBlack = new ImageIcon("H:/ChessPieces/QueenBlack");
ImageIcon queenWhite = new ImageIcon("H:/ChessPieces/QueenWhite");
ImageIcon rookBlack = new ImageIcon("H:/ChessPieces/RookBlack");
ImageIcon rookWhite = new ImageIcon("H:/ChessPieces/RookWhite");
ImageIcon bishopBlack = new ImageIcon("H:/ChessPieces/BishopBlack");
ImageIcon bishopWhite = new ImageIcon("H:/ChessPieces/BishopWhite");
ImageIcon knightBlack = new ImageIcon("H:/ChessPieces/KnightBlack");
ImageIcon knightWhite = new ImageIcon("H:/ChessPieces/KnightWhite");
JLabel[] pawnWhites = new JLabel[8];
JLabel[] pawnBlacks = new JLabel[8];
for(int i = 1; i < 9; ++i)
for(int j = 1; j < 9; ++j)
{
tiles[i-1][j-1] = new JPanel();
for(int z = 1; z<9;z++)
{
pawnBlacks[z-1] = new JLabel("", pawnBlack, JLabel.CENTER);
pawnWhites[z-1] = new JLabel("", pawnWhite, JLabel.CENTER);
pawnWhites[z-1].setOpaque(true);
tiles[i-1][j-1].add(pawnWhites[z-1]);
}
if(i%2==0)
{
if(j%2==0)
{
tiles[i-1][j-1].setBackground(Color.BLACK);
tiles[i-1][j-1].setSize(100,100);
places.add(tiles[i-1][j-1]);
} else if(j%2 !=0)
{
tiles[i-1][j-1].setBackground(Color.WHITE);
tiles[i-1][j-1].setSize(100,100);
places.add(tiles[i-1][j-1]);
}
} else if(i%2 !=0)
{
if(j%2==0)
{
tiles[i-1][j-1].setBackground(Color.WHITE);
tiles[i-1][j-1].setSize(100,100);
places.add(tiles[i-1][j-1]);
} else if(j%2 !=0)
{
tiles[i-1][j-1].setBackground(Color.BLACK);
tiles[i-1][j-1].setSize(100,100);
places.add(tiles[i-1][j-1]);
}
}
}
places.setSize(800,800);
board.setSize(800,800);
places.setSize(800,800);
board.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
board.add(places);
board.setVisible(true);
}
}
NOTE: I tried making setContentAreaFilled to false, and there were 8 buttons in ever panel, ALL without my icon. I then tried removing it as an array, and just as 1 JButton. After that it didn't show a button at all. I believe the lack of Icon is because it's having a problem finding the Icon somehow, but I can't be sure.
EDIT: I found the answer thanks to peeskillet. I was switching things around too much and forgot to add a file extension