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文件,图片只要稍微放大一点,就糊 ...
随机推荐
- MyBatis generator 使用方式 小结
1. 配置到 maven 中pom.xml 中 2. 手动运行 脚本文件 3. 基于 web 项目 http://git.oschina.net/redArmy/springboot-gener ...
- Spring 中属性配置
1 注册自定义属性编辑器,方法一.使用BeanFactory, 则用户需要手动调用 registerCustomEditor(Class requiredType, PropertyEditor pr ...
- 【Android】3.15 短串分享功能
分类:C#.Android.VS2015.百度地图应用: 创建日期:2016-02-04 一.简介 短串分享是指,用户搜索查询后得到的每一个地理位置结果将会对应一条短串(短链接),用户可以通过短信.邮 ...
- mysql部分学习心得(入门级别)
mysql中针对不同的数据选择对应的存储引擎 mysql中也会针对不同的数据处理选择对应的存储的引擎 mysql中也会针对不同的数据处理选择对应的存储的引擎 mysql中一些授权(grant)等的通常 ...
- android framework-下载Android系统源代码
□ apt-get install git-core curl #先下载这两个工具 □ mkdir android-froyo #建立下载目录 □ cd android-froyo #进入下载目录 □ ...
- centos7 ping: www.baidu.com: Name or service not known
[root@www ~]# ping www.baidu.com ping: www.baidu.com: Name or service not known [root@www ~]# 1.网络配置 ...
- Spring Boot干货系列:(七)默认日志框架配置
Spring Boot干货系列:(七)默认日志框架配置 原创 2017-04-05 嘟嘟MD 嘟爷java超神学堂 前言 今天来介绍下Spring Boot如何配置日志logback,我刚学习的时候, ...
- LeetCode: isSameTree1 解题报告
isSameTree1 Given two binary trees, write a function to check if they are equal or not. Two binary t ...
- LeetCode: Median of Two Sorted Arrays 解题报告
Median of Two Sorted Arrays There are two sorted arrays A and B of size m and n respectively. Find t ...
- DMA2D 图形加速器简介
在实际使用 LTDC 控制器控制液晶屏时,使 LTDC 正常工作后,往配置好的显存地址写入要显示的像素数据, LTDC 就会把这些数据从显存搬运到液晶面板进行显示,而显示数据的容量非常大,所以我们希望 ...