This example demonstrates how to convert a byte array of pixel values that are indices to a color table into an Image. In particular, the example generates the Mandelbrot set in a byte buffer and uses the MemoryImageSource image producer to create an image from the pixel data in the byte buffer. 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 Mandelbrot {
// Holds the generated image
Image image; // 16-color model
ColorModel colorModel = generateColorModel(); public Mandelbrot(int width, int height) {
// Initialize with default location
this(width, height, new Rectangle2D.Float(-2.0f, -1.2f, 3.2f, 2.4f));
} public Mandelbrot(int width, int height, Rectangle2D.Float loc) {
image = Toolkit.getDefaultToolkit().createImage(
new MemoryImageSource(width, height,
colorModel, generatePixels(width, height, loc), 0, width));
} public void draw(Graphics g, int x, int y) {
g.drawImage(image, x, y, null);
} private byte[] generatePixels(int w, int h, Rectangle2D.Float loc) {
float xmin = loc.x;
float ymin = loc.y;
float xmax = loc.x+loc.width;
float ymax = loc.y+loc.height; byte[] pixels = new byte[w * h];
int pIx = 0;
float[] p = new float[w];
float q = ymin;
float dp = (xmax-xmin)/w;
float dq = (ymax-ymin)/h; p[0] = xmin;
for (int i=1; i<w; i++) {
p[i] = p[i-1] + dp;
} for (int r=0; r<h; r++) {
for (int c=0; c<w; c++) {
int color = 1;
float x = 0.0f;
float y = 0.0f;
float xsqr = 0.0f;
float ysqr = 0.0f;
do {
xsqr = x*x;
ysqr = y*y;
y = 2*x*y + q;
x = xsqr - ysqr + p[c];
color++;
} while (color < 512 && xsqr + ysqr < 4);
pixels[pIx++] = (byte)(color % 16);
}
q += dq;
}
return pixels;
} private static ColorModel generateColorModel() {
// Generate 16-color model
byte[] r = new byte[16];
byte[] g = new byte[16];
byte[] b = new byte[16]; r[0] = 0; g[0] = 0; b[0] = 0;
r[1] = 0; g[1] = 0; b[1] = (byte)192;
r[2] = 0; g[2] = 0; b[2] = (byte)255;
r[3] = 0; g[3] = (byte)192; b[3] = 0;
r[4] = 0; g[4] = (byte)255; b[4] = 0;
r[5] = 0; g[5] = (byte)192; b[5] = (byte)192;
r[6] = 0; g[6] = (byte)255; b[6] = (byte)255;
r[7] = (byte)192; g[7] = 0; b[7] = 0;
r[8] = (byte)255; g[8] = 0; b[8] = 0;
r[9] = (byte)192; g[9] = 0; b[9] = (byte)192;
r[10] = (byte)255; g[10] = 0; b[10] = (byte)255;
r[11] = (byte)192; g[11] = (byte)192; b[11] = 0;
r[12] = (byte)255; g[12] = (byte)255; b[12] = 0;
r[13] = (byte)80; g[13] = (byte)80; b[13] = (byte)80;
r[14] = (byte)192; g[14] = (byte)192; b[14] = (byte)192;
r[15] = (byte)255; g[15] = (byte)255; b[15] = (byte)255; return new IndexColorModel(4, 16, r, g, b);
}
}

Here's some code that uses the Mandelbrot class:

    class RunMandelbrot {
static public void main(String[] args) {
new RunMandelbrot();
}
RunMandelbrot() {
Frame frame = new Frame("Mandelbrot Set");
frame.add(new MyCanvas());
frame.setSize(300, 200);
frame.setVisible(true);
} class MyCanvas extends Canvas {
Mandelbrot 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 Mandelbrot(newSize.width, newSize.height);
c.repaint();
}
});
} public void paint(Graphics g) {
if (mandelbrot != null) {
mandelbrot.draw(g, 0, 0);
}
}
}
}
Related Examples

