gpt4 book ai didi

java - java中的棋盘

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:18:12 26 4
gpt4 key购买 nike

下面是我的代码

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

public class board2 {

JFrame frame;
JPanel squares[][] = new JPanel[8][8];

public board2() {
frame = new JFrame("Simplified Chess");
frame.setSize(500, 500);
frame.setLayout(new GridLayout(8, 8));

for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
squares[i][j] = new JPanel();

if ((i + j) % 2 == 0) {
squares[i][j].setBackground(Color.black);
} else {
squares[i][j].setBackground(Color.white);
}
frame.add(squares[i][j]);
}
}

squares[0][0].add(new JLabel(new ImageIcon("rookgreen.png")));
squares[0][2].add(new JLabel(new ImageIcon("bishopgreen.png")));
squares[0][4].add(new JLabel(new ImageIcon("kinggreen.png")));
squares[0][5].add(new JLabel(new ImageIcon("bishopgreen.png")));
squares[0][7].add(new JLabel(new ImageIcon("rookgreen.png")));

squares[7][0].add(new JLabel(new ImageIcon("rookred.png")));
squares[7][2].add(new JLabel(new ImageIcon("bishopred.png")));
squares[7][4].add(new JLabel(new ImageIcon("kingred.png")));
squares[7][5].add(new JLabel(new ImageIcon("bishopred.png")));
squares[7][7].add(new JLabel(new ImageIcon("rookred.png")));

for (int i = 0; i < 8; i++) {
squares[1][i].add(new JLabel(new ImageIcon("pawngreen.png")));
squares[6][i].add(new JLabel(new ImageIcon("pawnred.png")));
}

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}

public static void main(String[] args) {
new board2();
}
}

我正在尝试创建一种类似国际象棋的游戏,我需要帮助在棋盘的所有面上放置标签,以标记 A-H 或 1-8 中的行和列。我不知道该怎么做。稍后我将添加一个功能来拖放这些片段。最好使用 JLabels 吗?不管怎样,我会把标签放在一边吗?谢谢!

最佳答案

我想提交一个使用 Unicode 字符的简单棋盘绘图示例。这个小项目涉及 3 个类。

国际象棋标签.java

import java.awt.Color;
import java.awt.Font;
import javax.swing.JLabel;
import javax.swing.SwingConstants;


public class ChessLabel extends JLabel {

Font font = new Font("Ariel", Font.PLAIN, 24);
Color bgLight = new Color(222, 184, 135);
Color bgDark = new Color(139, 69, 19);

ChessLabel(String s)
{
super(s);
}

void set(int idx, int row)
{
setFont(font);
setOpaque(true);
setBackground((idx+row)%2 == 0 ? bgDark : bgLight);
setHorizontalAlignment( SwingConstants.CENTER );
}

}

Board.java

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


public class Board extends JFrame {


//Initialise arrays to hold panels and images of the board

private ChessLabel[] labels = new ChessLabel[] {

// white
new ChessLabel("\u2656"), new ChessLabel("\u2658"), new ChessLabel("\u2657"),
new ChessLabel("\u2655"), new ChessLabel("\u2654"), new ChessLabel("\u2657"),
new ChessLabel("\u2658"), new ChessLabel("\u2656"), new ChessLabel("\u2659"),
new ChessLabel("\u2659"), new ChessLabel("\u2659"), new ChessLabel("\u2659"),
new ChessLabel("\u2659"), new ChessLabel("\u2659"), new ChessLabel("\u2659"),
new ChessLabel("\u2659"),
// empty
new ChessLabel(" "), new ChessLabel(" "), new ChessLabel(" "),
new ChessLabel(" "), new ChessLabel(" "), new ChessLabel(" "),
new ChessLabel(" "), new ChessLabel(" "), new ChessLabel(" "),
new ChessLabel(" "), new ChessLabel(" "), new ChessLabel(" "),
new ChessLabel(" "), new ChessLabel(" "), new ChessLabel(" "),
new ChessLabel(" "), new ChessLabel(" "), new ChessLabel(" "),
new ChessLabel(" "), new ChessLabel(" "), new ChessLabel(" "),
new ChessLabel(" "), new ChessLabel(" "), new ChessLabel(" "),
new ChessLabel(" "), new ChessLabel(" "), new ChessLabel(" "),
new ChessLabel(" "), new ChessLabel(" "), new ChessLabel(" "),
new ChessLabel(" "), new ChessLabel(" "),
// black
new ChessLabel("\u265F"), new ChessLabel("\u265F"), new ChessLabel("\u265F"),
new ChessLabel("\u265F"), new ChessLabel("\u265F"), new ChessLabel("\u265F"),
new ChessLabel("\u265F"), new ChessLabel("\u265F"), new ChessLabel("\u265C"),
new ChessLabel("\u265E"), new ChessLabel("\u265D"), new ChessLabel("\u265B"),
new ChessLabel("\u265A"), new ChessLabel("\u265D"), new ChessLabel("\u265E"),
new ChessLabel("\u265C")
};

public Board()
{

} // Board()

void display()
{
setTitle("Chess board with unicode images");

setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

Container contentPane = getContentPane();
GridLayout gridLayout = new GridLayout(8, 8);

contentPane.setLayout(gridLayout);

int row = -1;
for (int i = 0; i < labels.length; i++)
{
if(i % 8 == 0) row ++; // increment row number
labels[i].set(i, row);
contentPane.add(labels[i]);
} // i

setSize(600, 600);
setLocationRelativeTo(null);
setVisible(true);
} // display()

} // class Board

ChessBoardTest.java

public class ChessBoardTest {

/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
Board board = new Board();

board.display();
}

}

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com