16-bpp grayscale draw to canvas

Hi everyone,

Before I begin, I did look thru the tutorial info and am so lost that I don't even know what to look for. Now, let me describe my task...

I've been assigned a simple project: read in a text file containing integers that each represent 16bpp pixel grayscale values. The integers are delimited by commas and newline characters that mirror the width/height of the image exactly. For example, the image is 10x5 and the file looks like this (these are bogus values for example only):

40523,45645,34564,54654,56456,34545,69235,65456,43435,53765

40523,45645,34564,54654,56456,34545,69235,65456,43435,53765

40523,45645,34564,54654,56456,34545,69235,65456,43435,53765

40523,45645,34564,54654,56456,34545,69235,65456,43435,53765

40523,45645,34564,54654,56456,34545,69235,65456,43435,53765

I created a canvas (JPanel) that is 10x5 and wrote an algorithm to do this:

- read one 16bpp integer at a time into a buffer

- convert each 16bpp value to a 24bpp value (i.e. 40523 becomes 10395294)

- get the color represented by the 24bpp value, i.e. Color color = new Color(10395294);

- set graphics color of panel, i.e. panel.getGraphics().setColor(color);

- draw a pixel, i.e. panel.getGraphics().drawRect(x,y,1,1);

Of course, this isn't working - my entire panel draws solid black. When I found that "color.getRGB()" was returning a bogus negative number, I realized that I must not be obtaining a valid color. So then I started researching online. I started seeing info about BufferedImage (sounds eerily like what I should be using), ColorModel, Raster, and SampleModel. It's just too much to go chasing unless I know I'm going in the right directions. That's where, hopefully, someone here can offer me some advice.

Questions:

1. Is it possible to simply draw an image to a canvas by setting the color of the graphics and then drawing a pixel? I've already got all the logic in place to do this.

2. Is getGraphics().drawRect(x, y, 1, 1) essentially the same as drawing one pixel?

3. Have I gone wrong somewhere in my conversion from a 16bpp grayscale integer to a 24bpp integer? Someone at work advised me to use the following method to convert the 16bpp integer 40523:

40523 / 256 = 158

binary value of 158 = 10011110

append this string 3 times to get: 100111101001111010011110

convert binary string to integer > 10395294 (this is the 24bpp integer)

Please help! Thanks much...

[2531 byte] By [codeFastera] at [2007-11-15]