java - 棋盘坐标

标签 java swing coordinates chess

我正在尝试用 Java 创建一个国际象棋程序。现在,我已经把棋盘和现有的棋子一起完成了,我可以用鼠标拖放来移动它们。

我需要的是在两侧的方 block 上添加坐标,就像在真实的板上一样。不需要任何花哨的东西,只是视觉效果。由于我没有使用 Graphics 绘制我的板,我不知道如何在我创建的板的顶部绘制:/

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);
   }

}

最佳答案

  • 您应该将您的 chessBoard JPanel 放入另一个使用 BorderLayout 的 JPanel。
  • 此容器 JPanel 将在左侧和底部使用 JPanel 容纳一个 GridLayout。
  • 这些可以包含 JLabels,JLabels 包含您的行和列标签。

编辑

  • 再三考虑,容器 JPanel 使用 GridBagLayout 更好,这样侧面 JPanel 的大小就会正确。

编辑2
例如:

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();
         }
      });
   }
}

显示为:

enter image description here

关于java - 棋盘坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21432435/

相关文章:

Java无法跳出while循环

java - 如何使用 JPA+Hibernate 在 json 结果中包含连接列

java - 相机 Intent 后显示黑屏

Java Action 监听器不工作

java - equals() 方法检查 X 和 Y 坐标

java - C#、Java SQLite 无法读取/写入

java - JTable/DefaultTableModel 中的 TableModelListener 和 addRow 干扰

java swing 多线程绘制

c# - iTextSharp - 绝对坐标表

javascript - 在坐标系中找到最多未填充的点