[英]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);
}
}
正確完成繪畫僅需 100 毫秒:
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。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.