Java - >油漆时不能关闭窗口

时间:2017-04-07 19:44:01

标签: java paint paintcomponent

我不能在绘画时关闭窗口(我每隔1/10秒循环绘画1000次)。

计划守则:

public class myCanvas extends Canvas{
    private static final long serialVersionUID = 1L;
    private int x = 0;
    private int y = 0;
    private int i = 0;
    private int innerW;
    private int innerH;
    JFrame f;
    myCanvas() {
        super();
        f = new JFrame();
        f.setSize(500,500);
        f.setBackground(Color.blue);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);;
        f.setLayout(new GridLayout());;
        f.add(this);
        f.setVisible(true);
        for(i = 0; i<1000; i++) {
            innerW = f.getWidth() - f.getInsets().left - f.getInsets().right;
            innerH = f.getHeight() - f.getInsets().top - f.getInsets().bottom;
            x = (int) (Math.random() * innerW);
            y = (int) (Math.random() * innerH);
            try {
                TimeUnit.MILLISECONDS.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            repaint();
        }
    }
    public void paint(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;
        super.paint(g);
        do{
            g2d.setColor(new Color((int) (Math.random() * 16777216 )));
            g2d.fillOval(x, y, 25, 25);
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        } while(i<1000);
    }
}

0 个答案:

没有答案