e660. 用一组像素创建图像的更多相关文章

  1. e668. 在一组像素中创建缓冲图像

    This example demonstrates how to convert a byte array of pixel values that are indices to a color ta ...

  2. canvas上的像素操作(图像复制,细调)

    canvas上的像素操作(图像复制,细调) 总结 1.操作对象:ImageData 对象,其实是canvas的像素点集合 2.主要操作: var obj=ctx.getImageData(0,0,10 ...

  3. HTML <img>标签 创建图像映射

    初级前端一枚 下面代码是在图片上创建图像映射 自己整理了下 做个笔记 希望也可以帮助后来学习的朋友! <map name="planetmap"> <area s ...

  4. GDAL创建图像提示Driver xxx does not support XXX creation option的原因

    经常在群里有人问,创建图像的时候为什么老是提示下面的信息. CPLError: Driver GTiff does not support DCAP_CREATE creation option Wa ...

  5. OpenCV2:第二章 创建图像并显示

    一.简介 相当于在PS中,新建一个画布 二.CvMat类/LPLImage和CvMat结构体 参考: OpenCV2:第一章 图像表示 三.create() Mat m(2,2,CV_8UC3); m ...

  6. php创建图像具体步骤

    php 的图像处理在验证码是最常见的,下面说下使用php创建图像的具体步骤. 简要说明:PHP 并不仅限于创建 HTML 输出, 它也可以创建和处理包括 GIF, PNG(推荐), JPEG, WBM ...

  7. HTML创建图像映射,布局,表单

    来源: 实验楼 创建图像映射 在这之前我们动手试验过将图片作为链接来使用,触发链接的方式就是点击图片的任何地方都可以链接到跳转地址,有时我们需要实现,点击图片的不同地方跳转到不同的地方.意思就是,一张 ...

  8. php bmp中创建图像bmp2gd,让GD支持32位BMP

    php GD库可方便的从URL新建一图像, GD中有imagecreatefromjpeg(),imagecreatefromPNG()....等之类的FUNCTION 可有时从URL中读取的切BMP ...

  9. Vulkan Tutorial 25 Images

    操作系统:Windows8.1 显卡:Nivida GTX965M 开发工具:Visual Studio 2017 Introduction 到目前为止,几何图形使用每个顶点颜色进行着色处理,这是一个 ...

随机推荐

  1. Eclipse中导入Git项目

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

  2. ps减去图层混合模式

    ps减去图层混合模式 CMYK 1.1.青色作为基色,品红作为混合色(减去混合模式) 红反即青色(绿色+蓝色) - 绿反即品红色(红色+蓝色)= 绿色   公式简化: 绿色 + 蓝色 - 红色 - 蓝 ...

  3. paip.分布式应用系统java c#.net php的建设方案

    paip.分布式应用系统java c#.net php的建设方案 1. 基础设施的建立 1 2. 本地的的调用API 1 3. 数据的传输 1 4. 代码的传输 1 5. 代码的自动热编译 2 6.  ...

  4. [k8s]prometheus+grafana监控node和mysql(普罗/grafana均vm安装)

    https://github.com/prometheus/prometheus Architecture overview Prometheus Server Prometheus Server 负 ...

  5. ViewPager滑动不畅及灵敏度的问题

    在项目中用到了Android的ViewPager组件,可是发如今滑动的时候不是特别流畅,有些小的滑动无法响应,于是考虑进行优化. 一開始考虑改动ViewPager中的一些參数.比方mTouchSlop ...

  6. rabbitmq增加vhost

    查看当前rabbitmq上的vhost列表: # rabbitmqctl list_vhosts Listing vhosts ... / vhost2 添加名为demo的vhost虚似主机: rab ...

  7. [转帖]cocos2D-X源码分析之从cocos2D-X学习OpenGL(2)----QUAD_COMMAND

    原文:cocos2D-X源码分析之从cocos2D-X学习OpenGL(2)----QUAD_COMMAND 上一篇文章介绍了cocos2d-x的基本渲染结构,这篇顺着之前的渲染结构介绍渲染命令QUA ...

  8. Windows10 64位 Python2.7 Matplotlib安装

    为了安装Matplotlib 百度了一大堆,也下载了一大堆安装包,结果还是报错ImportError: DLL load failed: %1 不是有效的 Win32 应用程序. 后来直接从官网看怎么 ...

  9. 也谈免拆机破解中兴B860av1.1(解决不能安装软件/解决遥控)

    20170221更新   部分用户(自己恢复出厂测试过),操作后仍然无法直接在当贝市场安装应用了,    在第8条,最后两步,先改为中国通用市场,后面再改为未知局方.    如果开机想优先启动当贝桌面 ...

  10. python的一些科学计算的包

    在安装numpy这类科学计算的包的时候,pip下载的东西有时候缺少一些东西. 可以到这里下载,根据提示信息,少哪个包,或者哪个包出现错误就安装哪个包. PIL到这里下载