import javax.swing.*; import java.awt.*; import java.awt.event.*; public class ChessBoard extends JPanel implements MouseListener { private final Color WHITE_SPACE = new Color(47,76,76); private final Color BLACK_SPACE = new Color(247,232,215); private GridLayout grid; private Dimension squareSize = new Dimension(45,45); private Dimension boardSize = new Dimension(360,360); private ImageIcon imageTest = new ImageIcon("D:/Documents and Settings/Me/My Documents/My Pictures/saradomin_staff1.gif"); private ChessPiece[][] board = new ChessPiece[8][8]; private JPanel[][] square = new JPanel[8][8]; /* * FOR IMAGE BUG * Boarder donesnt exist * image size 45,45 * panel size 45,45 * setIconTextGap() ineffective * * */ public ChessBoard(GridLayout grid) { super(grid); this.grid = grid; setPreferredSize(boardSize); setMaximumSize(boardSize); setMinimumSize(boardSize); //Initialize Pieces initialize(true); addMouseListener(this); } //Args tell who is on top / bot :: true = white on bot private void initialize(boolean position) { ChessPiece.COLOR color; for (int b = 0; b < 8; b++) { if (b == 0 || b == 1) { color = (position)? ChessPiece.COLOR.BLACK : ChessPiece.COLOR.WHITE; } else if (b == 6 || b == 7) { color = (position)? ChessPiece.COLOR.WHITE : ChessPiece.COLOR.BLACK; } else { color = ChessPiece.COLOR.EMPTY; } for (int a = 0; a < 8; a++) { if (b == 0 || b == 7) { switch (a) { case 0: board[a][b] = new Rook(color,a,b); break; case 1: board[a][b] = new Knight(color,a,b); break; case 2: board[a][b] = new Bishop(color,a,b); break; case 3: if (position) { board[a][b] = new Queen(color,a,b); } else { board[a][b] = new King(color,a,b); } break; case 4: if (!position) { board[a][b] = new Queen(color,a,b); } else { board[a][b] = new King(color,a,b); } break; case 5: board[a][b] = new Bishop(color,a,b); break; case 6: board[a][b] = new Knight(color,a,b); break; case 7: board[a][b] = new Rook(color,a,b); break; } } else if (b == 1 || b == 6) { board[a][b] = new Pawn(color,a,b); } else { board[a][b] = new Empty(a,b); } } } //Set up the backgrounds on the chessboard boolean tt = false; for (int b = 0; b < 8; b++) { for (int a = 0; a < 8; a++) { JPanel boardColor = new JPanel(); boardColor.setPreferredSize(squareSize); boardColor.setMaximumSize(squareSize); boardColor.setMinimumSize(squareSize); if (tt) { boardColor.setBackground(WHITE_SPACE); tt = false; } else { boardColor.setBackground(BLACK_SPACE); tt = true; } boardColor.add(board[a][b]); add(boardColor); System.out.println(boardColor.getBorder() + " " + board[a][b].getBorder()); square[a][b] = boardColor; } tt = tt? false : true; } } //Returns wether or not the given place is open public boolean inUse(int x, int y) { if (board[x][y].getPiece() == ChessPiece.PIECE.EMPTY) return false; return true; } //Returns the entire ChessPiece[][] public ChessPiece[][] getBoard() { return board; } //Returns the ChessPiece at the given board location public ChessPiece getPiece(int x, int y) { return board[x][y]; } //Returns the ChessPiece at the given pixel location public ChessPiece getPieceByPixel(int x, int y) { return board[x /45][y /45]; } public void paintComponent(Graphics g) { } public void mousePressed(MouseEvent e) { ChessPiece p = getPieceByPixel(e.getX(),e.getY()); System.out.println(e.getX() + "," + e.getY() + " " + p.getColor() + " " + p.getPiece() + " " + p.getX() + "," + p.getY()); } public void mouseReleased(MouseEvent e) { } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } public void mouseClicked(MouseEvent e) { } } import java.awt.*; import javax.swing.*; //GridBagLayout GridLayout public class Test extends JPanel { private final int CHESSBOARD_SQUARE_SIZE = 45; // DO NOT CHANGE private ChessBoard chessBoard; public Test() { JFrame window = new JFrame("Chess"); GridLayout grid = new GridLayout(8,8,0,0); chessBoard = new ChessBoard(grid); //chessBoard.setSize(360,360); window.setBounds(100, 100, 600, 600); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // chessBoard.setLocation(100,100,100,100); Container c = window.getContentPane(); c.add(chessBoard); window.pack(); //grid.layoutContainer(panel); //System.out.println(grid.preferredLayoutSize(window)); window.setVisible(true); repaint(); } public void paintComponent(Graphics g) { System.out.println("I AM CALLED"); super.paintComponent(g); chessBoard.paintComponent(g); } public static void main(String[] args) { Test test = new Test(); } }