Creating a BufferedImage with a DataBuffer!!!

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!!!

If I get the graphics context of the BufferedImage there is a method called drawBytes(), but that is apparently used to draw charcters to the image! Does anyone have any ideas where I would even look for this, or if it’s even possible?

Just use BufferedImage.setRGB(int x, int y, int argb), it wil save you all sorts of trouble - and will be faster because you won’t need the ‘weather’ intermediary array.

Turns out I was just being stupid and trying to make things a lot harder than they needed to be! This is what I do now and it seems to go pretty quickly! It only needs to update every 5 min so this works fine!


try
{
	fStream = new FileInputStream("weather.buf");
	OIS = new ObjectInputStream(fStream);
	pattern = (byte[][])OIS.readObject();

	weatherImage = new BufferedImage(pattern.length, pattern[0].length, BufferedImage.TYPE_4BYTE_ABGR);
	Graphics g = weatherImage.getGraphics();
	for(int y = 0; y < pattern.length-1; y++)
	{
		for(int x = 0; x < pattern[y].length-1; x++)
		{
			switch(pattern[y][x])
			{
				case LT_GREEN:
					g.setColor(new Color(135, 186, 120));
					break;
				case GREEN:
					g.setColor(new Color(95, 158, 75));
					break;
				case DK_GREEN:
					g.setColor(new Color(25, 75, 14));
					break;
				case YELLOW:
					g.setColor(new Color(255, 255, 0));
					break;
				case ORANGE:
					g.setColor(new Color(255, 153, 0));
					break;
				case RED:
					g.setColor(new Color(255, 0, 0));
					break;
				default:
					g.setColor(new Color(153, 153, 153));
					break;
			}
			g.fillRect(x, y, 1, 1);
		}
	}
}
catch (FileNotFoundException e)
{
	e.printStackTrace();
}
catch (IOException e)
{
	e.printStackTrace();
}
catch (ClassNotFoundException e)
{
	e.printStackTrace();
}

I really appreciate your help though and I will definately use that in the future!