用JAVA绘制一个黑白棋牌(一个大正方形被分为若干小正方形,黑色小正方形和白色正方形交错)棋盘上下侧分别为文字北方参战者和南方参战者,左右两侧分别为文字西方观察团和东方观察团,
import javax.swing.*;
import java.awt.*;
public class Frame extends JFrame {
private static final int BOARD_SIZE = 8;
private static final int SQUARE_SIZE = 50;
public Frame() {
setTitle("Black and White Chessboard");
setSize(BOARD_SIZE * SQUARE_SIZE, BOARD_SIZE * SQUARE_SIZE);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setResizable(false);
JPanel panel = new JPanel() {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
drawChessboard(g);
drawLabels(g);
}
};
add(panel);
}
private void drawChessboard(Graphics g) {
g.setColor(Color.WHITE);
g.fillRect(0, 0, BOARD_SIZE * SQUARE_SIZE, BOARD_SIZE * SQUARE_SIZE);
for (int row = 0; row < BOARD_SIZE; row++) {
for (int col = 0; col < BOARD_SIZE; col++) {
if ((row + col) % 2 == 0) {
g.setColor(Color.BLACK);
} else {
g.setColor(Color.WHITE);
}
g.fillRect(col * SQUARE_SIZE, row * SQUARE_SIZE, SQUARE_SIZE, SQUARE_SIZE);
}
}
}
private void drawLabels(Graphics g) {
g.setColor(Color.BLACK);
//g.setFont(new Font("Arial", Font.BOLD, 16));
g.drawString("北方参战者", (BOARD_SIZE * SQUARE_SIZE - g.getFontMetrics().stringWidth("北方参战者")) / 2, 20);
g.drawString("南方参战者", (BOARD_SIZE * SQUARE_SIZE - g.getFontMetrics().stringWidth("南方参战者")) / 2, BOARD_SIZE * SQUARE_SIZE - 10);
Graphics2D g2d = (Graphics2D) g;
g2d.rotate(Math.toRadians(90));
g2d.drawString("西方观察团", (BOARD_SIZE * SQUARE_SIZE - g.getFontMetrics().stringWidth("西方观察团")) / 2, -10);
g2d.drawString("东方观察团", (BOARD_SIZE * SQUARE_SIZE - g.getFontMetrics().stringWidth("东方观察团")) / 2, -(BOARD_SIZE * SQUARE_SIZE) + 20);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
Frame chessboard = new Frame();
chessboard.setVisible(true);
});
}
}
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Chessboard extends JPanel {
private static final int BOARD_SIZE = 8;
private static final int SQUARE_SIZE = 60;
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
// 绘制棋盘背景
g.setColor(Color.WHITE);
g.fillRect(0, 0, BOARD_SIZE * SQUARE_SIZE, BOARD_SIZE * SQUARE_SIZE);
// 绘制棋盘格子
boolean isBlackSquare = false;
for (int row = 0; row < BOARD_SIZE; row++) {
for (int col = 0; col < BOARD_SIZE; col++) {
if (isBlackSquare) {
g.setColor(Color.BLACK);
} else {
g.setColor(Color.WHITE);
}
g.fillRect(col * SQUARE_SIZE, row * SQUARE_SIZE, SQUARE_SIZE, SQUARE_SIZE);
isBlackSquare = !isBlackSquare;
}
isBlackSquare = !isBlackSquare;
}
// 绘制文字
g.setFont(new Font("Arial", Font.BOLD, 20));
g.setColor(Color.BLACK);
// 绘制北方参战者和南方参战者
g.drawString("北方参战者", 0, (BOARD_SIZE * SQUARE_SIZE) + 30);
g.drawString("南方参战者", 0, -10);
// 绘制西方观察团和东方观察团
g.drawString("西方观察团", -70, (BOARD_SIZE * SQUARE_SIZE) / 2);
g.drawString("东方观察团", (BOARD_SIZE * SQUARE_SIZE) + 10, (BOARD_SIZE * SQUARE_SIZE) / 2);
}
public static void main(String[] args) {
JFrame frame = new JFrame("Chessboard");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(BOARD_SIZE * SQUARE_SIZE, BOARD_SIZE * SQUARE_SIZE + 60);
frame.add(new Chessboard());
frame.setVisible(true);
}
}