Images in accelerated memory are much faster to draw on the screen. This example demonstrates how to take an image and make an accelerated copy of it and then use it to draw on the screen.

The problem with images in accelerated memory is that they can disappear at any time. The system is free to free accelerated memory at any time. Such images are called volatile images. To deal with this issue, it is necessary to check whether a volatile image is still valid immediately after drawing it. If it is no longer valid, it is then necessary to reconstruct the image and then attempt to draw with it again.

This example implements a convenient method for drawing volatile images. It automatically handles the task of reconstructing the volatile image when necessary.

    // This method draws a volatile image and returns it or possibly a
// newly created volatile image object. Subsequent calls to this method
// should always use the returned volatile image.
// If the contents of the image is lost, it is recreated using orig.
// img may be null, in which case a new volatile image is created.
public VolatileImage drawVolatileImage(Graphics2D g, VolatileImage img,
int x, int y, Image orig) {
final int MAX_TRIES = 100;
for (int i=0; i<MAX_TRIES; i++) {
if (img != null) {
// Draw the volatile image
g.drawImage(img, x, y, null); // Check if it is still valid
if (!img.contentsLost()) {
return img;
}
} else {
// Create the volatile image
img = g.getDeviceConfiguration().createCompatibleVolatileImage(
orig.getWidth(null), orig.getHeight(null));
} // Determine how to fix the volatile image
switch (img.validate(g.getDeviceConfiguration())) {
case VolatileImage.IMAGE_OK:
// This should not happen
break;
case VolatileImage.IMAGE_INCOMPATIBLE:
// Create a new volatile image object;
// this could happen if the component was moved to another device
img.flush();
img = g.getDeviceConfiguration().createCompatibleVolatileImage(
orig.getWidth(null), orig.getHeight(null));
case VolatileImage.IMAGE_RESTORED:
// Copy the original image to accelerated image memory
Graphics2D gc = (Graphics2D)img.createGraphics();
gc.drawImage(orig, 0, 0, null);
gc.dispose();
break;
}
} // The image failed to be drawn after MAX_TRIES;
// draw with the non-accelerated image
g.drawImage(orig, x, y, null);
return img;
}

Here's a component that uses the method:

    // Declare a component that draws a volatile image
class MyComponent extends Canvas {
VolatileImage volImage;
Image origImage = null; MyComponent() {
// Get image to move into accelerated image memory
origImage = new ImageIcon("image.gif").getImage();
} public void paint(Graphics g) {
// Draw accelerated image
int x = 0;
int y = 0;
volImage = drawVolatileImage((Graphics2D)g, volImage, x, y, origImage);
}
}
Related Examples

e674. 创建并绘制加速图像的更多相关文章

  1. -_-#【Canvas】导出在<canvas>元素上绘制的图像

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  2. Matlab绘图基础——利用axes(坐标系图形对象)绘制重叠图像 及 一图多轴(一幅图绘制多个坐标轴)

    描述 axes在当前窗口中创建一个包含默认属性坐标系 axes('PropertyName',propertyvalue,...)创建坐标系时,同时指定它的一些属性,没有指定的使用DefaultAxe ...

  3. 使用ImageCreate()创建一个代表空白图像的变量

    在建立图像创建环境之前,还需要做一些准备工作.首先,安装t1lib接着安装jpeg-6b,然后再安装GD库文件.在安装时一定要按这里给定的顺序进行安装,因为在编译GD入库时会用到jpeg-6b,如果没 ...

  4. 基于qml创建最简单的图像处理程序(3)-使用opencv&qml进行图像处理

    <基于qml创建最简单的图像处理程序>系列课程及配套代码基于qml创建最简单的图像处理程序(1)-基于qml创建界面http://www.cnblogs.com/jsxyhelu/p/83 ...

  5. 基于qml创建最简单的图像处理程序(2)-使用c++&qml进行图像处理

     <基于qml创建最简单的图像处理程序>系列课程及配套代码基于qml创建最简单的图像处理程序(1)-基于qml创建界面http://www.cnblogs.com/jsxyhelu/p/8 ...

  6. 基于qml创建最简单的图像处理程序(1)-基于qml创建界面

    <基于qml创建最简单的图像处理程序>系列课程及配套代码基于qml创建最简单的图像处理程序(1)-基于qml创建界面http://www.cnblogs.com/jsxyhelu/p/83 ...

  7. Delphi实例之绘制正弦函数图像

    Delphi实例之绘制正弦函数图像 unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphic ...

  8. OpenCV 鼠标手动绘制掩码图像

    OpenCV 鼠标手动绘制掩码图像 完整的代码: #include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui ...

  9. matplotlib绘制矢量图像(svg),pdf and ps文件

    机器学习的过程中处理数据,会遇到数据可视化的问题. 大部分都是利用python的matplotlib库进行数据的可视化处理. plt.show() 默认都是输出.png文件,图片只要稍微放大一点,就糊 ...

随机推荐

  1. JAVA-day08 下午-总结、測试

    继承总结: class { public static void main(String[] args) { System.out.println("Hello World!"); ...

  2. spring sessionFactory 属性配置详解,applicationContext中各种属性详解

    1.Bean的id为sessionFactory,对应的类为AnnotationSessionFactory,即采用注解的形式实现hibernate. 2.hibernateProperties,配置 ...

  3. 温故而知新 gulp.src 指定数组文件夹

    gulp.src语法是基于这个库来实现的,所以详情请看这个API: https://www.gulpjs.com.cn/docs/api/ https://github.com/isaacs/node ...

  4. Java并发和多线程那些事儿

    我记得我接触电脑的时候是在小学三年级的时候,那是1995年,那年发布了windows95,但是我学习的时候还是只是dos系统,简单对于文件的一些命令操作还有五笔 在过去的那个年代,电脑都是单CPU,也 ...

  5. 服务器上装filezilla server后,本地的ftp客户端连接不上去

    公司一台服务器,上面装了filezilla server后,按平常配置好了,但是在本地用FTP客户端不管怎么连接都连接不上,本地FTP客户端总提示连接失败,远程filezilla server的界面也 ...

  6. IP分片

    物理网络层一般要限制每次发送数据帧的最大长度.任何时候IP层接受到一份要发送的IP数据报时,它要判断向本地哪个接口发送数据(选路),并查询该接口获得其MTU(最大传输单元:Maximum Transm ...

  7. cocos2d-x画线

    在class HelloWorld : public cocos2d::CCLayer中添加 void draw(); 实现: void HelloWorld::draw() { CCSize s = ...

  8. python学习之pyc,pyo,pyd文件

    pyc:二进制文件,python文件经过编译器编译之后的文件.可以提高文件加载速度. pyo:二进制文件,优化编译后的文件.可以通过`python -O file.py`生成. pyd:python的 ...

  9. Not get deviceToken yet. Maybe: your certificate not configured APNs? or current network is not so good so APNs registration failed? or there is no APNs register code? Please refer to JPush docs.”

    今天在项目中集成了极光推送,一切都配置完毕,把程序运行起来的时候,报了下面的错误: Not get deviceToken yet. Maybe: your certificate not confi ...

  10. 李洪强iOS开发之极光推送JPush

    李洪强iOS开发之极光推送JPush