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. 在一组像素中创建缓冲图像的更多相关文章

  1. e667. 在给定图像中创建缓冲图像

    An Image object cannot be converted to a BufferedImage object. The closest equivalent is to create a ...

  2. e666. 创建缓冲图像

    A buffered image is a type of image whose pixels can be modified. For example, you can draw on a buf ...

  3. e675. 翻转缓冲图像

    // To create a buffered image, see e666 创建缓冲图像 // Flip the image vertically AffineTransform tx = Aff ...

  4. PNG格式的图像文件,创建的图像的MIME类型的头部

    在安装完这三个组件后,还需要重新配置一次PHP,这也是你对采用DSO方式安装PHP感到庆幸的地方之一.运行make clean,然后在当前的配置中添加下面的内容: --with-gd=[/path/t ...

  5. opencv ,亮度调整【【OpenCV入门教程之六】 创建Trackbar & 图像对比度、亮度值调整

    http://blog.csdn.net/poem_qianmo/article/details/21479533 [OpenCV入门教程之六] 创建Trackbar & 图像对比度.亮度值调 ...

  6. Android Multimedia框架总结(三)MediaPlayer中创建到setDataSource过程

    转载请把头部出处链接和尾部二维码一起转载,本文出自:http://blog.csdn.net/hejjunlin/article/details/52392430 前言:前一篇的mediaPlayer ...

  7. 什么是WebP以及如何在WordPress中使用WebP图像

    图像通常是缓慢加载网页的最大原因之一.它们不仅减慢了加载时间,而且还可以占用服务器上的大量空间和资源.仔细选择文件类型并压缩它们有助于降低加载速度,但它们只能在图像质量受损之前进行优化.另一种选择是使 ...

  8. 关于创建Web图像时应记住的五个要素

    1. 格式与下载速度 当前,Web上用的最广泛的三种格式是GIF.PNG和JPEG.我们的目标是选择质量最高,同时文件最小的格式. WebP图像格式 谷歌建立了另一种图像格式,名为WebP. 这种格式 ...

  9. WebGPU 中的缓冲映射机制

    1. 什么是缓冲映射 就不给定义了,直接简单的说,映射(Mapping)后的某块显存,就能被 CPU 访问. 三大图形 API(D3D12.Vulkan.Metal)的 Buffer(指显存)映射后, ...

随机推荐

  1. 在windows下部署Redis 当作系统服务器自动启动

    部署Redis 其实Redis是可以安装成windows服务的,开机自启动,命令如下: redis-server --service-install redis.windows.conf 安装完之后, ...

  2. js 正则表达式 exec 和 match的使用

    match body.match(/\d\.\d\.\d\.\d:\d/g); // 推荐使用exec可以拿到多个数组 exec var a = [ 'PHPSESSID=sglvjui97o18bg ...

  3. jenkins 执行远程linux命令

    在Jenkins中进行构建时,可能需要首先SSH登录到一个远程服务器以执行必要的脚本,然后再执行构建.这时,需要安装SSH Plugin,并进行如下配置.1.在Jenkins界面,系统管理->管 ...

  4. 使用国内镜像源安装npm包报错

    1 使用命令安装 npm install webpack --save-dev 可能是版本太高,国内没有更新. 通过设置镜像源 npm config set registry http://regis ...

  5. Eclipse中导入Git项目

    1.先将项目git到本地 2.导入刚刚git到本地项目 if(如果project带.calsspath .project 文件){ 直接用genaral导入或andorid project导入即可. ...

  6. [hihoCoder] #1096 : Divided Product

    时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Given two positive integers N and M, please divide N into sev ...

  7. Win7中安装Windows PowerShell 3.0

    win7内置的powershell是2.0,现在已经明显落伍了,但win系统软件更新,需要解决依赖问题,so,按下面步骤安装即可. 1. 安装Microsoft .NET Framework 4.0的 ...

  8. [sh]sed 4个功能

    [root@lanny test]# cat test.txt test liyao lanny 经典博文: http://oldboy.blog.51cto.com/2561410/949365 h ...

  9. ETL调度开发(5)——连接数据库运行数据库命令子程序

    ETL调度中读写数据信息,都须要连接数据库,以下的子程序通过传入的数据库连接字符串和数据库命令(或SQL)运行所须要的操作: #!/usr/bin/bash #created by lubinsu # ...

  10. python 开发在线音乐播放器-简易版

    在线音乐播放器,使用python的Tkinter库做了一个界面,感觉这个库使用起来还是挺方便的,音乐的数据来自网易云音乐的一个接口,通过urllib.urlopen模块打开网址,使用Json模块进行数 ...