I am trying to make a chess game, to show chess pieces on board, I made a main JPanel which has a gridlayout containing every tiles which are all JPanels too. I then draw pieces on the board using this function:
public void drawPiece(Graphics g) {
try {
g.drawImage(ImageIO.read(Queen.class.getResource(path)), 18, 10, 75, 90, null);
} catch (IOException e) {
e.printStackTrace();
}
g.dispose();
}
This works fine, the problem I am having comes when I try to set a border on the JPanel in which there is already a piece. I have a color code to put borders on tiles to show illegal moves, kill moves and normal moves when a piece is selected. When I place a border where there is a chess piece, then the image dissapears and even if I draw it again right after adding the border, it doesnt show.
For a solution I tried placing a second JPanel on top of my tile panel, which I called graphicsPanel, this panel is where I now draw my chess piece, and then I change the border of the tile containing the panel where the image is and this didn't fix anything, the problem stays the same. I am out of ideas and don't know how to fix it, so if anyone can help me, that would be great, thank you!
public void mouseClicked(MouseEvent e) {
Tile tile = (Tile) e.getSource();
ArrayList<Point> possibleMoves = new ArrayList<Point>();
if(tile.piece != null) {
switch(currentTurn) {
case white:{
for(Piece current: this.white.pieceList) {
if(current.x + current.y * 8 == tile.number) {
if(current.isSelected == false) {
current.isSelected = true;
possibleMoves = current.possibleMoves();
for(Point currentPoint: possibleMoves) {
if(board.tileList.get(currentPoint.x + currentPoint.y * 8).piece == null) {
board.tileList.get(currentPoint.x + currentPoint.y * 8).setBorder(legalMoveBorder);
}else if(board.tileList.get(currentPoint.x + currentPoint.y * 8).piece.color == 'b') {
board.tileList.get(currentPoint.x + currentPoint.y * 8).setBorder(killBorder);
}else {
board.tileList.get(currentPoint.x + currentPoint.y * 8).setBorder(illegalMoveBorder);
}
}
}else {
current.isSelected = false;
board.removeAllBorders();
}
break;
}
}
}
case black:{
for(Piece current: this.black.pieceList) {
if(current.x + current.y * 8 == tile.number) {
if(current.isSelected == false) {
current.isSelected = true;
possibleMoves = current.possibleMoves();
for(Point currentPoint: possibleMoves) {
if(board.tileList.get(currentPoint.x + currentPoint.y * 8).piece == null) {
board.tileList.get(currentPoint.x + currentPoint.y * 8).setBorder(legalMoveBorder);
}else if(board.tileList.get(currentPoint.x + currentPoint.y * 8).piece.color == 'w') {
board.tileList.get(currentPoint.x + currentPoint.y * 8).setBorder(killBorder);
}else {
board.tileList.get(currentPoint.x + currentPoint.y * 8).setBorder(illegalMoveBorder);
}
}
}else {
current.isSelected = false;
board.removeAllBorders();
}
break;
}
}
}
}
}
}
This the code where I add borders and removes borders when piece gets either selected or stop being selected. If anyone can help me, thanks a lot!