⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mandelbrot.htm

📁 这个压缩包里的都是超级经典的java例子
💻 HTM
字号:
<HTML>
<HEAD>
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
<TITLE>Creating an Image from an Array of Color-Indexed Pixel Values (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">
<META CONTENT="no-cache" HTTP-EQUIV="Pragma">
<LINK href="/almanac.css" media="screen" type="text/css" rel="stylesheet">
</HEAD>
<BODY>
<TABLE CELLSPACING="0" CELLPADDING="0" BORDER="0">
<TR>
<TD></TD>
</TR>
</TABLE>
<br>
<TABLE CELLSPACING="0" CELLPADDING="0" BORDER="0">
<TR>
<TD></TD>
</TR>
<TR>
<TD rowspan="3"><A HREF="/?l=ex"><IMG BORDER="0" ALIGN="BOTTOM" HSPACE="10" SRC="/egs/almanac14a.jpg"></A></TD><TD VALIGN="top">
<h1>The Java Developers Almanac 1.4</h1>
<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">
<TABLE CELLSPACING="0" CELLPADDING="0" BORDER="0">
<TR>
<TD valign="top"><script type="text/javascript">
<!--
google_ad_client = "pub-6001183370374757";
google_ad_width = 120;
google_ad_height = 600;
google_ad_format = "120x600_as";
google_ad_channel = "4777242811";
google_ad_type = "text_image";
google_color_border = "FFFFFF";
google_color_bg = "FFFFFF";
google_color_link = "6666CC";
google_color_url = "6666CC";
google_color_text = "000000";
//--></script><script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"></script></TD><TD>&nbsp;&nbsp;&nbsp;</TD><TD valign="top">
<DIV ALIGN="LEFT">
<A HREF="/">Home</A>
    &gt;
    <A HREF="../index.html">List of Packages</A>
    &gt;

    
    <A HREF="../java.awt.image/pkg.html">java.awt.image</A><font color="#666666" class="xsmall-font">
        &nbsp;[21 examples]
    </font>
        &gt;
        <B><A HREF="../java.awt.image/pkg.html#Images">Images</A></B><font color="#666666" class="xsmall-font">
            &nbsp;[6 examples]
            </font>
</DIV><P>
  <h3>e660. Creating an Image from an Array of Color-Indexed Pixel Values</h3>

This example demonstrates how to convert a byte array of pixel values
that are indices to a color table into an <code>Image</code>.  In particular,
the example generates the Mandelbrot set in a byte buffer and uses the
<code>MemoryImageSource</code> 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.


<pre>    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&lt;w; i++) {
                p[i] = p[i-1] + dp;
            }
    
            for (int r=0; r&lt;h; r++) {
                for (int c=0; c&lt;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 &lt; 512 &amp;&amp; xsqr + ysqr &lt; 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);
        }
    }
</pre>
Here's some code that uses the <code>Mandelbrot</code> class:

<pre>    class RunMandelbrot {
        static public void main(String[] args) {
            new RunMandelbrot();
        }
        RunMandelbrot() {
            Frame frame = new Frame("Mandelbrot Set");
            frame.add(new MyCanvas());
            frame.setSize(<font color="#0066ff"><i>300</i></font>, <font color="#0066ff"><i>200</i></font>);
            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);
                }
            }
        }
    }
</pre>
<P><table width="600" CELLSPACING="0" CELLPADDING="2" BORDER="0">
<tr>
<td bgcolor="#6666cc" align="center"><font color="#ffffff">
            &nbsp;<b>Related Examples</b></font></td>
</tr>
</table>


e661. <a class="eglink" href="HasAlpha.html?l=rel">
    Determining If an Image Has Transparent Pixels
</a>
<br>

e662. <a class="eglink" href="GetColorModel.html?l=rel">
    Getting the Color Model of an Image
</a>
<br>

e663. <a class="eglink" href="GetGifColors.html?l=rel">
    Getting the Transparent Pixel and Number of Colors Used in a GIF Image
</a>
<br>

e664. <a class="eglink" href="CropImage.html?l=rel">
    Getting a Sub-Image of an Image
</a>
<br>

e665. <a class="eglink" href="FilterRgb.html?l=rel">
    Filtering the RGB Values in an Image
</a>
<br>


<table width="600" CELLSPACING="0" CELLPADDING="2" BORDER="0">
<tr>
<td align="left">
<br>
        See also: 
<a class="eglink" href="/egs/java.awt.image/pkg.html?l=rel#Buffered%20Images">
    Buffered Images
</a>&nbsp;&nbsp;

<a class="eglink" href="/egs/java.awt.image/pkg.html?l=rel#Effects">
    Effects
</a>&nbsp;&nbsp;

<a class="eglink" href="/egs/java.awt.image/pkg.html?l=rel#Volatile%20Images">
    Volatile Images
</a>&nbsp;&nbsp;

</td>
</tr>
</table>

<br>

<br>
<FONT class="xsmall-font">
&copy; 2002 Addison-Wesley.
</FONT></TD><TD>&nbsp;&nbsp;&nbsp;</TD><TD valign="top"><A href="http://compositesw.com/devzone?ref=javaalmanac"><IMG alt="Click Here" height="600" width="120" border="0" src="/csw_oad_120x600_final.gif"></A></TD>
</TR>
</TABLE>
</BODY>
<HEAD>
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
<META CONTENT="NO-CACHE" HTTP-EQUIV="PRAGMA">
</HEAD>
</HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -