Question

Im trying to create a chess program in Java. Right now, I have the board done together with the pieces present, which I can move with my mouse by dragging and dropping.

What I need is to add coordinates to the squares at the sides, like on a real board. Doesn't have to be anything fancy, just a visual. Since I drew my board not with Graphics, I don't know how to draw on top of the board I have created :/

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;

public class ChessGame extends JFrame implements MouseListener,
      MouseMotionListener {

   JLayeredPane layeredPane;
   JPanel chessBoard;
   JLabel chessPiece;
   int xCoordinate;
   int yCoordinate;

   public ChessGame() {
      Dimension boardSize = new Dimension(600, 600);

      layeredPane = new JLayeredPane();
      getContentPane().add(layeredPane);
      layeredPane.setPreferredSize(boardSize);
      layeredPane.addMouseListener(this);
      layeredPane.addMouseMotionListener(this);

      // adding chess board

      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 ? new Color(238, 221, 187)
                  : new Color(204, 136, 68));
         else
            square.setBackground(i % 2 == 0 ? new Color(204, 136, 68)
                  : new Color(238, 221, 187));
      }

      // Black pieces on the board

      JLabel piece = new JLabel(new ImageIcon(getClass().getResource(
            "Rooka8.png")));
      JPanel panel = (JPanel) chessBoard.getComponent(0);
      panel.add(piece);
      piece = new JLabel(new ImageIcon(getClass().getResource("Knightb8.png")));
      panel = (JPanel) chessBoard.getComponent(1);
      panel.add(piece);
      piece = new JLabel(new ImageIcon(getClass().getResource("Bishopc8.png")));
      panel = (JPanel) chessBoard.getComponent(2);
      panel.add(piece);
      piece = new JLabel(new ImageIcon(getClass().getResource("Queend8.png")));
      panel = (JPanel) chessBoard.getComponent(3);
      panel.add(piece);
      piece = new JLabel(new ImageIcon(getClass().getResource("Kinge8.png")));
      panel = (JPanel) chessBoard.getComponent(4);
      panel.add(piece);
      piece = new JLabel(new ImageIcon(getClass().getResource("Bishopf8.png")));
      panel = (JPanel) chessBoard.getComponent(5);
      panel.add(piece);
      piece = new JLabel(new ImageIcon(getClass().getResource("Knightg8.png")));
      panel = (JPanel) chessBoard.getComponent(6);
      panel.add(piece);
      piece = new JLabel(new ImageIcon(getClass().getResource("Rookh8.png")));
      panel = (JPanel) chessBoard.getComponent(7);
      panel.add(piece);
      piece = new JLabel(new ImageIcon(getClass().getResource("Pawna7.png")));
      panel = (JPanel) chessBoard.getComponent(8);
      panel.add(piece);
      piece = new JLabel(new ImageIcon(getClass().getResource("Pb7.png")));
      panel = (JPanel) chessBoard.getComponent(9);
      panel.add(piece);
      piece = new JLabel(new ImageIcon(getClass().getResource("Pawnc7.png")));
      panel = (JPanel) chessBoard.getComponent(10);
      panel.add(piece);
      piece = new JLabel(new ImageIcon(getClass().getResource("Pawnd7.png")));
      panel = (JPanel) chessBoard.getComponent(11);
      panel.add(piece);
      piece = new JLabel(new ImageIcon(getClass().getResource("Pawne7.png")));
      panel = (JPanel) chessBoard.getComponent(12);
      panel.add(piece);
      piece = new JLabel(new ImageIcon(getClass().getResource("Pawnf7.png")));
      panel = (JPanel) chessBoard.getComponent(13);
      panel.add(piece);
      piece = new JLabel(new ImageIcon(getClass().getResource("Pawng7.png")));
      panel = (JPanel) chessBoard.getComponent(14);
      panel.add(piece);
      piece = new JLabel(new ImageIcon(getClass().getResource("Pawnh7.png")));
      panel = (JPanel) chessBoard.getComponent(15);
      panel.add(piece);

      // White pieces on the board

      piece = new JLabel(new ImageIcon(getClass().getResource("Pawna2.png")));
      panel = (JPanel) chessBoard.getComponent(48);
      panel.add(piece);
      piece = new JLabel(new ImageIcon(getClass().getResource("Pawnb2.png")));
      panel = (JPanel) chessBoard.getComponent(49);
      panel.add(piece);
      piece = new JLabel(new ImageIcon(getClass().getResource("Pawnc2.png")));
      panel = (JPanel) chessBoard.getComponent(50);
      panel.add(piece);
      piece = new JLabel(new ImageIcon(getClass().getResource("Pawnd2.png")));
      panel = (JPanel) chessBoard.getComponent(51);
      panel.add(piece);
      piece = new JLabel(new ImageIcon(getClass().getResource("Pawne2.png")));
      panel = (JPanel) chessBoard.getComponent(52);
      panel.add(piece);
      piece = new JLabel(new ImageIcon(getClass().getResource("Pawnf2.png")));
      panel = (JPanel) chessBoard.getComponent(53);
      panel.add(piece);
      piece = new JLabel(new ImageIcon(getClass().getResource("Pawng2.png")));
      panel = (JPanel) chessBoard.getComponent(54);
      panel.add(piece);
      piece = new JLabel(new ImageIcon(getClass().getResource("Pawnh2.png")));
      panel = (JPanel) chessBoard.getComponent(55);
      panel.add(piece);
      piece = new JLabel(new ImageIcon(getClass().getResource("Rooka1.png")));
      panel = (JPanel) chessBoard.getComponent(56);
      panel.add(piece);
      piece = new JLabel(new ImageIcon(getClass().getResource("Knightb1.png")));
      panel = (JPanel) chessBoard.getComponent(57);
      panel.add(piece);
      piece = new JLabel(new ImageIcon(getClass().getResource("Bishopc1.png")));
      panel = (JPanel) chessBoard.getComponent(58);
      panel.add(piece);
      piece = new JLabel(new ImageIcon(getClass().getResource("Queend1.png")));
      panel = (JPanel) chessBoard.getComponent(59);
      panel.add(piece);
      piece = new JLabel(new ImageIcon(getClass().getResource("Kinge1.png")));
      panel = (JPanel) chessBoard.getComponent(60);
      panel.add(piece);
      piece = new JLabel(new ImageIcon(getClass().getResource("Bishopf1.png")));
      panel = (JPanel) chessBoard.getComponent(61);
      panel.add(piece);
      piece = new JLabel(new ImageIcon(getClass().getResource("Knightg1.png")));
      panel = (JPanel) chessBoard.getComponent(62);
      panel.add(piece);
      piece = new JLabel(new ImageIcon(getClass().getResource("Rookh1.png")));
      panel = (JPanel) chessBoard.getComponent(63);
      panel.add(piece);

   }

   public void mousePressed(MouseEvent e) {
      chessPiece = null;
      Component c = chessBoard.findComponentAt(e.getX(), e.getY());

      if (c instanceof JPanel)
         return;

      Point parentLocation = c.getParent().getLocation();
      xCoordinate = parentLocation.x - e.getX();
      yCoordinate = parentLocation.y - e.getY();
      chessPiece = (JLabel) c;
      chessPiece.setLocation(e.getX() + xCoordinate, e.getY() + yCoordinate);
      chessPiece.setSize(chessPiece.getWidth(), chessPiece.getHeight());
      layeredPane.add(chessPiece, JLayeredPane.DRAG_LAYER);
   }

   // move pieces

   public void mouseDragged(MouseEvent me) {
      if (chessPiece == null)
         return;
      chessPiece.setLocation(me.getX() + xCoordinate, me.getY() + yCoordinate);
   }

   // drop a piece when mouse is released

   public void mouseReleased(MouseEvent e) {
      if (chessPiece == null)
         return;

      chessPiece.setVisible(false);
      Component c = chessBoard.findComponentAt(e.getX(), e.getY());

      if (c instanceof JLabel) {
         Container parent = c.getParent();
         parent.remove(0);
         parent.add(chessPiece);
      }

      else

      {
         Container parent = (Container) c;
         parent.add(chessPiece);
      }

      chessPiece.setVisible(true);
   }

   public void mouseClicked(MouseEvent e) {
   }

   public void mouseMoved(MouseEvent e) {
   }

   public void mouseEntered(MouseEvent e) {
   }

   public void mouseExited(MouseEvent e) {
   }

   public static void main(String[] args) {
      JFrame frame = new ChessGame();
      frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
      frame.pack();
      frame.setResizable(true);
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

}
Was it helpful?

