如何在GUI程序中绘制500行?

时间:2016-11-03 15:27:57

标签: java user-interface

这是我的代码,但它只创建一行:

    public void paint(Graphics page)
    {
        super.paint(page);  
        int r = (int)(Math.random() * 256);
        int g = (int)(Math.random() * 256);
        int b = (int)(Math.random() * 256);
        int x1 = (int)(Math.random() * 501);
        int x2 = (int)(Math.random() * 501);
        int y1 = (int)(Math.random() * 501);
        int y2 = (int)(Math.random() * 501);
        Color myColor = new Color(r, g, b);
        page.setColor(myColor);

        while (x1 < 501 && x2 < 501 && y1 <501 && y2 <501)
        {
//I believe this is where the error is but i'm not sure.    

page.drawLine(x1, y1, x2, y2);
        }           
    }

    // main method
    public static void main(String[] args)
    {
        GUI07 app = new GUI07();  // run program
    }
}

1 个答案:

答案 0 :(得分:0)

添加外循环。摆脱内在的。

 public void paint(Graphics page)
 {
    super.paint(page);  
    for (int i = 0; i < 500; i++) {
        int r = (int)(Math.random() * 256);
        int g = (int)(Math.random() * 256);
        int b = (int)(Math.random() * 256);
        int x1 = (int)(Math.random() * 501);
        int x2 = (int)(Math.random() * 501);
        int y1 = (int)(Math.random() * 501);
        int y2 = (int)(Math.random() * 501);
        Color myColor = new Color(r, g, b);
        page.setColor(myColor);

        //I believe this is where the error is but i'm not sure.    

        page.drawLine(x1, y1, x2, y2);                    
    }
 }

 // main method
 public static void main(String[] args)
 {
     GUI07 app = new GUI07();  // run program
 }
}