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. failed to load selinux policy freezing

    一.原因: 在修改selinux配置文件时输入并保存了一个错误的配置参数. 二.挽救: 1. 重启系统. 2. 在启动选项上按 e. 3. 进入grub编辑页面. 4. 向下移动光标至fi下一行. 5 ...

  2. Aixs2 使用总结,持续更新中 ...

    参考博客:http://zhangjunhd.blog.51cto.com/113473/23692     消息交换模式. 目前Axis2支持三种模式:In-Only.Robust-In和In-Ou ...

  3. Spring MVC 的xml一些配置

    1.可以自动加载注解驱动,通过注解找到对应Controller <!-- spring MVC 注解驱动 --> <mvc:annotation-driven></mvc ...

  4. 生产环境JAVA进程高CPU占用故障排查

    问题描述:生产环境下的某台tomcat7服务器,在刚发布时的时候一切都很正常,在运行一段时间后就出现CPU占用很高的问题,基本上是负载一天比一天高. 问题分析:1,程序属于CPU密集型,和开发沟通过, ...

  5. [svc]samba服务搭建

    说实话搞这些很蛋疼, 没啥技术含量. What is Samba? 这个历史悠久了 Since 1992, Samba has provided secure, stable and fast fil ...

  6. UVA10519 - !! Really Strange !!(数论+高精度)

    10519 - !! Really Strange !!(数论+高精度) option=com_onlinejudge&Itemid=8&category=24&page=sh ...

  7. Linux系统中 Sublime Text 中文 GBK 文件乱码问题

    Sublime Text 是一个很不错编辑器,具有漂亮的界面和强大的功能.再加上丰富的插件,而且还跨平台,绝对是一款实打实的神器啊! 众所周知,Sublime Text 对中文支持的极差,可以说几乎就 ...

  8. 解决MAC下ctags -R无效的问题

    MAC下自带了ctags,与我们常用的是不同的. 我们需要去重新下载一个ctags并重新安装 1.去http://ctags.sourceforge.net/下载Ctags的最新版本源代码 2.tar ...

  9. grails3.1.5 com.mysql.jdbc.Driver

    [报错] Caused by: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver at java.net.URLClassLoader$1 ...

  10. LeetCode: Substring with Concatenation of All Words 解题报告

    Substring with Concatenation of All Words You are given a string, S, and a list of words, L, that ar ...