2013-10-30 40 views
0

我想用Java繪製棋盤。 我是Java新手。所以任何建議都會有幫助。Java:Mac終端無法顯示Java圖形

更新:我在主要方法中添加。我在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); 
      } 
     } 
    } 
} 
+0

相關:http://stackoverflow.com/q/3442971/335858 – dasblinkenlight

+0

我沒有使用Ecilpse。我正在使用mac的終端 – mynameisJEFF

回答

1

你似乎缺乏的主要方法:public static void main(String[] args)當你開始你的程序運行。

(除去編輯我之前,這本來是在我自己的崗位去)

1

你的類需要有簽名

主要方法讓你能夠運行。

您的編輯後:

也許你需要在調用run方法的主要方法的循環?例如:

boolean exit = false; 
while (!exit) { 
    c.run(); 
    // if something set exit to true 
} 
+0

我試了一下。它仍然不起作用。 – mynameisJEFF

+0

你可以用你現在得到的代碼更新代碼,也許可以添加一些調試行('System.out.println(「一些文本」);')併發布控制檯輸出? :) –

+0

(另外,對我和kviiri的投票會很好,因爲我們都發布瞭解決方案到您的原始問題;)(雖然「接受」可能不合理,因爲你有更多的問題)。 –