使用匿名类中的图形

发布于 2024-11-03 10:42:53 字数 640 浏览 1 评论 0原文

这是我的代码的一部分,它执行某种动画;但是,似乎有问题:每当我尝试使用匿名类内部传递的“g”来绘制任何内容时,它都不会执行任何操作,但是当我在匿名类外部(在 rollBalls 方法内部)使用它时,它会执行以下操作这是应该做的。知道为什么吗?我该如何解决这个问题?谢谢。

   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();
    }

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. 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();
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

酸甜透明夹心 2024-11-10 10:42:53

您的问题有好几个方面,但首先您要了解传递给 Paint 或 PaintComponent 方法的 Graphics 对象通常不会持久存在,并且可能会在该方法完成后被丢弃。接下来,您将在 PaintComponent 方法中调用程序逻辑,而这是永远不应该执行的。您无法完全控制何时或即使调用paint或paintComponent方法,因此不应让它决定您的应用程序的逻辑。

因此,您不要以这种方式制作 Swing 图形。相反,让你的计时器在任何 PaintComponent 方法之外,让它更新类字段,然后让它调用 repaint() 并让你的 PaintComponent 使用这些类字段根据需要进行绘制,这次使用一个稳定的 Graphics 对象,该对象是通过其参数传递给它。

我知道您的代码“只是一个示例”,但是您尝试直接从 actionPerformed 中进行绘制而做错了事情。你根本不应该这样做。

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. Next you have program logic being called from within a paintComponent method which should never be done. 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.

For this reason you do not do Swing graphics in this way. 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.

I understand that your code is "just a sample", but your doing things wrong by trying to paint directly from within actionPerformed. You simply shouldn't do this.

无远思近则忧 2024-11-10 10:42:53

我同意充满鳗鱼的气垫船的评论。你应该做这样的事情

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);
    }

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);
    }
桃扇骨 2024-11-10 10:42:53

也许是在错误的 GUI 更新线程上?尝试将匿名类中的绘图命令放入 Runnable 中,并将其传递给 SwingUtilities.invokeLater。

我不认为这与内部阶级有任何关系。

Maybe living on the wrong thread for GUI update? Try putting the drawing commands in the anonymous class into a Runnable and pass that to SwingUtilities.invokeLater.

I don't think it has anything to do with inner-class-ness.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文