[java] Setting Pixels?

Started by
12 comments, last by Melraidin 21 years, 4 months ago
Can anyone point me to where I could find how to set and get a pixel in a Java applet? I''ve been looking at the Image class and Graphics2D, but don''t think there''s anything in either of those to help me. Obviously I''ve just started with Java, so any help anyone?
Advertisement
Check out the setRGB method in the java.awt.image.BufferedImage class.

Last time I checked it was actually a bit faster to get the raster's data buffer and set the pixel there, but you will soon learn all that once you start looking at BufferedImage.

Btw, there are probably even faster ways to do it in JDK1.4 with its accelerated VolatileImage and its internal automatic image class but I have never done that. You may want to ask around at javagaming.org for info about doing that (pushing the moderators to republish the article that explains how to work with accelerated images).

[edited by - HenryAPe on November 3, 2002 4:43:27 PM]
I am not sure of the syntax cause I am to lazy to grab a book but lookup, the PixelGrabber class this will return an array of RGB values for an image.. basically raw pixel data. You can also create an image with that raw data(one of the Image constructor methods). So I think that should sovle your problem and it should work in every applet.
Well, since the original poster mentioned Graphics2D, he is already using Java version 1.2+ and should not have to bother with PixelGrabber.
So I should skip PixelGrabber and go work with VolatileImage? Or perhaps BufferedImage, although as I understand it VolatileImage will be faster.

Welp, no time right now to play with this, but will do some reading and let you all now how I fare later...

Thanks all
I have been looking for this too.
I gave up.
I found lots of examples of drawing lines and shapes on Graphics2D images though, so I''m just going with a function that draws a colored line 1 pixel wide+long. <:D Not the fastest way, but who cares? Better than the nothing I had before, and if I needed speed I''d be using C, not Java.
RPD=Role-Playing-Dialogue. It's not a game,it never was. Deal with it.
Eh, what''s so difficult about calling the setRGB method on a BufferedImage instance?
Volatile images are fast because they are hardware accelerated through DirectX (on Windows only though!!!) but I think that for that kind of op you want to use BufferedImage because they are better when the image data needs to be manipulated (VolatileImages reside in video memory so to modify them they need to be pulled out into conventional RAM, modifified and then placed back into video memory; so that defeats the whole purpose of VolatileImage).

Your best bet would be setting up a back buffer as a BufferedImage and then manipulating that image through the functions provided in BufferedImage. Hope that helps
______________________________"Crack a government encryption code on my laptop? Easy as really difficult pie." - Willow.------------------------------
quote:Eh, what's so difficult about calling the setRGB method on a BufferedImage instance?

-Well, it's completely impossible if you know nothing about it.
...
It's very common to find examples such as how to draw a line on one type of image, how to draw text on another type of image, and how to get/set a pixel on a third type of image. Now what'd be great is if there was an example that showed all three on the same image but so far I have not been able to find any, and the methods of the three can't be combined. I have a book on J2D, but it only shows how to draw lines, actual images (files) and text-- but no pixels.


[edited by - Lubb on November 10, 2002 4:55:54 PM]
RPD=Role-Playing-Dialogue. It's not a game,it never was. Deal with it.
Sorry about my earlier post, I guess I didn't pay enough attention. Hey if you know how to draw a line on an image then you alredy know how to set a pixel.. Just draw a line that is one pixel long with the color you are looking for.. Unless I am missing something ^_^, that should work for you. Although I think henry's solution would work better if you are doing a lot of pixels.

"Technological progress is like an ax in the hands of a pathological criminal."


[edited by - wrathnut on November 18, 2002 8:48:46 PM]

[edited by - wrathnut on November 18, 2002 8:49:30 PM]

This topic is closed to new replies.

Advertisement