aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Creating an Image by Plotting Pixels? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Creating an Image by Plotting Pixels?" Watch "Creating an Image by Plotting Pixels?" New topic
Author

Creating an Image by Plotting Pixels?

Don Gardner
Ranch Hand

Joined: Aug 30, 2001
Posts: 33
I want to plot a 256 x 256 pixel grayscale image from an array of bytes. Does anyone have any suggestions as to how to do this?
I've looked through the discussion group, and I haven't been able to find anything that helps me out. My gut feeling is to use a Canvas, but I can't figure out how to set specific pixels in the Canvas to a given color.
My experience with graphics in Java is limited to methods like drawOval() and drawRect(), so I need a good starting point.
Thanks for your help,
Don
Shashi Kanta
Ranch Hand

Joined: May 08, 2002
Posts: 89
just have a look at the PixelGrabber class. i think that would solve ur problem.
its very easy to use

rgds,
Shashi
meera sood
Ranch Hand

Joined: Dec 18, 2001
Posts: 50
hi!
i would like to expand on Shashi's answer...
the pixelgrabber class, as far as i have experienced, is to grab the pixel information from an image into an array. to get an image from an array of ints(where the ints contain color codes), you would use the createImage method e.g.

where pixels1 is the int array.
hope this helps.
meera
Don Gardner
Ranch Hand

Joined: Aug 30, 2001
Posts: 33
Thanks for the help! Here is what I've got so far. My program extends JFrame. I've inserted a JButton, a JTextField, and a Canvas into the JFrame. I would like to display the image on the Canvas (drawingArea), so I have overridden the paint( Graphics g ) method, as follows:

The code compiles and runs without error, but nothing gets displayed. What am I doing wrong?
Thanks for the help,
Don
Don Gardner
Ranch Hand

Joined: Aug 30, 2001
Posts: 33
I've just about gotten it all figured out now. It seems that the problem is not with how I am plotting the image, but with the data I am using to create the image. I need to do some dorking around to get the data into an approprate format, but at least I now know that is the problem.
Thanks for all of the help,
Don
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15300
    
    6

Just a word of advice. Canvas is an AWT Component. It is not recommended to mix SWING and AWT components. You may not be having problems now, but it could reak havoc on you later.
Just wanted to give you a heads up.


GenRocket - Experts at Building Test Data
meera sood
Ranch Hand

Joined: Dec 18, 2001
Posts: 50
hi!
in your example, the intArray would contain color codes, for ex. if you want to render a black-and-white image, it would contain a series of "0xffffffff" and "0xff000000", because this is the format for ints that the MemoryImageSource recognizes.
you can have the color codes of the other colors in the array in the same way.
hope this helps...
meera
Shashi Kanta
Ranch Hand

Joined: May 08, 2002
Posts: 89
hi Don,
why don't u set the value of 'g' inside ur paint method to the Graphics context of the canvas?
ie
g=drawingArea.getGraphics();
g.drawImage....
hope u manage seeing the image now.
rgds
Shashi
Don Gardner
Ranch Hand

Joined: Aug 30, 2001
Posts: 33
I wasn't using the graphics handle for the canvas in the code I posted because I made a simpler example of the program to test functionality. I was placing the image on the JFrame.
I can't remember where I saw it, but I did see that one should not mix AWT and Swing components. I am planning to replace the canvas with a JPanel in my finished program.
As for the color scaling, does anyone have any detained information about the format of the colors? I know that they are ints, but what color is what int? The documentation doesn't give any detail, and doesn't give any references.
Thanks for all of the great advice.
Don
Chris Shepherd
Ranch Hand

Joined: Jun 27, 2000
Posts: 286
I think the int you need is just one of the color hex values. Check this page for a basic explaination of the color codes and check this page for 216 basic colors in RGB and hex.
to input the values as hex in java, use something like this:

Thats yellow by the way. and the hex indicator is a zero-x, not letter O-x.
Hope this is what you needed to know.
[ June 20, 2002: Message edited by: Chris Shepherd ]
Don Gardner
Ranch Hand

Joined: Aug 30, 2001
Posts: 33
Thanks for the link, Chris! Just what I needed.
Each one of my pixels has an 8-bit values attached to it, and I want the image to be grayscale, so this is how I am defining the color value:

Where tempInt is the original int that varies between 0 and 255. Now for some reason, the image is coming out in shades of purple rather than shades of gray.
Any clues why?
Thansk,
Don
Chris Shepherd
Ranch Hand

Joined: Jun 27, 2000
Posts: 286
I think you might be shifting too far. Shouldn't the middle shift be 8 bits instead of 16? and then the leftmost shift would be 16 bits?
I think what you're getting is like 0xFF00FF(purple) and variants of that. I think the color code chops off everything above 24 bits, so with your code, you are losing the high end bits you try to set and never setting the mid range bits. That would give you the variants of purple you are seeing I think.
[ June 20, 2002: Message edited by: Chris Shepherd ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Creating an Image by Plotting Pixels?