e660. 用一组像素创建图像
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. 用一组像素创建图像的更多相关文章
- e668. 在一组像素中创建缓冲图像
This example demonstrates how to convert a byte array of pixel values that are indices to a color ta ...
- canvas上的像素操作(图像复制,细调)
canvas上的像素操作(图像复制,细调) 总结 1.操作对象:ImageData 对象,其实是canvas的像素点集合 2.主要操作: var obj=ctx.getImageData(0,0,10 ...
- HTML <img>标签 创建图像映射
初级前端一枚 下面代码是在图片上创建图像映射 自己整理了下 做个笔记 希望也可以帮助后来学习的朋友! <map name="planetmap"> <area s ...
- GDAL创建图像提示Driver xxx does not support XXX creation option的原因
经常在群里有人问,创建图像的时候为什么老是提示下面的信息. CPLError: Driver GTiff does not support DCAP_CREATE creation option Wa ...
- OpenCV2:第二章 创建图像并显示
一.简介 相当于在PS中,新建一个画布 二.CvMat类/LPLImage和CvMat结构体 参考: OpenCV2:第一章 图像表示 三.create() Mat m(2,2,CV_8UC3); m ...
- php创建图像具体步骤
php 的图像处理在验证码是最常见的,下面说下使用php创建图像的具体步骤. 简要说明:PHP 并不仅限于创建 HTML 输出, 它也可以创建和处理包括 GIF, PNG(推荐), JPEG, WBM ...
- HTML创建图像映射,布局,表单
来源: 实验楼 创建图像映射 在这之前我们动手试验过将图片作为链接来使用,触发链接的方式就是点击图片的任何地方都可以链接到跳转地址,有时我们需要实现,点击图片的不同地方跳转到不同的地方.意思就是,一张 ...
- php bmp中创建图像bmp2gd,让GD支持32位BMP
php GD库可方便的从URL新建一图像, GD中有imagecreatefromjpeg(),imagecreatefromPNG()....等之类的FUNCTION 可有时从URL中读取的切BMP ...
- Vulkan Tutorial 25 Images
操作系统:Windows8.1 显卡:Nivida GTX965M 开发工具:Visual Studio 2017 Introduction 到目前为止,几何图形使用每个顶点颜色进行着色处理,这是一个 ...
随机推荐
- 关于apache服务器加载so的报错
早上突然发现我的虚拟机上的WEB应用访问不了了,后台检查httpd服务,无法启动,出现一行提示: ①starting httpd: httpd: Syntax error on line 163 of ...
- __align
__align关键字指示编译器在n字节边界上对齐变量. __align是一个存储类修饰符.它不影响函数的类型. 语法 __align(n) 其中: n是对齐边界. n可以具有值1.2.4或者8. _ ...
- 深入浅出 妙用Javascript中apply、call、bind【转】
网上文章虽多,大多复制粘贴,且晦涩难懂,我希望能够通过这篇文章,能够清晰的提升对apply.call.bind的认识,并且列出一些它们的妙用加深记忆. apply.call 在 javascript ...
- 分享一:关于mysql中避免重复插入记录方法
一: INSERT ON DUPLICATE KEY UPDATE 如果您指定了ON DUPLICATE KEY UPDATE,并且插入行后会导致在一个UNIQUE索引或PRIMARY KEY中出现重 ...
- iphone CGContextSetLineWidth 画线的问题
转自:http://blog.csdn.net/jxncwzb/article/details/6267154 CGContextRef context = UIGraphicsGetCurrentC ...
- cocos2d-x树结构执行动作
void Test2::onEnter() { TestCocosNodeDemo::onEnter(); CCSize s = CCDirector::sharedDirector()->ge ...
- angular学习笔记(十四)-$watch(1)
本篇主要介绍$watch的基本概念: $watch是所有控制器的$scope中内置的方法: $scope.$watch(watchObj,watchCallback,ifDeep) watchObj: ...
- 委托事件和jquery中的delegate方法
利用事件冒泡的特性,给父元素绑定事件,然后判断事件对象,来给父元素的每个子元素添加事件,而不是直接在所有的子元素上绑定事件: <ul> <li></li> < ...
- 【转】python多版本并存,python3安装pip
python3 与 pip3 安装与使用 1. yum -y install openssl* (pip依赖ssl环境) 2.编译安装python3 下载地址:https://www.python ...
- 【Java】包装类总结
Java语言是一个面向对象的语言,但是Java中的基本数据类型却是不面向对象的,这在实际使用时存在很多的不便,为了解决这个不足,在设计类时为每个基本数据类型设计了一个对应的类进行代表,这样八个和基本数 ...