2015-04-02 67 views
-2

我需要做一個循環與paintComponentJava的塗料成份環

像這樣

public class Screen extends JComponent 
{ 

    public void paintComponent(Graphics g) 
    { 
     Graphics2D g2d = (Graphics2D) g; 
     g.fillOval(Main.x, Main.y, 20, 20); 

    } 
    { 
    for(int c = 0; c<10; c++) 
    { 

     paintComponent(g); 
    } 

    } 

的方法是在擴展JComponent類。

在for循環的Java給我一個錯誤

g不能被解析爲一個變量

任何人都可以幫助嗎?

謝謝!

+0

請問您可以發佈完整的代碼片段,例如'for'循環本身並不意味着什麼,周圍代碼在'for'循環中的位置會給我們更清晰的圖片 – 2015-04-02 11:07:59

+0

我發佈了,這是一個非常小的類 – thevilcode 2015-04-02 11:13:49

+0

它只是paintComponent方法和for循環 – thevilcode 2015-04-02 11:15:05

回答

2

你的循環是不是塊或方法中封閉:

public void paintComponent(Graphics g) 
    { 
     Graphics2D g2d = (Graphics2D) g; 
     g.fillOval(Main.x, Main.y, 20, 20); 

    }// method closed here itself 

    //{ 
    //for(int c = 0; c<10; c++) 
    //{ 
     //paintComponent(g);//here `g` is not declared 
    //} 
+0

Java給我一個錯誤 – thevilcode 2015-04-02 11:53:04

+0

「線程中的異常」AWT-EventQueue-0「java.lang.StackOverflowError」 – thevilcode 2015-04-02 11:53:12

0

你不能叫paintComponent(Graphics g)這就是爲什麼我們已經重繪()方法,你應該知道,圖形是不能創造的 例如一個抽象類它。 另一件事,我假設你試圖讓一個動畫球能夠做到這一點,你應該先閱讀有關擺動計時器。我也在學習java,所以繼續嘗試。