2

我正在尝试学习 2D 图形。下面的代码绘制了几个旋转的轮子。为了让它们刷新,我最后在绘制方法中插入了 repaint(1000),但我知道有时它不需要绘制。

    public void paint(Graphics g) {
        Graphics2D g2 = (Graphics2D) g;

        for(int angle=0; angle<360; angle+=90){
            g2.setColor(blue);
            g2.fillArc(100,100,200,200,theta1 + angle,45);
            g2.setColor(red);
            g2.fillArc(100,100,200,200,theta1 + angle + 45,45);
        }

        for(int angle=0; angle<360; angle+=30){
            g2.setColor(green);
            g2.fillArc(250,250,250,250,angle + theta2,15);
            g2.setColor(yellow);
            g2.fillArc(250,250,250,250,angle + theta2 + 15,15);
        }

//        repaint(1000);
   }

    public static void main(String s[]) {
        JFrame f = new JFrame("ShapesDemo2D");
        f.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {System.exit(0);}
        });
        JApplet applet = new ShapesDemo2D();
        f.getContentPane().add("Center", applet);
        applet.init();
        f.pack();
        f.setSize(new Dimension(800,800));
        f.setVisible(true);

        while(true) {
            theta2 += 5;
            theta1 -= 2;

            f.repaint(1000);

            try {
                Thread.sleep(100);
            } catch (InterruptedException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        }
    }

我真正想做的是在我做出改变后刷新它。Main 在创建小程序后就引用了paint,但 f.repaint() 似乎没有做任何事情。(如果我在paint中注释掉repaint(),它不会更新)。我究竟做错了什么?

4

1 回答 1

3

通读对您有利

我也想看看

其中都展示了 Swing 中的动画原理和自定义图形

于 2012-10-24T04:11:58.110 回答