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 到目前为止,几何图形使用每个顶点颜色进行着色处理,这是一个 ...
随机推荐
- java实现简单webserver(分析+源码)
在日常的开发中,我们用过非常多开源的webserver,比如tomcat.apache等等.如今我们自己实现一个简单的webserver,主要的功能就是用户点击要訪问的资源,server将资源发送到c ...
- php - 中文字符串分割
//先删除掉非中文的字体$str = preg_replace('/[^\x{4e00}-\x{9fa5}]/u', '', $str);//经过测试中文占3个篇幅$re = chunk_split( ...
- unity hide/show text
using UnityEngine;using System.Collections; public class PlayerController:MonoBehaviour{ public U ...
- 在 Visual Studio 2013 中使用 JavaScript 的 IntelliSense
原本JavaScript直接在页面中引用的时候,智能感知会直接根据js文件的内容来获得提示.但是由于在新的ASP.NET中使用了Bundle,js文件不是直接在页面中引用了,所以智能感知也就出了问题. ...
- 判断js对象是否拥有某一个属性的js代码
js对象是否拥有某一个属性的判断方法有很多. 本文分享一个简单的方法,如下: <script> /** * 判断js对象是否具有某属性 * by www.jbxue.com */ var ...
- 在线升级java的版本
真接rpm包安装javaxxxx.rpm 然后调整/etc/profile 如下: # tail -n /etc/profile JAVA_HOME=/usr/java/jdk1..0_45 PATH ...
- ny47 过河问题
过河问题 时间限制:1000 ms | 内存限制:65535 KB 难度:5 描述 在漆黑的夜里,N位旅行者来到了一座狭窄而且没有护栏的桥边.如果不借助手电筒的话,大家是无论如何也不敢过桥去的 ...
- Oracle PLSQL Demo - 17.游标查询个别字段(非整表)
declare Type ref_cur_variable IS REF cursor; cur_variable ref_cur_variable; v_empno scott.emp.empno% ...
- codeBlocks编译undefined reference to错误
是没有把c文件编译进去的原因. 右键项目,选择属性,弹出窗体 然后选择build targets 在最下面有个build target files:中把c文件勾选.点击ok重新编译即可. Code:: ...
- Java web项目中java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
原来是tomcat找不到MYSQL JAR包的问题.后来又把mysql-connector-java-5.1.7-bin.jar导入到tomcat的lib目录下面就ok了,嘿…… 在java项目中,只 ...