How do I speed up this swing animation?

[亡魂溺海] 提交于 2019-11-29 16:21:12
trashgod

Here are a few suggestions:

  • Critically examine the need to repaint() every millisecond; in particular, see if some updates may be coalesced.

  • Consider the convenience of java.swing.Timer, which renders on the EDT and supports coalescing events.

  • Calls to drawImage() are fastest when no scaling is required, as shown in this AnimationTest.

  • Always pre-compute images to the extent possible, as suggested in this KineticModel that illustrates several animation techniques.

  • TexturePaint, used in KineticModel, is also shown here.

  • IndexColorModel, illustrated here, may be applicable.

  • An sscce will allow you to isolate various approaches for easier profiling.

EDITED TO BETTER EXAMPLE

Essentially we can use the RepaintManager to keep track of/update the few pixels that we change every millisecond. The dirty region will accumulate until the paint is actually able to happen. When the paint happens, it uses the paintImmediately(int x, int y, int w, int h) function (unless the whole component is dirty) - so we get away with just updating a small portion of the image. Hopefully the sample code isn't OS dependent - let me know if it doesn't work for you.

import java.awt.*;
import java.awt.image.BufferedImage;

import javax.swing.*;

public class UpdatePane extends JPanel{
    final int MAX = 1024;

    //The repaint manager in charge of determining dirty pixels
    RepaintManager paintManager = RepaintManager.currentManager(this);

    //Master image
    BufferedImage img = new BufferedImage(MAX, MAX, BufferedImage.TYPE_INT_RGB);


    @Override
    public void paintComponent(Graphics g){
        g.drawImage(img, 0, 0, null);
    }

    public void paintImmediately(int x, int y, int w, int h){
        BufferedImage img2 = img.getSubimage(x, y, w, h);
        getGraphics().drawImage(img2, x, y, null);
    }

    public static void main(String... args) {
        SwingUtilities.invokeLater(new Runnable(){
            @Override
            public void run() {
                final UpdatePane outside = new UpdatePane(); 

                outside.setPreferredSize(new Dimension(1024,1024));

                JFrame frame = new JFrame();
                frame.add(outside);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.pack();
                frame.setVisible(true);

                outside.new Worker().execute();
            }
        });
        }

    //Goes through updating pixels (like your application would)
    public class Worker extends SwingWorker<Integer, Integer>{
        int currentColor = Color.black.getRGB();
        public int x = 0;
        public int y = 0;
        @Override
        protected Integer doInBackground() throws Exception {
            while(true){
                if(x < MAX-1){
                    x++;
                } else if(x >= MAX-1 && y < MAX-1){
                    y++;
                    x = 0;
                } else{
                    y = 0;
                    x = 0;
                }

                if(currentColor < 256){
                    currentColor++;
                } else{
                    currentColor = 0;
                }

                img.setRGB(x, y, currentColor); 

                //Tells which portion needs to be repainted [will call paintImmediately(int x, int y, int w, int h)]
                paintManager.addDirtyRegion(UpdatePane.this, x, y, 1, 1);
                Thread.sleep(1);
            }
        } 

    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!