如何在Windows 10中使drawString更快?



我的旧应用程序严重依赖于drawString。在Windows 10中至少要慢20倍。有没有办法加快在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。

相关内容

  • 没有找到相关文章

最新更新