?? mandelbrot1.html
字號:
<HTML>
<HEAD>
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
<TITLE>Drawing the Mandelbrot Set
(Java Developers Almanac Example)
</TITLE>
<META CONTENT="Patrick Chan" NAME="AUTHOR">
<META CONTENT="Code Examples from The Java Developers Almanac 1.4" NAME="DESCRIPTION">
<META CONTENT="Addison-Wesley/Patrick Chan" NAME="OWNER">
<META CONTENT="3/20/02" NAME="revision">
<STYLE TYPE="text/css">
<!-- BODY CODE {font-family: Courier, Monospace; font-size: 11pt} TABLE, BODY {font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10pt} PRE {font-family: Courier, Monospace; font-size: 10pt} H3 {font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11pt} A.eglink {text-decoration: none} A:hover.eglink {text-decoration: underline} -->
</STYLE>
</HEAD>
<BODY>
<TABLE CELLSPACING="0" CELLPADDING="0" BORDER="0">
<TR>
<TD rowspan="3"><A HREF="/?l=ex"><IMG BORDER="0" ALIGN="BOTTOM" HSPACE="10" SRC="/egs/almanac14a.jpg"></A></TD><TD VALIGN="top"><font face="Times" size="6"><b>The Java Developers Almanac 1.4</b></font>
<br>
Order this book from <a href="/cgi-bin/scripts/redirect.pl?l=ex&url=http://www.amazon.com/exec/obidos/ASIN/0201752808/xeo">Amazon</a>.
</TD>
</TR>
<TR>
<TD align="right" valign="bottom">
<FORM method="get" action="/cgi-bin/search/find.pl">
<INPUT size="25" name="words" type="text"><INPUT value="Search" type="submit">
</FORM>
</TD>
</TR>
</TABLE>
<HR color="#6666cc">
<DIV ALIGN="LEFT">
<A HREF="/">Home</A>
>
<A HREF="../index.html">List of Packages</A>
>
<B><A HREF="../java.awt.image/pkg.html">java.awt.image</A></B><font color="#666666" SIZE="-2">
[17 examples]
</font>
</DIV><P>
<h3>
e674.
Drawing the Mandelbrot Set</h3>
This example generates the Mandelbrot set in a pixel buffer and uses
the <code>MemoryImageSource</code> image producer to create an image from the
pixel buffer. A 16-color index color model is used to represent the
pixel values.
<pre>
// Instantiate this class and then use the draw() method to draw the
// generated on the graphics context.
class Mandelbrot {
// Dimension of the image
int width;
int height;
// Holds the generated image
Image image;
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) {
// Initialize color model
generateColorModel();
this.width = width;
this.height = height;
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;
}
// 16-color model
ColorModel colorModel;
private void 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;
colorModel = new IndexColorModel(4, 16, r, g, b);
}
}
</pre>
Here's a component that uses the <code>Mandelbrot</code> class:
<pre>
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);
}
}
}
</pre>
<pre>java.awt.image/Mandelbrot1.java
e c java.awt.image IndexColorModel(int,int,byte[],byte[],byte[])
e c java.awt.image MemoryImageSource(int,int,java.awt.image.ColorModel,byte[],int,int)
e f java.awt Dimension.height
e f java.awt Dimension.width
e m java.awt Canvas.paint(java.awt.Graphics)
e m java.awt Component.getSize()
e m java.awt Component.repaint()
e m java.awt Graphics.drawImage(java.awt.Image,int,int,java.awt.image.ImageObserver)
e m java.awt Toolkit.createImage(java.awt.image.ImageProducer)
e m java.awt Toolkit.getDefaultToolkit()
i c java.awt.event ComponentAdapter()
i c java.awt.geom Rectangle2D.Float(float,float,float,float)
i f java.awt.geom Rectangle2D.Float.height
i f java.awt.geom Rectangle2D.Float.width
i f java.awt.geom Rectangle2D.Float.x
i f java.awt.geom Rectangle2D.Float.y
i m java.awt.event ComponentAdapter.componentResized(java.awt.event.ComponentEvent)
i m java.awt.event ComponentEvent.getSource()
m x java.awt Image
m x java.awt.image ColorModel
</pre>
<br>
<FONT FACE="Verdana, Arial, Helvetica, sans-serif" SIZE="0">
© 2002 Addison-Wesley.
</FONT>
</BODY>
</HTML>
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -