0

我正在尝试对组件进行增量显示,因为进行所有计算需要花费太多时间。所以我不想冻结图形界面,我想每 2.3 秒显示一次我的图像(分形)。计算所有点的函数是计算。在我想做增量显示之前,这个方法是计算所有点。现在它只计算 10000 个点。

FlameBuilderPreviewComponent

Timer timer1=new Timer(1000,new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub
            time=time+10000;
            fa= builder.build().compute(density,frame,getWidth(),getHeight(),accumulator);

        }

    });
    timer1.start();
    for (int z = 0; z <fa.height(); z++) {
        for (int j = 0; j < fa.width(); j++) {
            image.setRGB(j,z,fa.color(palette, background, j, fa.height()-1-z).asPackedRGB());
        }
    }
    g0.drawImage(image,0,0,null);
    if (time>density*getWidth()*getHeight()){
        timer1.stop();
    }

然后程序的另一部分是GUI界面,我放了另一个计时器,这个计时器负责重新绘制图像。

FlameMakerGUI

    fBPC=new FlameBuilderPreviewComponent(builder, background, palette, r1, density);
    Timer timer = new Timer(2500,new ActionListener(){
        public void actionPerformed(ActionEvent e) {
            fBPC.repaint();
            System.out.println("titi");
        }       
    });
    timer.start();
    fractale.add(fBPC,BorderLayout.CENTER);

然后这是程序每次执行计时器时显示的错误:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at ch.epfl.flamemaker.gui.FlameBuilderPreviewComponent.paintComponent(FlameBuilderPreviewComponent.java:82)
    at javax.swing.JComponent.paint(JComponent.java:1054)
    at javax.swing.JComponent.paintChildren(JComponent.java:887)
    at javax.swing.JComponent.paint(JComponent.java:1063)
    at javax.swing.JComponent.paintToOffscreen(JComponent.java:5221)
    at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(RepaintManager.java:1512)
    at javax.swing.RepaintManager$PaintManager.paint(RepaintManager.java:1443)
    at javax.swing.RepaintManager.paint(RepaintManager.java:1236)
    at javax.swing.JComponent._paintImmediately(JComponent.java:5169)
    at javax.swing.JComponent.paintImmediately(JComponent.java:4980)
    at javax.swing.RepaintManager$3.run(RepaintManager.java:796)
    at javax.swing.RepaintManager$3.run(RepaintManager.java:784)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:784)
    at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:757)
    at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:706)
    at javax.swing.RepaintManager.access$1000(RepaintManager.java:62)
    at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1651)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:727)
    at java.awt.EventQueue.access$200(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:688)
    at java.awt.EventQueue$3.run(EventQueue.java:686)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:697)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

谢谢你的帮忙

4

1 回答 1

1

您每秒钟都在创建一个动作侦听器,这没有任何意义。您显然不了解事件侦听器的工作原理或Timer类的工作原理。

此外,(事件调度线程)上的任务必须快速完成;如果他们不这样做,未处理的事件会备份并且用户界面变得无响应EDT

您需要使用Swing Worker来归档正确的并发性

从甲骨文网站:

Swing 由三种线程组成:

  • 初始线程,执行初始应用程序代码的线程。

  • 事件调度线程,所有事件处理代码都在其中执行。大多数与 Swing 框架交互的代码也必须在这个线程上执行。

  • 工作线程,也称为后台线程,执行耗时的后台任务。

于 2013-05-25T16:35:01.960 回答