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. 小半斤拔凉 支付Java 相关参考

    http://git.oschina.net/littleCrazy/dianshangpingtai-zhifu http://git.oschina.net/52itstyle/springMvc ...

  2. git版本控制文件提交到composer应用市场,并下载用市场的软件库

    要把github中的项目提交到composer中去,必须在github管理的项目中新建对应的composer.json文件, composer.json文件建立的方法 cmd定位到项目目录 compo ...

  3. Android开发1——查找所需要出示权限的内容

    一.发现问题 用户在执行一些如拨打电话.发送短信等关系用户隐私的功能时,Android需要出示权限,权限在AndroidManifest.xml中配置 拨打电话的权限 发送短信的权限 那么这些权限信息 ...

  4. 第一个Verilog程序:通用加法器

    Verilog作为一门硬件描述语言,快速掌握它的方法就是不断的练习,反复动手实践,通过例子掌握隐藏在语句背后的硬件电路.下面是第一个需要学习的Verilog例子: )( :] a, :] b, inp ...

  5. OpenVpn的ipp.txt文件

    ipp=ip pool我猜得,呵呵 ipp.txt文件中存放上一次连接时,客户端分配的ip地址. ipp.txt用来保存上次的连接状态的,并不能为客户端设置固定ip地址. 该文件的格式为:用户名,ip ...

  6. .NET url 的编码与解码

    举例: aaa.aspx?tag=.net%bc%bc%ca%f5 aaa.aspx?tag=.net%e6%8a%80%e6%9c%af看起来好像是不一样,其实他们都是对".net技术&q ...

  7. UTF8字符串在lua的截取和字数统计【转载】

    转载自:GitHub:pangliang/pangliang.github.com 需求 按字面个数来截取 函数(字符串, 开始位置, 截取长度) utf8sub(,) = 好1世界哈 utf8sub ...

  8. my.cnf 配置详解

    调整MySQL运行参数,修改/etc/my.cnf文件调整mysql运行参数重启MySQL后生效,在MySQL4版本以后,一部分内部变量可以在MySQL运行时设置,不过重启MySQL就失效了. mys ...

  9. ecshop和ucenter的整合

    按照网上的教材,一直提示数据库.密码错误,开始怀疑代码错了,毕竟都是两个老古董. 于是开始调试,居然调试也不能很好的支持,点击下一步后就卡死了,好吧,只好用log大法了, error_log(prin ...

  10. sql数据类型总结

    一.数字数据类型 bigint int smallint tinyint decimal numeric money smallmoney float real Bit 二.字符数据类型 非unico ...