Java OS X 上的图形 drawImage 方法非常慢
Java Graphics drawImage method very slow on OS X
在 JPanel
class 中的 Graphics
对象上使用的 drawImage(image, x, y, null);
方法需要很长时间才能完成并制作游戏 运行低 FPS,特别是在 MacBook Pro 上它已经过测试 - Linux 机器似乎 运行 它非常好。所花费的时间受图像大小的影响很大。我在网上找到的许多潜在修复方法都没有帮助,例如:
Graphics.drawImage() in Java is EXTREMELY slow on some computers yet much faster on others 以及使用不同版本 Java 的建议根本没有太大区别 - 我尝试了 Java 6 和 8,因为有人建议这有时是由于 Java 7+ 版本。
经过数小时的搜索,我决定尝试创建一个全新的项目,该项目实现了 运行 在循环中使用此方法的基础知识,下面是代码 - 以防我做一些愚蠢的事情在里面!
设置JFrame的Class:
import javax.swing.JFrame;
public class Main {
public static void main(String args[]) {
JFrame f = new JFrame("Carnival Carnage");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setContentPane(new TPanel());
f.pack();
f.setVisible(true);
}
}
扩展JPanel的Class:
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import javax.swing.JPanel;
public class TPanel extends JPanel implements Runnable {
private Graphics g;
private BufferedImage image;
private Thread thread;
private int width = 1600, height = 900;
public TPanel() {
super();
setPreferredSize(new Dimension(width,height));
image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
setFocusable(true);
requestFocus();
}
private void draw() {
g = getGraphics();
long start = System.nanoTime();
g.drawImage(image, 0, 0, null);
System.out.println(System.nanoTime() - start);
}
public void run() {
while(true) {
draw();
}
}
public void addNotify(){
super.addNotify();
if(thread == null) {
thread = new Thread(this);
thread.start();
}
}
}
你有一个无限循环,将继续重新绘制图像。
您不应使用 getGraphics() 并使用该 Graphics 对象进行绘画,因为绘画不是永久性的。
自定义绘画是通过重写 JPanel 的 paintComponent()
方法完成的,然后使用传递给该方法的 Graphics 对象。
阅读 Custom Painting 上的 Swing 教程部分以获取更多信息和示例。
要安排动画,您可以使用 Swing Timer。
在 JPanel
class 中的 Graphics
对象上使用的 drawImage(image, x, y, null);
方法需要很长时间才能完成并制作游戏 运行低 FPS,特别是在 MacBook Pro 上它已经过测试 - Linux 机器似乎 运行 它非常好。所花费的时间受图像大小的影响很大。我在网上找到的许多潜在修复方法都没有帮助,例如:
Graphics.drawImage() in Java is EXTREMELY slow on some computers yet much faster on others 以及使用不同版本 Java 的建议根本没有太大区别 - 我尝试了 Java 6 和 8,因为有人建议这有时是由于 Java 7+ 版本。
经过数小时的搜索,我决定尝试创建一个全新的项目,该项目实现了 运行 在循环中使用此方法的基础知识,下面是代码 - 以防我做一些愚蠢的事情在里面!
设置JFrame的Class:
import javax.swing.JFrame;
public class Main {
public static void main(String args[]) {
JFrame f = new JFrame("Carnival Carnage");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setContentPane(new TPanel());
f.pack();
f.setVisible(true);
}
}
扩展JPanel的Class:
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import javax.swing.JPanel;
public class TPanel extends JPanel implements Runnable {
private Graphics g;
private BufferedImage image;
private Thread thread;
private int width = 1600, height = 900;
public TPanel() {
super();
setPreferredSize(new Dimension(width,height));
image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
setFocusable(true);
requestFocus();
}
private void draw() {
g = getGraphics();
long start = System.nanoTime();
g.drawImage(image, 0, 0, null);
System.out.println(System.nanoTime() - start);
}
public void run() {
while(true) {
draw();
}
}
public void addNotify(){
super.addNotify();
if(thread == null) {
thread = new Thread(this);
thread.start();
}
}
}
你有一个无限循环,将继续重新绘制图像。
您不应使用 getGraphics() 并使用该 Graphics 对象进行绘画,因为绘画不是永久性的。
自定义绘画是通过重写 JPanel 的 paintComponent()
方法完成的,然后使用传递给该方法的 Graphics 对象。
阅读 Custom Painting 上的 Swing 教程部分以获取更多信息和示例。
要安排动画,您可以使用 Swing Timer。