Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/398.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 缓冲图像性能_Java_Graphics_Jframe_Bufferedimage - Fatal编程技术网

Java 缓冲图像性能

Java 缓冲图像性能,java,graphics,jframe,bufferedimage,Java,Graphics,Jframe,Bufferedimage,我正在查看ludum dare(minicraft)中Notch的代码,我注意到他使用: private BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB); private int[] pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData(); 所以我做了一个简单的测试程序,我注意到我的性能比以前

我正在查看ludum dare(minicraft)中Notch的代码,我注意到他使用:

private BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
private int[] pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();
所以我做了一个简单的测试程序,我注意到我的性能比以前使用的方法低了很多。使用这种方法,只需绘制一个完全白色的屏幕,我就可以获得800 fps的速度,当我绘制一个测试图像时,我可以获得超过1200 fps的速度,这似乎有点奇怪。即使我没有在
像素[]
上声明任何东西,只保留默认值,它仍然是800 fps。我想知道是否有人能给我解释一下原因。这是我编写的测试代码,我使用fraps获得fps:

public class Test extends Canvas implements Runnable{

private static final long serialVersionUID = 1L;

private boolean running = false;

public  final int HEIGHT = 300;
public  final int WIDTH = 360;
private final int SCALE = 3;

private BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
private BufferedImage testImage;
private int[] pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();

JFrame frame;
public Test(){
    Dimension size = new Dimension(WIDTH*SCALE, HEIGHT*SCALE);
    frame = new JFrame();
    this.frame.setResizable(false);
    this.setPreferredSize(size);
    this.setMaximumSize(size);
    this.setMinimumSize(size);
    this.frame.add(this);
    this.frame.pack();
    this.frame.setLocationRelativeTo(null);
    this.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setFocusable(true);
    this.frame.setVisible(true);

    try {
        testImage = ImageIO.read(new File("res/test.jpg"));
    } catch (IOException e) {
        e.printStackTrace();
    }
    for(int i = 0; i < pixels.length; i++){
    pixels[i] = 0xffffff;
    }
    running = true;
}

public void run() {
    while(running){
        render();
    }
}

private void render() {
    BufferStrategy bs = getBufferStrategy();
    if(bs == null){
        createBufferStrategy(3);
        return;
    }

    Graphics g = bs.getDrawGraphics();
    //Draw stuff
    g.setColor(Color.BLACK);
    g.fillRect(0, 0, getWidth(), getHeight());
    g.drawImage(testImage, 0, 0, getWidth(), getHeight(), null);
    g.dispose();
    bs.show();
}

public static void main(String args[]){
    Test test = new Test();
    test.run();
}
公共类测试扩展画布实现可运行{
私有静态最终长serialVersionUID=1L;
私有布尔运行=false;
公共最终内部高度=300;
公共最终整数宽度=360;
私人最终智力量表=3;
私有BuffereImage=新的BuffereImage(宽度、高度、BuffereImage.TYPE_INT_RGB);
私有缓冲区映像测试映像;
私有int[]像素=((DataBufferInt)image.getRaster().getDataBuffer()).getData();
JFrame框架;
公开考试(){
尺寸尺寸=新尺寸(宽度*比例,高度*比例);
frame=新的JFrame();
this.frame.setresizeable(false);
此.setPreferredSize(大小);
此.setMaximumSize(大小);
此.setMinimumSize(大小);
this.frame.add(this);
this.frame.pack();
this.frame.setLocationRelativeTo(null);
此.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
此参数为.setFocusable(true);
此.frame.setVisible(true);
试一试{
testImage=ImageIO.read(新文件(“res/test.jpg”);
}捕获(IOE异常){
e、 printStackTrace();
}
对于(int i=0;i
}

我只需将
testImage
替换为
image
。谢谢大家,节日快乐


编辑:我注意到只需调用int[]pixels=((DataBufferInt)image.getRaster().getDataBuffer()).getData();使应用程序的速度大大降低。有人能解释一下为什么会这样吗?我需要处理像素还是什么?谢谢

据我所知,BuffereImage被视为幕后的挥发图像。这意味着它们将获得巨大的硬件加速提升。这是因为图像被直接复制到视频卡的内存中,允许快速绘制

当您想要访问像素矩阵时,图像将被放置在常规RAM中,并从那里访问,因此会导致显著的速度减慢

实际上,如果禁用JVM中的所有硬件加速,白色图像将以最快的速度绘制

如果要高效地更新屏幕,请将setIgnoreRepaint(true)应用于窗口,并仅绘制已更改的屏幕部分