[英]Java: Mac terminal cannot display Java Graphics
I am trying to draw checkerboard using Java. 我正在尝试使用Java绘制棋盘格。 I am new to Java.
我是Java新手。 So any advices would be helpful.
所以任何建议都会有所帮助。
UPDATE: I added in the main method. 更新:我在main方法中添加了。 I compiled it successfully in the Mac terminal .
我在Mac终端上成功编译了它。 However, when I did
java Checkerboard
, there was an ICON appearing at the bottom and then it disappeared and no graphics appeared. 然而,当我做了
java Checkerboard
,底部出现了一个ICON然后消失了,没有出现任何图形。 What's wrong in here? 这里有什么问题? The code is as follow:
代码如下:
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);
}
}
}
}
You seem to be lacking the main method: public static void main(String[] args)
that is run when you start your program. 您似乎缺少main方法:
public static void main(String[] args)
在您启动程序时运行。
(removed the edit I did before, which was meant to go in my own post) (删除了我以前做的编辑,这本来是我自己的帖子)
Your class needs to have a main method with the signature 您的类需要具有签名的main方法
public static void main(String[] args)
for you to be able to run it. 为了你能够运行它。
After your edit: 编辑后:
Maybe you need a loop in the main method calling the run method? 也许你需要在main方法中调用run方法的循环? Something like:
就像是:
boolean exit = false;
while (!exit) {
c.run();
// if something set exit to true
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.