我正在尝试使用 Java 绘制棋盘。我是 Java 新手。因此,任何建议都会有所帮助。
更新:我在 main 方法中添加了。我在Mac 终端编译成功。但是,当我这样做时java Checkerboard
,底部出现了一个ICON,然后它消失了,没有出现任何图形。这里有什么问题?代码如下:
import acm.graphics.*;
import acm.program.*;
/*
* This class draws a checkerboard on the graphics window.
* The size of the chcekerboard is specified by the constants NROWS
* and NCOLUMNS, and the checkerboard fills the vertical space available.
*/
public class Checkerboard extends GraphicsProgram {
public static void main(String[] args){
Checkerboard c = new Checkerboard();
c.run();
}
// Number of rows
private static final int NROWS = 8;
//Number of columns
private static final int NCOLUMNS = 8;
//Runs the program
public void run() {
int sqSize = getHeight() / NROWS;
for(int i = 0; i < NROWS; i++) {
for(int j = 0; j < NCOLUMNS ; j++) {
int x = j * sqSize;
int y = i * sqSize;
GRect sq = new GRect(x,y,sqSize,sqSize);
sq.setFilled( ((i+j) % 2) != 0);
add(sq);
}
}
}
}