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 到目前为止,几何图形使用每个顶点颜色进行着色处理,这是一个 ...
随机推荐
- CView类的使用
首先我们来写一个样例: 1.建一个win32简单应用程序,不要觉得这样就不能写出MFC程序,由于是不是MFC程序取决于调没调MFC函数. 2. 删除入口函数.仅仅留下#include "st ...
- spring boot实战读书笔记1
1 覆盖起步依赖引入的传递依赖. 以Spring Boot的Web起步依赖为例,它传递依赖了Jackson JSON库.如果不想使用,可以使用 <exclusions>元素去除Jackso ...
- 多主机共享ssh Public/Private Key
前期服务器比较少,所有代码都放在github的私库中,在自己的github 设置中设置SSH keys就可以拉下相应的库中的代码到本地与服务器了,但是最近服务器多家了几台,每台都生成key加到gith ...
- mac下设置redis开机启动方法
Mac OS 的开机启动方式 launchd 是 Mac OS 下用于初始化系统环境的关键进程,它是内核装载成功之后在OS环境下启动的第一个进程.其实它的作用就是我们平时说的守护进程,简单来说,用户守 ...
- navicat 在写存储过程的时候总是说语法错误
这个时候我们在sql的头部加上 DELIMITER $$ 尾部加上 $$DELIMITER;
- perl内置特殊变量查询
perl中有许多预定于的内置变量,想$_,$,,$>,等等,基本是记不住全部的用法,如果在因特网查阅,有很麻烦,信息不准啦,说的不细啦,但是,万能的perldoc早就帮我们准备好了. 你需要做的 ...
- 【LeetCode-面试算法经典-Java实现】【136-Single Number(仅仅出现一次的数字)】
[136-Single Number(仅仅出现一次的数字)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given an array of integers, ev ...
- 使用post向webservice发送请求,并且返回值
注意,这个方法仅仅适用于:该post页面处于websercive 站点下,或者是与webservice同属于一个主站点,即在iis里属于同一主站点的同级虚拟目录. protected void btn ...
- Eclipse最新版注释模板设置详解
/** * HassCMS (http://www.hassium.org/) * * @link http://github.com/hasscms for the canonical source ...
- JavaHbase连接代码示例
package com.rokid.hbase; import java.io.IOException; import org.apache.hadoop.conf.Configuration; im ...