> MIME-Version: 1.0
> Content-Transfer-Encoding: 7bit
> X-Priority: 3
> X-MSMail-Priority: Normal
> X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4133.2400
> Date: Wed, 4 Oct 2000 14:26:25 -0500
> From: Ted Hill <[EMAIL PROTECTED]>
> Subject: [JAVA2D] DataBuffer question
> To: [EMAIL PROTECTED]
>
> Hello,
>
> I'm trying to create a BufferedImage from a 2D int array as follows:
>
> ====================================================
>         int width = 256;
>         int height = 256;
>
>         int [ ][ ] myData = new int[width][height];
>
> /*
>     load values into myData
> */
>
>         rgbImage  = new BufferedImage(width, height,
> BufferedImage.TYPE_INT_RGB);
>
>         WritableRaster wr = rgbImage.getRaster( );
>
>         DataBuffer db = new DataBufferInt(myData, myData.length);
>
>         wr.setDataElements(width, height, db);

The method, setDataElements, is used to set the x,y value of a pixel in
the raster.  If you want to set the entire image, you might want to do:
        wr.setDataElements(0, 0, width, height, myData);

To get rid of copying the data, you could do:

        DataBuffer db = newDataBufferInt(myData, myData.length);
        int[] bandMasks = new int[3];
        bandMasks[0] = 0xff0000;  // red
        bandMasks[1] = 0x00ff00;  // green
        bandMasks[2] = 0x000000;  // blue
        WritableRaster wr = Raster.createPackedRaster(db, width, height,
                                                      width, bandMasks, null);
        ColorModel cm = new DirectColorModel(24,
                                             0x00ff0000,        // Red
                                             0x0000ff00,        // Green
                                             0x000000ff,        // Blue
                                             0x0           // Alpha
                                             );
        rgbImage = new BufferedImage(cm, wr, false, null);

jeannette

> ====================================================
>
> the last line throws an exception:
>
> java.lang.ClassCastException: java.awt.image.DataBufferInt
>  at
> sun.awt.image.IntegerInterleavedRaster.setDataElements(IntegerInterleavedRas
> ter.java:269)
>
> Can some one help me? How do I feed a 2D int array to a BufferedImage?
>
> Ted Hill
> Software Engineer
> Tomotherapy
> [EMAIL PROTECTED]
>
> ===========================================================================
> To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
> of the message "signoff JAVA2D-INTEREST".  For general help, send email to
> [EMAIL PROTECTED] and include in the body of the message "help".

===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff JAVA2D-INTEREST".  For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".

Reply via email to