2015-04-02 57 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,所以继续尝试。