问题描述
我正在学习在Java中使用BufferedImages,并且试图创建一个动画,其中动画的每一帧都是数学上摆弄像素数据的结果.我只是在玩.最初,我使用的是带索引的ColorModel,但是我已经将其更改为直接使用ColorModel(以利用更多的颜色).但是现在出现了一个错误-
I'm learning to use BufferedImages in java, and am trying to create an animation where each frame of the animation is the result of mathematically fiddling around with pixel data. I'm just playing around really. Originally I was using an indexed ColorModel, but I've changed it (to take advantage of more colors) to a direct ColorModel. But now an error crops up saying -
光栅sun.awt.image.SunWritableRaster@29c204与ColorModel DirectColorModel不兼容:rmask = ff0000 gmask = ff00 bmask = ff amask = ff000000
Raster sun.awt.image.SunWritableRaster@29c204 is incompatible with ColorModel DirectColorModel: rmask=ff0000 gmask=ff00 bmask=ff amask=ff000000
我用来创建BufferedImage和WriteableRaster的代码在这里:
The code I was using 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);
}
推荐答案
确保您拥有与ColorModel
兼容的WritableRaster
的最简单方法是首先选择颜色模型,然后从像这样:
The easiest way to make sure you have a WritableRaster
that is compatible with your ColorModel
is to first choose the color model, then create a raster from it like this:
ColorModel colorModel = ColorModel.getRGBdefault(); // Or any other color model
WritableRaster raster = colorModel.createCompatibleWritableRaster(width, height);
但是,这可能不切实际,例如,在像您这样的情况下,您可以根据现有数组创建DataBuffer
.在那种情况下,我实际上建议您查看java.awt.image.BufferedImage
的构造函数的源代码以及不同的ColorModel
实现的createCompatibleWritableRaster
方法(这是我自学的方式:-).它显示了可以很好地协同工作的栅格和颜色模型的最常见组合.
However, this might not be practical, for example in situations like yours where you create a DataBuffer
from an existing array. In that case, I would actually recommend looking at the source code of the constructors of java.awt.image.BufferedImage
and the createCompatibleWritableRaster
methods of the different ColorModel
implementations (that's the way I taught myself how to do it :-). It shows the most common combinations of rasters and color models that work well together.
您的行:
Raster.createPackedRaster(db,width,height,1,null);
...似乎正在创建具有MultiPixelPackedSampleModel
且每像素1位的栅格...这两种可能都与RGB颜色模型不兼容.您想要的可能是:
...seem to be creating a raster with MultiPixelPackedSampleModel
and 1 bit per pixel... Both of these are probably incompatible with the RGB color model. What you want is probably want is:
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
数组作为其后备缓冲区.
PS: You shouldn't need to to do the image.setRGB
on the last line of your code, as the image is using your data
array as its backing buffer already.
这篇关于使用BufferedImages,在创建WritableRaster时如何确保与特定的ColorModel兼容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!