Java 棋盘坐标
我正在尝试用Java创建一个国际象棋程序。现在,我已经把棋盘和现在的棋子放在一起了,我可以用鼠标拖拽来移动 我需要的是给两边的正方形添加坐标,就像在一块真正的木板上一样。不需要任何花哨的东西,只是视觉效果。由于我不是用图形绘制板,我不知道如何在我创建的板上绘制:/Java 棋盘坐标,java,swing,coordinates,chess,Java,Swing,Coordinates,Chess,我正在尝试用Java创建一个国际象棋程序。现在,我已经把棋盘和现在的棋子放在一起了,我可以用鼠标拖拽来移动 我需要的是给两边的正方形添加坐标,就像在一块真正的木板上一样。不需要任何花哨的东西,只是视觉效果。由于我不是用图形绘制板,我不知道如何在我创建的板上绘制:/ import java.awt.*; import java.awt.event.*; import java.util.*; import javax.swing.*; public class ChessGame extends
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);
}
}
import java.awt.*;
导入java.awt.event.*;
导入java.util.*;
导入javax.swing.*;
公共类棋类游戏扩展JFrame实现MouseStener,
MouseMotionListener{
JLayeredPane layeredPane;
JPanel棋盘;
JLabel棋子;
国际协调;
国际协调;
公共棋局{
尺寸板尺寸=新尺寸(600600);
layeredPane=新的JLayeredPane();
getContentPane().add(layeredPane);
layeredPane.setPreferredSize(boardSize);
layeredPane.addMouseListener(此);
layeredPane.addMouseMotionListener(此);
//添加棋盘
棋盘=新JPanel();
layeredPane.add(棋盘,JLayeredPane.DEFAULT_层);
棋盘布局(新网格布局(8,8));
棋盘。设置首选大小(棋盘大小);
棋盘.立根(0,0,boardSize.width,boardSize.height);
对于(int i=0;i<64;i++){
JPanel square=newjpanel(newborderlayout());
棋盘。加(正方形);
int行=(i/8)%2;
如果(行==0)
正方形。退根(i%2==0?新颜色(238221187)
:新颜色(204、136、68);
其他的
正方形。背景(i%2==0?新颜色(20413668)
:新颜色(238221187);
}
//黑板上的黑色碎片
JLabel片段=新JLabel(新图像图标(getClass().getResource(
“Rooka8.png”);
JPanel面板=(JPanel)chessBoard.getComponent(0);
面板。添加(件);
piece=newjlabel(newimageicon(getClass().getResource(“Knightb8.png”));
panel=(JPanel)chessBoard.getComponent(1);
面板。添加(件);
piece=newjlabel(newimageicon(getClass().getResource(“Bishopc8.png”));
panel=(JPanel)chessBoard.getComponent(2);
面板。添加(件);
piece=newjlabel(newimageicon(getClass().getResource(“Queend8.png”));
panel=(JPanel)chessBoard.getComponent(3);
面板。添加(件);
piece=newjlabel(newimageicon(getClass().getResource(“Kinge8.png”));
panel=(JPanel)chessBoard.getComponent(4);
面板。添加(件);
工件=新的JLabel(新的图像图标(getClass().getResource(“Bishopf8.png”));
panel=(JPanel)chessBoard.getComponent(5);
面板。添加(件);
piece=newjlabel(newimageicon(getClass().getResource(“Knightg8.png”));
panel=(JPanel)chessBoard.getComponent(6);
面板。添加(件);
piece=newjlabel(newimageicon(getClass().getResource(“Rookh8.png”));
panel=(JPanel)chessBoard.getComponent(7);
面板。添加(件);
piece=newjlabel(newimageicon(getClass().getResource(“Pawna7.png”));
panel=(JPanel)chessBoard.getComponent(8);
面板。添加(件);
piece=newjlabel(newimageicon(getClass().getResource(“Pb7.png”));
panel=(JPanel)chessBoard.getComponent(9);
面板。添加(件);
piece=newjlabel(newimageicon(getClass().getResource(“Pawnc7.png”));
panel=(JPanel)chessBoard.getComponent(10);
面板。添加(件);
piece=newjlabel(newimageicon(getClass().getResource(“Pawnd7.png”));
panel=(JPanel)chessBoard.getComponent(11);
面板。添加(件);
piece=newjlabel(newimageicon(getClass().getResource(“Pawne7.png”));
panel=(JPanel)chessBoard.getComponent(12);
面板。添加(件);
piece=newjlabel(newimageicon(getClass().getResource(“Pawnf7.png”));
panel=(JPanel)chessBoard.getComponent(13);
面板。添加(件);
piece=newjlabel(newimageicon(getClass().getResource(“Pawng7.png”));
panel=(JPanel)chessBoard.getComponent(14);
面板。添加(件);
piece=newjlabel(newimageicon(getClass().getResource(“Pawnh7.png”));
panel=(JPanel)chessBoard.getComponent(15);
面板。添加(件);
//白板
piece=newjlabel(newimageicon(getClass().getResource(“Pawna2.png”));
panel=(JPanel)chessBoard.getComponent(48);
面板。添加(件);
piece=newjlabel(newimageicon(getClass().getResource(“Pawnb2.png”));
panel=(JPanel)chessBoard.getComponent(49);
面板。添加(件);
piece=newjlabel(newimageicon(getClass().getResource(“Pawnc2.png”));
panel=(JPanel)chessBoard.getComponent(50);
面板。添加(件);
piece=newjlabel(newimageicon(getClass().getResource(“Pawnd2.png”));
panel=(JPanel)chessBoard.getComponent(51);
面板。添加(件);
piece=newjlabel(newimageicon(getClass().getResource(“Pawne2.png”));
panel=(JPanel)chessBoard.getComponent(52);
面板。添加(件);
piece=newjlabel(newimageicon(getClass().getResource(“Pawnf2.png”));
panel=(JPanel)chessBoard.getComponent(53);
面板。添加(件);
piece=newjlabel(newimageicon(getClass().getResource(“Pawng2.png”));
panel=(JPanel)chessBoard.getComponent(54);
面板。添加(件);
piece=newjlabel(newimageicon(getClass().getResource(“Pawnh2.png”));
panel=(JPanel)chessBoard.getComponent(55);
面板。添加(件);
piece=newjlabel(newimageicon(getClass().getResource(“Rooka1.png”));
panel=(JPanel)chessBoard.getComponent(56);
面板。添加(件);
piece=newjlabel(newimageicon(getClass().getResource(“Knightb1.png”));
panel=(JPanel)chessBoard.getComponent(57);
面板。添加(件);
piece=newjlabel(newimageicon(getClass().getResource(“Bishopc1.png”));
panel=(JPanel)ches
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();
}
});
}
}