如何在 Windows 10 中使 drawString 更快?

How can I make drawString faster in Windows 10?

我的旧应用程序严重依赖 drawString。 Windows 至少慢了 20 倍 10. 有什么方法可以加快 Windows 中绘制文本的速度吗?以下代码说明了差异。

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

public class hello{

// hello takes roughly 1 second in Ubuntu 20.04 SDK 11.
// hello takes roughly 2 seconds in Windows 7 Java SDK 1.7.
// hello takes roughly 40 seconds in Windows 10 Java SDK 16.

 public static void main(String[] args) {
        JPanel panel= new JPanel();
        JFrame frame = new JFrame();
        frame.add(panel);
        frame.pack();
        frame.setSize(100,100);
        frame.setVisible(true);
        Graphics g=panel.getGraphics();
        g.setFont(new Font("Arial", Font.PLAIN,12));

        long start_time=System.currentTimeMillis();
        for (int times=0;times<150000;times++)
            g.drawString("hello",50,50);

       System.out.println("total drawString time was "+(System.currentTimeMillis()-start_time));
       System.exit(0);
    }
}

正确完成绘画只需要大约 100ms:

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

public class SSCCE extends JPanel
{
    public SSCCE()
    {
    }

    @Override
    protected void paintComponent(Graphics g)
    {
        super.paintComponent(g);

        g.setFont(new Font("Arial", Font.PLAIN,12));

        long start_time=System.currentTimeMillis();
        for (int times=0;times<150000;times++)
            g.drawString("hello",50,50);

       System.out.println("total drawString time was "+(System.currentTimeMillis()-start_time));
    }

    private static void createAndShowGUI()
    {
        JFrame frame = new JFrame("SSCCE");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new SSCCE());
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//      frame.pack();
        frame.setSize(300, 100);
        frame.setLocationByPlatform( true );
        frame.setVisible( true );
    }

    public static void main(String[] args) throws Exception
    {
        java.awt.EventQueue.invokeLater( () -> createAndShowGUI() );
    }
}

我在 Windows 10.

上使用 JDK11