With BufferedImages, by creating a WritableRaster, how do you guarantee that it will be compatible with a particular ColorModel?

I am studying the use of BufferedImages in java and trying to create an animation in which each frame of the animation is the result of a mathematical game with pixel data. I just play really. I originally used an indexed ColorModel, but I changed it (to take advantage of more colors) to the direct ColorModel. But now an error occurs: "

Raster sun.awt.image.SunWritableRaster@29c204 is not compatible with ColorModel DirectColorModel: rmask = ff0000 gmask = ff00 bmask = ff amask = ff000000

The code I used to create the BufferedImage and WriteableRaster is here:

public void initialize(){
    int width = getSize().width;
    int height = getSize().height;
    data = new int [width * height];

    DataBuffer db = new DataBufferInt(data,height * width);
    WritableRaster wr = Raster.createPackedRaster(db,width,height,1,null);
    image = new BufferedImage(ColorModel.getRGBdefault(),wr,false,null);
    image.setRGB(0, 0, width, height, data, 0, width);
}
+1
1

, WritableRaster, ColorModel, - , :

ColorModel colorModel = ColorModel.getRGBdefault(); // Or any other color model
WritableRaster raster = colorModel.createCompatibleWritableRaster(width, height);

, , , , DataBuffer . java.awt.image.BufferedImage createCompatibleWritableRaster ColorModel (, , :-). , .

:

Raster.createPackedRaster(db,width,height,1,null);

... , MultiPixelPackedSampleModel 1 ... , , RGB. , :

int[] masks = new int[]{0xff0000, 0xff00, 0xff}; // Add 0xff000000 if you want alpha
Raster.createPackedRaster(db, width, height, width, masks, null); 

PS: image.setRGB , data .

+1

All Articles