e674. 创建并绘制加速图像
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. 创建并绘制加速图像的更多相关文章
- -_-#【Canvas】导出在<canvas>元素上绘制的图像
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- Matlab绘图基础——利用axes(坐标系图形对象)绘制重叠图像 及 一图多轴(一幅图绘制多个坐标轴)
描述 axes在当前窗口中创建一个包含默认属性坐标系 axes('PropertyName',propertyvalue,...)创建坐标系时,同时指定它的一些属性,没有指定的使用DefaultAxe ...
- 使用ImageCreate()创建一个代表空白图像的变量
在建立图像创建环境之前,还需要做一些准备工作.首先,安装t1lib接着安装jpeg-6b,然后再安装GD库文件.在安装时一定要按这里给定的顺序进行安装,因为在编译GD入库时会用到jpeg-6b,如果没 ...
- 基于qml创建最简单的图像处理程序(3)-使用opencv&qml进行图像处理
<基于qml创建最简单的图像处理程序>系列课程及配套代码基于qml创建最简单的图像处理程序(1)-基于qml创建界面http://www.cnblogs.com/jsxyhelu/p/83 ...
- 基于qml创建最简单的图像处理程序(2)-使用c++&qml进行图像处理
<基于qml创建最简单的图像处理程序>系列课程及配套代码基于qml创建最简单的图像处理程序(1)-基于qml创建界面http://www.cnblogs.com/jsxyhelu/p/8 ...
- 基于qml创建最简单的图像处理程序(1)-基于qml创建界面
<基于qml创建最简单的图像处理程序>系列课程及配套代码基于qml创建最简单的图像处理程序(1)-基于qml创建界面http://www.cnblogs.com/jsxyhelu/p/83 ...
- Delphi实例之绘制正弦函数图像
Delphi实例之绘制正弦函数图像 unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphic ...
- OpenCV 鼠标手动绘制掩码图像
OpenCV 鼠标手动绘制掩码图像 完整的代码: #include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui ...
- matplotlib绘制矢量图像(svg),pdf and ps文件
机器学习的过程中处理数据,会遇到数据可视化的问题. 大部分都是利用python的matplotlib库进行数据的可视化处理. plt.show() 默认都是输出.png文件,图片只要稍微放大一点,就糊 ...
随机推荐
- Windows 7 64bit上安装Oracle Database 12c [INS-30131] 错误的解决方法
Windows 7 64bit上安装Oracle Database 12c,出现以下错误: 解决方法: 第一步:控制面板>所有控制面板项>管理工具>服务>SERVER 启动 ...
- 简单易懂的laravel事件,这个功能非常的有用(监听事件,订阅者模式)
先说一下在什么场景会使用这个事件功能. 事情大概是这样的,需求要在用户注册的时候发一些帮助邮件给用户(原本用户在注册之后已经有发别的邮件的了,短信,IM什么的) 原来这个注册的方法也就10多行代码.但 ...
- 微信开发,对象转换为xml时候引用XStream这个类报错处理方案
报错的信息为:The type org.xmlpull.v1.XmlPullParser cannot be resolved. /** * 扩展XStream 支持CDATA */ privat ...
- vim打造简易C语言编辑器(在用2016.7.10)
vim和C语言都需要长期的学习,才能够精通,我制作了这个简单的笔记,主要的作用是,不要在重复的,反复的找同一样东西了,积累是成功的关键. 1. 安装pathogen插件管理器. 在官网下载pathog ...
- C#操作txt文件并清空添加操作
C#操作txt文件,进行清空添加操作的例子.代码: //把txt清空 FileStream stream = File.Open(Adr,FileMode.OpenOrCreate,FileAcces ...
- cocos2d-x开发记录:二,基本概念(导演,场景,层和精灵,场景切换,效果)
四,Director Scene Layer和Sprite(导演,场景,层和精灵) 1.Scenes(场景) 一个场景 (用CCScene对象实现)相当于APP工作流的独立部分.一些人也喜欢叫做“屏幕 ...
- 【Android】10.5 滚动视图(RecyclerView)
分类:C#.Android.VS2015: 创建日期:2016-02-19 一.简介 滚动视图(RecyclerView)的用法与布局控件的用法类似,唯一不同的是,所有布局控件中都可以包含多个组件,而 ...
- 使用 NGINX 和 NGINX Plus 的 Ingress Controller 进行 Kubernetes 的负载均衡
运行和管理跨机器集群的大规模的容器微服务应用是一个极具挑战的任务.Kubernetes 提供了一个强大的容器编排解决方案,从而帮助我们迎接这个挑战.它包含了一些重要特性,比如容错,自动伸缩,滚动升级, ...
- 编写一个程序,开启 3 个线程,这三个线程的 ID 分别为 A、B、C,每个线程将自己的 ID 在屏幕上打印 10 遍,要求输出的结果必须按顺序显示。如:ABCABCABC…… 依次递归
import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.Lock; import java.uti ...
- Linux操作系统及应用课程笔记 索引
第0部分 软件的安装与配置 Linux下软件的安装与配置 第1部分* 绪论 第2部分* Linux的安装过程 第3部分 系统Shell和经常使用命令 Shell文件相关经常使用命令及參数总 ...