【发布时间】:2013-10-15 18:25:39
【问题描述】:
我正在为我的班级做另一个 Java 项目。在这个任务中,我们必须创建一个棋盘并用适当数量的棋子填充它。我正确构建了显示良好的棋盘格,但我很难使用 Graphics 类进行绘制。
这是我目前的代码:
import javax.swing.*;
import java.awt.*;
public class Checkerboard extends JFrame {
//Define the default values for the separate checker pieces
private final int RED_PIECE = 0;
private final int BLACK_PIECE = 1;
/** Construct the default checker board */
public Checkerboard() {
this.setSize(600, 600);
this.setResizable(false);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
this.setTitle("Checkerboard Lab");
this.setLayout(new GridLayout(8, 8));
this.setVisible(true);
for (int a=0; a<2; a++) {
for (int i=0; i<4; i++) {
add(new WhiteSpace());
add(new GraySpace(RED_PIECE));
}
for (int j=0; j<4; j++) {
add(new GraySpace(RED_PIECE));
add(new WhiteSpace());
}
}
for (int b=0; b<2; b++) {
for (int k=0; k<4; k++) {
add(new WhiteSpace());
add(new GraySpace(RED_PIECE));
}
for (int l=0; l<4; l++) {
add(new GraySpace());
add(new WhiteSpace());
}
}
for (int c=0; c<2; c++) {
for (int m=0; m<4; m++) {
add(new GraySpace());
add(new WhiteSpace());
}
for (int n=0; n<4; n++) {
add(new GraySpace(BLACK_PIECE));
add(new WhiteSpace());
}
}
for (int d=0; d<2; d++) {
for (int o=0; o<4; o++) {
add(new WhiteSpace());
add(new GraySpace(BLACK_PIECE));
}
for (int p=0; p<4; p++) {
add(new GraySpace(BLACK_PIECE));
add(new WhiteSpace());
}
}
}
/** White Space constructor */
public class WhiteSpace extends JPanel {
public WhiteSpace() {
setBackground(Color.WHITE); //Sets the panel's background color to white
}
}
/** Gray Space constructor */
/* GraySpace is a little different, since this color space is the only space that will be holding checker
* pieces. There is a default constructor to create a space without a checker piece on it, and another
* constructor that places either a red or black piece on the space, pending an optional parameter.*/
public class GraySpace extends JPanel {
//Initial variable for the checker piece
int checkerPiece;
//Default GraySpace constructor
public GraySpace() {
setBackground(Color.LIGHT_GRAY);
}
//The GraySpace constructor with the optional parameter to determine if it holds a checker piece
public GraySpace(int piece) {
this.checkerPiece = piece;
setBackground(Color.LIGHT_GRAY); //Sets the panel's background color to white
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
//Default width and height variables
int width = getWidth() -10;
int height = getHeight() - 10;
//This switch statement determines which checker piece type appears on the square
switch (checkerPiece) {
case RED_PIECE:
g.setColor(Color.RED);
g.fillOval(5, 5, width, height);
break;
case BLACK_PIECE:
g.setColor(Color.BLACK);
g.fillOval(5, 5, width, height);
break;
}
}
}
/** Initiate the Checker board */
public static void main(String[] args) {
JFrame checkerboard = new Checkerboard();
}
}
这很简单。我的 Checkerboard 类是 JFrame 的子类,我将彩色面板放在 8x8 正方形中。面板是 Checkerboard 类的内部类,每个都扩展 JPanel(分别为 WhiteSpace 和 GraySpace)。由于 GraySpace 是唯一必须拥有检查器的类,因此我认为我只需将 Graphics 代码放入 GraySpace 内部类。
无论如何,对于我的问题:我该如何使用 Graphics 来绘制它?我知道我必须显式声明 paintComponent() 方法才能绘制圆,但我不知道如何指定 GraySpace 的尺寸以便有效绘制。有什么建议吗?
编辑:新问题!
好的,所以我弄清楚了如何将棋子添加到我的棋盘上,而且效果很好。我的 GraySpace 内部类有一个可选的构造函数,它接受 int 值,并据此确定 GraySpace 面板上的颜色块。我对此进行了测试,它确实有效。
然而,我的问题实际上是将棋子放到棋盘上。棋盘必须代表“默认”棋盘游戏,所有可用棋子都在棋盘上。所以,三排红色棋子,三排黑色棋子,用两排空隔开。到目前为止,我有 4 个单独的 for 循环在板上一次绘制两行......但它不能正常工作。有什么建议吗?最新的源代码在上面,替换了我的老问题源代码。再次感谢您的建议!
【问题讨论】:
标签: java user-interface swing