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. ASP.NET Helper

  2. php 数组与数组 的交集和差集

    注意,必须是第一个参数的数组的长度比较长才可以使用 $a1=array("0","1","2","3"); $a2=ar ...

  3. MySQL "replace into" 的坑以及insert相关操作

    下面我们主要说一下在插入时候的几种情况: 1:insert ignore 2:replace into 3:ON DUPLICATE KEY UPDATE 关于insert ignore: 关于rep ...

  4. ASP.NET 解决URL中文乱码的解决

    暂时先记录一个方法: 在Web.config文件中configuration下的system.web下加入一个配置项:globalization,主要是设置其requestEncoding,貌似中文系 ...

  5. JAX-RS(REST Web Services)2.0 can not be installed: One or more constraints have not been satisfied

    eclipse出错: JAX-RS(REST Web Services)2.0 can not be installed: One or more constraints have not been ...

  6. 一些通用性的haproxy调优tips

    一.硬件和系统 haproxy是单线程,非阻塞,事件驱动,所以会最大化利用单个CPU内核,选择haproxy的硬件时要关注如下:   1.选择CPU的时候,选择高主频,大缓存的型号,比内核数更重要 2 ...

  7. rabbitmq 基本信息

    两个或多个系统间需要通过定时任务来同步数据,异构系弘的不同进程间想互调用 通讯的问题,可以用rabbitmq解决.rabbitmq擅长解决多系统 异构系统音的数据交换,也可以用于系统间服务的相互调用R ...

  8. CCLayer设置anchorPoint无效的问题

      最近刚发现一个问题,anchorPoint这个属性虽然是属于CCNode的,但是CCLayer设置anchorPoint没有效果.CCLayer的anchorPoint被默认设定在(0, 0)位置 ...

  9. 解决老是提示找不到Mapper文件无法执行定义的方法问题!

    尼玛,被mybatis的*Mapper.xml文件害惨了!整整两天都在围绕这个问题转圈! 先看问题长啥样吧!下面是通过逆向工程生成的Mapper.xml文件,包路径什么的都没有错! 但是每次调用Map ...

  10. Eclipse下maven部署web项目到tomcat7(兼容tomcat8)

    1.下载tomcat7并配置好JAVA_HOME,tomcat7\webapps目录除了manager之外,其它都可以删除(删除没用的,可加速tomcat的启动). 2.新建系统变量CATALINA_ ...