Arghhh! Ok I am completely frustrated! I have looked all over and I can’t find anywhere that tells how to do this! (At least not in a way that I understand!) I have a byte array that I convert into a DataBuffer and I try to write that to a BufferedImage, but it keeps telling me:
Exception in thread "main" java.lang.IllegalArgumentException: Raster sun.awt.image.SunWritableRaster@1c247a0 is incompatible with ColorModel DirectColorModel: rmask=ff0000 gmask=ff00 bmask=ff amask=ff000000 at java.awt.image.BufferedImage.<init>(Unknown Source) at Main.<init>(Main.java:178) at Main.main(Main.java:41)
Here is my code:
fStream = new FileInputStream("weather.buf");
OIS = new ObjectInputStream(fStream);
pattern = (byte[][])OIS.readObject();
weather = new byte[pattern.length*pattern[0].length*4];
int a = 0;
for(int y = 0; y < pattern[0].length; y++)
{
for(int x = 0; x < pattern.length; x++)
{
switch(pattern[x][y])
{
case LT_GREEN:
weather[a] = (byte)135; //R
weather[a+1] = (byte)186; //G
weather[a+2] = (byte)120; //B
weather[a+3] = (byte)255; //A
a += 4;
break;
case GREEN:
weather[a] = (byte)95; //R
weather[a+1] = (byte)158; //G
weather[a+2] = (byte)75; //B
weather[a+3] = (byte)255; //A
a += 4;
break;
case DK_GREEN:
weather[a] = (byte)25; //R
weather[a+1] = (byte)75; //G
weather[a+2] = (byte)14; //B
weather[a+3] = (byte)255; //A
a += 4;
break;
case YELLOW:
weather[a] = (byte)255; //R
weather[a+1] = (byte)255; //G
weather[a+2] = (byte)0; //B
weather[a+3] = (byte)255; //A
a += 4;
break;
case ORANGE:
weather[a] = (byte)255; //R
weather[a+1] = (byte)153; //G
weather[a+2] = (byte)0; //B
weather[a+3] = (byte)255; //A
a += 4;
break;
case RED:
weather[a] = (byte)255; //R
weather[a+1] = (byte)0; //G
weather[a+2] = (byte)0; //B
weather[a+3] = (byte)255; //A
a += 4;
break;
default:
weather[a] = (byte)0; //R
weather[a+1] = (byte)255; //G
weather[a+2] = (byte)0; //B
weather[a+3] = (byte)0; //A
a += 4;
break;
}
}
}
ColorModel colorModel = ColorModel.getRGBdefault();
DataBuffer weatherData = new DataBufferByte(weather, weather.length);
SampleModel sampleModel = new ComponentSampleModel(DataBuffer.TYPE_BYTE, pattern.length, pattern[0].length, 1, 1, blah);
WritableRaster raster = Raster.createWritableRaster(sampleModel, weatherData, new Point(0, 0));
BufferedImage weatherImage = new BufferedImage(colorModel, raster, false, null);
I am completely Lost!!!