简体   繁体   English

使用匿名类中的图形

[英]Using Graphics from anonymous class

This is a part of my code which does some sort of animation; 这是我的代码的一部分,做了一些动画处理。 however, there seems to be something wrong: Whenever I try to use the passed 'g' from inside the anonymous class to draw anything, it does nothing, yet when I used it outside the anonymous class (inside the rollBalls method) it does what it's supposed to do. 但是,似乎有问题:每当我尝试使用匿名类内部传递的'g'绘制任何内容时,它都不会执行任何操作,但是当我在匿名类外部(在rollBalls方法内部)使用它时,它会执行什么操作应该做的。 Any idea why? 知道为什么吗? And how do I fix this? 我该如何解决呢? Thank you. 谢谢。

   protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        rollBalls(g);
    }


    private void rollBalls(final Graphics g) { //Roll all 3 balls on the bar
        new Timer(1, new ActionListener() { 
            @Override
            public void actionPerformed(ActionEvent e) {
                                   g.setColor(Color.red);
                                   g.fillRect(0, 0, 500, 500);
            }
        }).start();
    }

I agree with Hovercraft Full Of Eels's comment. 我同意“气垫船充满鳗鱼”的评论。 you should do something like this 你应该做这样的事情

class MyClass extends JComponent{

MyClass(){
new Timer(1, new ActionListener() { 
            @Override
            public void actionPerformed(ActionEvent e) {
                 MyClass.this.repaint();
            }
        }).start();
}

 protected void paintComponent(Graphics g) {
         super.paintComponent(g);
         g.setColor(Color.red);
         g.fillRect(0, 0, 500, 500);
    }

Your problem is several fold but first and foremost you understand that the Graphics object passed into a paint or paintComponent method usually does not persist and may be disposed of after the method completes. 您的问题有很多方面,但是首先您要了解的是,传递给paint或paintComponent方法的Graphics对象通常不会持久存在,并且可以在方法完成后进行处理。 Next you have program logic being called from within a paintComponent method which should never be done. 接下来,您需要从paintComponent方法中调用程序逻辑,这是绝对不能做的。 You do not have full control of when or even if the paint or paintComponent methods are called and thus should not have it dictating your app's logic. 您无法完全控制何时或什至是否调用paint或paintComponent方法,因此不应由它决定应用程序的逻辑。

For this reason you do not do Swing graphics in this way. 因此,您不能以这种方式制作Swing图形。 Instead, have your timer outside of any paintComponent method, have it update class fields, have it then call repaint() and have your paintComponent use these class fields to do drawing as needed and this time with a stable Graphics object that is passed into it via its parameters . 而是让您的计时器位于任何paintComponent方法之外,让其更新类字段,然后让它调用repaint(),并让paintComponent根据需要使用这些类字段进行绘制,这一次是将稳定的Graphics对象传递给它通过其参数

I understand that your code is "just a sample", but your doing things wrong by trying to paint directly from within actionPerformed. 我了解您的代码只是“一个示例”,但是您尝试通过直接在actionPerformed内部绘制来做错事。 You simply shouldn't do this. 您根本不应该这样做。

Maybe living on the wrong thread for GUI update? 也许生活在错误的GUI更新线程上? Try putting the drawing commands in the anonymous class into a Runnable and pass that to SwingUtilities.invokeLater. 尝试将匿名类中的绘图命令放入Runnable中,并将其传递给SwingUtilities.invokeLater。

I don't think it has anything to do with inner-class-ness. 我认为这与内在阶级无关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM