简体   繁体   English

Java Graphics drawImage消耗多少?

[英]How much does Java Graphics drawImage consume?

I was making an application in Java, and noticed after doing this: 我正在用Java开发应用程序,执行此操作后发现:

g.drawImage(ImageIO.read(...)....);

that it consumed a lot more CPU and frames, so I started to wonder how much memory does this take up? 它消耗了更多的CPU和框架,所以我开始怀疑这会占用多少内存? and if there were any other better ways to draw an image. 以及是否还有其他更好的方法来绘制图像。

Thanks! 谢谢!

It's not the drawImage that's taking up a lot of CPU, it's the ImageIO.read(…) ! 不是drawImage占用大量CPU,而是ImageIO.read(…) Opening streams is expensive and thus should only be done once; 打开流很昂贵,因此只能执行一次。 when you first need to load the images. 当您第一次需要加载图像时。 The best way to draw an image is to pre-load it into a BufferedImage (or if you manipulate the pixels a VolatileImage ). 绘制图像的最好方法是将其预加载到BufferedImage (或者,如果操纵像素,则为VolatileImage )。

BufferedImage imageToDraw = ImageIO.read(getClass().getResource("path/To/Image.png"));

That path starts in the directory of your .java files. 该路径从.java文件的目录开始。 So, you'll want to add a folder containing your image(s). 因此,您需要添加一个包含图像的文件夹。 There are many different ways to load images, more of which are discussed here . 加载图像的方式有很多, 这里将讨论更多方法。

Then you can draw it just like you did before. 然后,您可以像以前一样绘制它。

g.drawImage(imageToDraw, x, y, null);

This should be much faster. 这应该是快得多。 If you are still having problems with speed then this question might be useful. 如果您仍然有速度问题,那么这个问题可能会有用。

One possibility is using visualvm: http://visualvm.java.net . 一种可能是使用visualvm: http : //visualvm.java.net I've used it with some success. 我已经成功地使用了它。

Quick steps: 快速步骤:

  • start your app locally 在本地启动您的应用
  • start visualvm 启动visualvm
  • Tools > Options > Profiling > Misc (bottom) > Reset calibration data: Ok 工具>选项>分析>其他(底部)>重置校准数据:确定
  • in VVM, connect to your app ie, in left-hand frame, double-click on pkg.YourMainClass (pid 2112) 在VVM中,连接到您的应用程序,即在左侧框架中,双击pkg.YourMainClass(pid 2112)
  • Profiler tab, then click on Memory. 分析器选项卡,然后单击内存。

after initial baseline profiling is complete, you see a histogram of live objects. 完成初始基准配置后,您会看到活动对象的直方图。

You can take snapshots and compare them, set filters on packages, etc. See http://visualvm.java.net/docindex.html 您可以拍摄快照并进行比较,在软件包上设置过滤器等。请参见http://visualvm.java.net/docindex.html

If you're reading the image rather than holding it as an instance of Image, you will be taking way too much memory up. 如果您正在读取图像而不是将其作为Image的实例保存,则将占用过多的内存。

I highly, HIGHLY suggest to store the image as an instance of Image with ImageIO.read(params) rather than drawing it and reading it every single loop pass-through. 我强烈建议将图像存储为具有ImageIO.read(params)的Image实例,而不是绘制并读取每个循环传递的图像。

You'd most likely do it like this in your Canvas/JPanel/Swing Component class: 您最有可能在Canvas / JPanel / Swing Component类中这样做:

private Image myImage;

//Assume other code here

public void loadImages() {
     myImage = ImageIO.read(params);
     //load any other images
}

public void paint(Graphics g) {
     g.drawImage(myImage, x, y, null);
}

Just like any other application, resources (images, data, etc) are all loaded at the start-up. 与其他任何应用程序一样,资源(图像,数据等)都在启动时加载。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM