0

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

Community
  • 1
  • 1
Nicholas Eason
  • 290
  • 4
  • 13
  • @peeskillet I just did, doing that caused it to stretch vertically way more than I want it to, and just the board without pieces showed up. – Nicholas Eason Oct 10 '14 at 18:33
  • Ok, I'll remove it then I guess. – Nicholas Eason Oct 10 '14 at 18:35
  • See [example from Andrew Thompson](http://stackoverflow.com/a/21096455/2587435) – Paul Samsotha Oct 10 '14 at 19:09
  • @peeskillet I don't think making multiple methods will help. Everything in this method is used to make the board and I'm pretty sure my issue is either the JButtons/Labels themselves or with the Icons not being found by the program. – Nicholas Eason Oct 10 '14 at 19:10
  • When I test it, you are adding more than one label/image to each panel. That's why I said to break down the logic, like first adding creating and adding the panels, then add the images and labels separately. You are trying to do everything in the same loop, which is a little hard to read. There a bunch of chess board question here on SO, just search for some good techniques. I'm sure there are hundreds of different ways this can be accomplished – Paul Samsotha Oct 10 '14 at 19:14
  • As far as _"or with the Icons not being found by the program"_, you don't have any file extension in your paths `"H:/ChessPieces/PawnWhite"`. Not sure if that's the problem. – Paul Samsotha Oct 10 '14 at 19:17
  • That might be the problem... – Nicholas Eason Oct 10 '14 at 19:22
  • Yeah, I was switching between ImageIcon's and BufferedImages a lot to get one working correctly :/ Seems I forgot a file extension lol – Nicholas Eason Oct 10 '14 at 19:24

0 Answers0