zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    scm工作流部署问题解决
    mysql 数据库时间慢了8小时
    Vue加了二级路由后,跳转后js好像都失效
    flutter 莫名其妙错误集锦
    confluence-6.7.1 install
    git idea 项目复原
    springboot 本地jar发布,打war包
    flutter 初探2--点击按钮打开新窗口
    [转载]无法解决 equal to 操作中 "Chinese_PRC_CI_AS" 和 "Chinese_PRC_CI_AS_KS_WS" 之间的排序规则冲突
    [转载]天赋秉异的人永远是少数
  • 原文地址:https://www.cnblogs.com/borter/p/9575562.html
Copyright © 2011-2022 走看看