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

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

时间: 2024-10-15 13:59:38

e668. 在一组像素中创建缓冲图像的相关文章

e667. 在给定图像中创建缓冲图像

An Image object cannot be converted to a BufferedImage object. The closest equivalent is to create a buffered image and then draw the image on the buffered image. This example defines a method that does this. // This method returns a buffered image w

e666. 创建缓冲图像

A buffered image is a type of image whose pixels can be modified. For example, you can draw on a buffered image and then draw the resulting buffered image on the screen or save it to a file. A buffered image supports many formats for storing pixels.

e675. 翻转缓冲图像

// To create a buffered image, see e666 创建缓冲图像 // Flip the image vertically AffineTransform tx = AffineTransform.getScaleInstance(1, -1); tx.translate(0, -image.getHeight(null)); AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE

【OpenCV入门教程之六】 创建Trackbar & 图像对比度、亮度值调整(转)

本系列文章由@浅墨_毛星云 出品,转载请注明出处. 文章链接:http://blog.csdn.net/poem_qianmo/article/details/21479533 作者:毛星云(浅墨)    邮箱: [email protected] 写作当前博文时配套使用的OpenCV版本: 2.4.8 这篇文章中我们一起学习了如何在OpenCV中用createTrackbar函数创建和使用轨迹条,以及图像对比度.亮度值的动态调整. 文章首先详细讲解了OpenCV2.0中的新版创建轨迹条的函数c

opencv ,亮度调整【【OpenCV入门教程之六】 创建Trackbar & 图像对比度、亮度值调整

http://blog.csdn.net/poem_qianmo/article/details/21479533 [OpenCV入门教程之六] 创建Trackbar & 图像对比度.亮度值调整 标签: opencvvs2010c++图像处理 2014-03-18 21:43 43189人阅读 评论(99) 收藏 举报  分类: [OpenCV](18)  目录(?)[+] 本系列文章由@浅墨_毛星云 出品,转载请注明出处. 文章链接:http://blog.csdn.net/poem_qian

uwp开发:截取当前屏幕中需要的图像并保存至应用内存储

在uwp开发中,有时候需要获取当前屏幕中的图像信息,但是又不适合直接截图保存,因为截图会保存整个屏幕的图像,而且,还需要用户会截屏操作.总之不适合获取屏幕中需要的图像信息.注意题目中的“需要的”. 意思是什么呢?就是我们可以获取当前屏幕中任意一个UIElement中的图像.废话不多说,还是以实战场景为例,因为自己最近就遇到了这种情况. 在做<简影UWP>的“电影台词”模块的时候,显示如下: 需求是:是用户点击保存图片,将会把图片和文字一块保存下来,查看的时候,也是当前显示的这样. 首先贴上前台

Android Multimedia框架总结(三)MediaPlayer中创建到setDataSource过程

转载请把头部出处链接和尾部二维码一起转载,本文出自:http://blog.csdn.net/hejjunlin/article/details/52392430 前言:前一篇的mediaPlayer框架,对于各个模块的关系,得先从核心类MediaPlayer铺开,同样看下今天的Agenda: MediaPlayer从create到setDisplay时序图 MediaPlayer的create过程 MediaPlayer的setDataSource过程 MediaPlayer的setDispl

求一组图形中的最大面积

package oo.day06;//求一组图形中的最大面积public class ShapeTest { public static void main(String[] args) { //Shape s = new Shape(); //编译错误,抽象类不能被实例化 Shape[] shapes = new Shape[4]; //创建Shape数组对象 shapes[0] = new Circle(1); //向上造型 shapes[1] = new Circle(2); shapes

ios中创建可以拖动的view原理和实现详解

有时候我们会需要在界面上拖动view;uiview是继承于uiresponder的,所以可以响应触摸相关的事件. 重点是以下一组方法: - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event - (void)touchesEnded:(NSSet *)touches withEvent:(UIE