为什么Java绘制中的paint()方法比我设置的更多

时间:2017-08-22 00:55:55

标签: java swing paint

如果我通过直接方式设置(eggsetOval)我在窗口上获得一个Oval但是如果我在内部创建g.setOval设置Object我有时会在窗口(2,3或4)上获得多个Oval,请查看代码。

- 为什么这可以解释我? - 如何在我的Object中创建Graphics并在一次调用Object后得到一个Oval。 (没有2,3或4)

public class Obj {

    Random rand;
    private int x, y;

    public void paintc (Graphics g,int x, int y) {
        g.fillOval(x,y,20,20);
        System.out.println(x + " : " +y);
    }
}


public class Main extends JPanel{

    private Obj obj;
    Random rand;

        public Main () {
            obj = new Obj();
        }

        public void paint(Graphics g) {
            rand = new Random();
            int x = rand.nextInt(300 - 20);
            int y = rand.nextInt(200 - 20);

            obj.paintc(g,x,y);              //by this way I sometimes get more than one Oval why????
            g.fillOval(x,y,20,20);          //by this way I get one oval It is alright
        }

        public static void main(String[] args) {
            JFrame frame = new JFrame();
            frame.setSize(300,200);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
            frame.add(new Main());
        }
    }

0 个答案:

没有答案
相关问题