码迷,mamicode.com
首页 > 其他好文 > 详细

e660. 用一组像素创建图像

时间:2018-09-03 00:00:15      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:像素   run   cell   geo   context   news   orm   zed   c++   

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. 用一组像素创建图像

标签:像素   run   cell   geo   context   news   orm   zed   c++   

原文地址:https://www.cnblogs.com/borter/p/9575562.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!