Solution

  • You should put your chessBoard JPanel into another BorderLayout-using JPanel.
  • This container JPanel will hold a GridLayout using JPanel on the left and on the bottom.
  • And these can hold JLabels which hold your row and column labels.

Edit

  • On second thought, better for the container JPanel to use a GridBagLayout so that the side JPanels will size correctly.

Edit 2
For example:

import java.awt.*;
import javax.swing.*;

@SuppressWarnings("serial")
public class ChessGame2 extends JPanel {
   private static final int RANKS = 8;
   private static final int FILES = RANKS;
   private static final int SIDE = 80;
   private static final Dimension SQUARE_SIZE = new Dimension(SIDE, SIDE);
   private static final Color LIGHT_COLOR = new Color(238, 221, 187);
   private static final Color DARK_COLOR = new Color(204, 136, 68);
   private static final int GAP = 5;

   private JPanel chessBoard = new JPanel(new GridLayout(RANKS, FILES));

   public ChessGame2() {
      for (int rank = 0; rank < RANKS; rank++) {
         for (int file = 0; file < FILES; file++) {
            JPanel square = new JPanel();
            square.setPreferredSize(SQUARE_SIZE);
            Color bg = (rank % 2 == file % 2) ? LIGHT_COLOR : DARK_COLOR; 
            square.setBackground(bg);
            chessBoard.add(square);
         }
      }

      setLayout(new GridBagLayout());
      GridBagConstraints gbc = new GridBagConstraints();
      gbc.gridx = 0;
      gbc.gridy = 1;
      gbc.gridwidth = 1;
      gbc.gridheight = 1;
      gbc.weightx = 0.0;
      gbc.weighty = 0.0;
      gbc.fill = GridBagConstraints.BOTH;
      gbc.anchor = GridBagConstraints.WEST;
      gbc.insets = new Insets(0, 2 * GAP, 0, 2 * GAP);
      add(createRankPanel(), gbc);

      gbc.gridx = 2;
      gbc.anchor = GridBagConstraints.EAST;
      add(createRankPanel(), gbc);

      gbc.gridx = 1;
      gbc.gridy = 0;
      gbc.anchor = GridBagConstraints.SOUTH;
      gbc.insets = new Insets(GAP, 0, GAP, 0);
      add(createFilePanel(), gbc);

      gbc.gridy = 2;
      gbc.anchor = GridBagConstraints.NORTH;
      add(createFilePanel(), gbc);      

      gbc.gridx = 1;
      gbc.gridy = 1;
      gbc.anchor = GridBagConstraints.CENTER;
      gbc.insets = new Insets(0, 0, 0, 0);
      add(chessBoard, gbc);
   }

   private JPanel createFilePanel() {
      JPanel filePanel = new JPanel(new GridLayout(1, 0));
      for (int i = 0; i < FILES; i++) {
         char fileChar = (char) ('A' + i);
         filePanel.add(new JLabel(String.valueOf(fileChar), SwingConstants.CENTER));
      }
      return filePanel;
   }

   private JPanel createRankPanel() {
      JPanel rankPanel = new JPanel(new GridLayout(0, 1));
      for (int i = 0; i < RANKS; i++) {
         int row = RANKS - i;
         rankPanel.add(new JLabel(String.valueOf(row)));
      }
      return rankPanel;
   }

   private static void createAndShowGui() {
      ChessGame2 mainPanel = new ChessGame2();

      JFrame frame = new JFrame("Chess Game");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

Which displays as:

enter image description here

OTHER TIPS

The panel that you return from the chess board should be your custom class extending JPanel and in there you will want to override paintComponent().

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top