e668. 在一组像素中创建缓冲图像
This example demonstrates how to convert a byte array of pixel values that are indices to a color table into a BufferedImage
. In particular, the example generates the Mandelbrot set in a byte buffer and combines this data with a SampleModel
, ColorModel
, and Raster
into a BufferedImage
. A 16-color index color model is used to represent the pixel colors.
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.image.*; // Instantiate this class and then use the draw() method to draw the
// generated on the graphics context.
public class Mandelbrot2 {
// Holds the generated image
Image image; // 16-color model; this method is defined in
// e660 用一组像素创建图像
ColorModel colorModel = generateColorModel(); public Mandelbrot2(int width, int height) {
// Initialize with default location
this(width, height, new Rectangle2D.Float(-2.0f, -1.2f, 3.2f, 2.4f));
} public Mandelbrot2(int width, int height, Rectangle2D.Float loc) {
// Generate the pixel data; this method is defined in
// e660 用一组像素创建图像
byte[] pixels = generatePixels(width, height, loc); // Create a data buffer using the byte buffer of pixel data.
// The pixel data is not copied; the data buffer uses the byte buffer array.
DataBuffer dbuf = new DataBufferByte(pixels, width*height, 0); // The number of banks should be 1
int numBanks = dbuf.getNumBanks(); // 1 // Prepare a sample model that specifies a storage 4-bits of
// pixel datavd in an 8-bit data element
int bitMasks[] = new int[]{(byte)0xf};
SampleModel sampleModel = new SinglePixelPackedSampleModel(
DataBuffer.TYPE_BYTE, width, height, bitMasks); // Create a raster using the sample model and data buffer
WritableRaster raster = Raster.createWritableRaster(sampleModel, dbuf, null); // Combine the color model and raster into a buffered image
image = new BufferedImage(colorModel, raster, false, null);//new java.util.Hashtable());
} public void draw(Graphics g, int x, int y) {
g.drawImage(image, x, y, null);
}
}
Here's some code that uses the Mandelbrot2
class:
class RunMandelbrot2 {
static public void main(String[] args) {
new RunMandelbrot2();
}
RunMandelbrot2() {
Frame frame = new Frame("Mandelbrot2 Set");
frame.add(new MyCanvas());
frame.setSize(300, 200) ;
frame.setVisible(true);
} class MyCanvas extends Canvas {
Mandelbrot2 mandelbrot; MyCanvas() {
// Add a listener for resize events
addComponentListener(new ComponentAdapter() {
// This method is called when the component's size changes
public void componentResized(ComponentEvent evt) {
Component c = (Component)evt.getSource(); // Get new size
Dimension newSize = c.getSize(); // Regenerate the image
mandelbrot = new Mandelbrot2(newSize.width, newSize.height);
c.repaint();
}
});
} public void paint(Graphics g) {
if (mandelbrot != null) {
mandelbrot.draw(g, 0, 0);
}
}
}
}
Related Examples |
e668. 在一组像素中创建缓冲图像的更多相关文章
- e667. 在给定图像中创建缓冲图像
An Image object cannot be converted to a BufferedImage object. The closest equivalent is to create a ...
- e666. 创建缓冲图像
A buffered image is a type of image whose pixels can be modified. For example, you can draw on a buf ...
- e675. 翻转缓冲图像
// To create a buffered image, see e666 创建缓冲图像 // Flip the image vertically AffineTransform tx = Aff ...
- PNG格式的图像文件,创建的图像的MIME类型的头部
在安装完这三个组件后,还需要重新配置一次PHP,这也是你对采用DSO方式安装PHP感到庆幸的地方之一.运行make clean,然后在当前的配置中添加下面的内容: --with-gd=[/path/t ...
- opencv ,亮度调整【【OpenCV入门教程之六】 创建Trackbar & 图像对比度、亮度值调整
http://blog.csdn.net/poem_qianmo/article/details/21479533 [OpenCV入门教程之六] 创建Trackbar & 图像对比度.亮度值调 ...
- Android Multimedia框架总结(三)MediaPlayer中创建到setDataSource过程
转载请把头部出处链接和尾部二维码一起转载,本文出自:http://blog.csdn.net/hejjunlin/article/details/52392430 前言:前一篇的mediaPlayer ...
- 什么是WebP以及如何在WordPress中使用WebP图像
图像通常是缓慢加载网页的最大原因之一.它们不仅减慢了加载时间,而且还可以占用服务器上的大量空间和资源.仔细选择文件类型并压缩它们有助于降低加载速度,但它们只能在图像质量受损之前进行优化.另一种选择是使 ...
- 关于创建Web图像时应记住的五个要素
1. 格式与下载速度 当前,Web上用的最广泛的三种格式是GIF.PNG和JPEG.我们的目标是选择质量最高,同时文件最小的格式. WebP图像格式 谷歌建立了另一种图像格式,名为WebP. 这种格式 ...
- WebGPU 中的缓冲映射机制
1. 什么是缓冲映射 就不给定义了,直接简单的说,映射(Mapping)后的某块显存,就能被 CPU 访问. 三大图形 API(D3D12.Vulkan.Metal)的 Buffer(指显存)映射后, ...
随机推荐
- java读取url中json文件中的json数据
有时候需要远程从其他接口中获取json数据,如果遇到返回的json数据是一个文件而不直接是数据,那么可以通过以下方法进行读取: /** * 从数据接口获取到数据 * @return * @throws ...
- mysqlbackup 重建带有gtid特性的slave
一.mysqlbackup简介: mysqlbackup是mysql的一个企业级备份工具,优点就是牛逼,缺点就是这东西要钱买. 二.gtid 特性简介: gtid 的中文名叫全局事务ID,也就是说每一 ...
- unity5 创建material
在material文件夹下 unity5中新创建的material默认如下 开始我不知道贴图应该加在哪儿.于是跳过教程上这一步,直接去选shader: 在这个shader下很明显看到加贴图的地方了: ...
- PHP采集库-Snoopy.class.php
Snoopy是一个php类,用来模拟浏览器的功能,可以获取网页内容,发送表单. Snoopy的特点: 1.抓取网页的内容 fetch 2.抓取网页的文本内容 (去除HTML标签) fetchtext ...
- 几种常见排序算法之Java实现(插入排序、希尔排序、冒泡排序、快速排序、选择排序、归并排序)
排序(Sorting) 是计算机程序设计中的一种重要操作,它的功能是将一个数据元素(或记录)的任意序列,重新排列成一个关键字有序的序列. 稳定度(稳定性)一个排序算法是稳定的,就是当有两个相等记录的关 ...
- php短域名转为实际域名的函数参考
将实际域名转换为短域名,有时也要反转查看下实际域名,可以参考如下的函数. 代码如下: <?php /** * php短域名互转 * edit by www.jbxue.com * 最后修改日期: ...
- 批处理学习笔记8 - 深入学习For命令1
for命令用途非常广,它分为4种 /d /r /l /f /l是类似高级语言的数值循环,在之前就说过,这篇就不说了: http://www.cnblogs.com/HONT/p/3155187.htm ...
- 批处理学习笔记1 - Hellow World
记录自己学习批处理的一点总结吧. 批处理的好处: 可以配合vs,在build完文件之后执行自己的批处理命令. 可以批量修改文件名,或者进行复杂的查询等,对文件可编程操作. 从Hellow world开 ...
- paip.分布式应用系统java c#.net php的建设方案
paip.分布式应用系统java c#.net php的建设方案 1. 基础设施的建立 1 2. 本地的的调用API 1 3. 数据的传输 1 4. 代码的传输 1 5. 代码的自动热编译 2 6. ...
- posix多线程--互斥量
多线程程序在线程间共享数据时,如果多个线程同时访问共享数据就可能有问题.互斥量是解决多个线程间共享数据的方法之一. 1.互斥量初始化两种方式:(1)静态初始化 #include <pthread ...