paint组件性能不佳

问题描述 投票:0回答:1

当我填充一个矩形时:

import javax.swing.*;
import java.awt.*;


public class App extends JPanel {

    private App() {
        this.setPreferredSize(new Dimension(600, 600));
        JFrame frame = new JFrame();
        frame.add(this);
        frame.pack();
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        long start = System.currentTimeMillis();
        g.fillRect(100, 100, 100, 100);
        System.out.println(System.currentTimeMillis() - start);
    }

        public static void main(String[] args) {

        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new App();
            }
        });

    }
}

fillRect()的性能非常快,执行时间为0毫秒。

但是,当我具有完全相同的代码但使用drawString()时,如下所示:

import javax.swing.*;
import java.awt.*;


public class App extends JPanel {

    private App() {
        this.setPreferredSize(new Dimension(600, 600));
        JFrame frame = new JFrame();
        frame.add(this);
        frame.pack();
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        long start = System.currentTimeMillis();
        g.drawString("hello", 100,100);
        System.out.println(System.currentTimeMillis() - start);
    }

        public static void main(String[] args) {

        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new App();
            }
        });

    }
}

至少可以说性能很糟糕......执行大约需要 140-160 毫秒。 PaintComponent 方法似乎也执行了两次,首先我得到一个打印语句:140-160 毫秒,然后我得到打印语句:0。

但是,当我创建 BufferImage 时,然后在 BufferImage 上使用drawString(),然后在paintComponent()内部调用drawString(),如下所示:

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


public class App extends JPanel {

    private BufferedImage buffer;

    private App() {
        this.setPreferredSize(new Dimension(600, 600));
        JFrame frame = new JFrame();
        frame.add(this);
        frame.pack();
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);


        //BUFFER
        buffer = new BufferedImage(600,600,BufferedImage.TYPE_INT_ARGB);
        Graphics g = buffer.getGraphics();
        g.drawString("test",100,100);


        frame.setVisible(true);
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        long start = System.currentTimeMillis();
        g.drawString("hello", 300,300);
        System.out.println(System.currentTimeMillis() - start);
    }

       public static void main(String[] args) {

        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new App();
            }
        });

    }
}

然后paintComponent内部的drawString()具有良好的性能,执行需要0毫秒(即使我根本没有使用在paintComponent方法中创建的缓冲区)。如果我删除构造函数内部的代码drawString(),代码会再次获得糟糕的性能。

发生什么事了?

java swing
1个回答
0
投票

尝试将您的paintComponent更改为以下内容:注意后续drawString调用与第一次调用的区别。评估线程可能很棘手。我建议您完成整个应用程序,然后尝试解决可能出现的任何性能问题。在这种情况下,最好在 code review

中提出问题
  public void paintComponent(Graphics g) {
        super.paintComponent(g);
        for (int y = 50; y < 500; y+=50) {
           long start = System.currentTimeMillis();
           g.drawString("hello", 100,y);
           System.out.println(System.currentTimeMillis() - start);
        }
    }

在我的 Windows i7 上,输出是

25
0
0
0
0
0
0
0
0
© www.soinside.com 2019 - 2024. All rights reserved.