e668. 在一组像素中创建缓冲图像

Posted borter

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了e668. 在一组像素中创建缓冲图像相关的知识,希望对你有一定的参考价值。

This example demonstrates how to convert a byte array of pixel values that are indices to a color table into a BufferedImage. In particular, the example generates the Mandelbrot set in a byte buffer and combines this data with a SampleModel, ColorModel, and Raster into a BufferedImage. 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 Mandelbrot2 {
        // Holds the generated image
        Image image;
    
        // 16-color model; this method is defined in
        // e660 用一组像素创建图像
        ColorModel colorModel = generateColorModel();
    
        public Mandelbrot2(int width, int height) {
            // Initialize with default location
            this(width, height, new Rectangle2D.Float(-2.0f, -1.2f, 3.2f, 2.4f));
        }
    
        public Mandelbrot2(int width, int height, Rectangle2D.Float loc) {
            // Generate the pixel data; this method is defined in
            // e660 用一组像素创建图像
            byte[] pixels = generatePixels(width, height, loc);
    
            // Create a data buffer using the byte buffer of pixel data.
            // The pixel data is not copied; the data buffer uses the byte buffer array.
            DataBuffer dbuf = new DataBufferByte(pixels, width*height, 0);
    
            // The number of banks should be 1
            int numBanks = dbuf.getNumBanks(); // 1
    
            // Prepare a sample model that specifies a storage 4-bits of
            // pixel datavd in an 8-bit data element
            int bitMasks[] = new int[]{(byte)0xf};
            SampleModel sampleModel = new SinglePixelPackedSampleModel(
                DataBuffer.TYPE_BYTE, width, height, bitMasks);
    
            // Create a raster using the sample model and data buffer
            WritableRaster raster = Raster.createWritableRaster(sampleModel, dbuf, null);
    
            // Combine the color model and raster into a buffered image
            image = new BufferedImage(colorModel, raster, false, null);//new java.util.Hashtable());
        }
    
        public void draw(Graphics g, int x, int y) {
            g.drawImage(image, x, y, null);
        }
    }

Here‘s some code that uses the Mandelbrot2 class:

    class RunMandelbrot2 {
        static public void main(String[] args) {
            new RunMandelbrot2();
        }
        RunMandelbrot2() {
            Frame frame = new Frame("Mandelbrot2 Set");
            frame.add(new MyCanvas());
            frame.setSize(300, 200) ;
            frame.setVisible(true);
        }
    
        class MyCanvas extends Canvas {
            Mandelbrot2 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 Mandelbrot2(newSize.width, newSize.height);
                        c.repaint();
                    }
                });
            }
    
            public void paint(Graphics g) {
                if (mandelbrot != null) {
                    mandelbrot.draw(g, 0, 0);
                }
            }
        }
    }

 

Related Examples

以上是关于e668. 在一组像素中创建缓冲图像的主要内容,如果未能解决你的问题,请参考以下文章

在实验室颜色空间中创建像素缓冲区

在 c# 中从图像中创建具有特定像素格式的位图而不加载图像

Java 灰度缓冲图像

e671. 在缓冲图像中存取像素

e660. 用一组像素创建图像

在python中创建圆圈以遮盖图像并计算每个圆圈内的像素