我试图用Java创建一个国际象棋程序。现在,我把电路板与现有的电池一起完成,我可以通过拖放移动鼠标。
我需要的是在两侧的方块上添加坐标,就像在真正的板上一样。不必是任何花哨的东西,只是视觉上的东西。由于我不使用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);
}
}
答案 0 :(得分:5)
修改强>
编辑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();
}
});
}
}
显示为:
答案 1 :(得分:0)
从棋盘返回的面板应该是扩展JPanel的自定义类,在那里你需要覆盖paintComponent()。