Archived

This topic is now archived and is closed to further replies.

Melraidin

[java] Setting Pixels?

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
-Well actually that won''t work, quite. If you use that method, there''s no way to use any colors but the basic few line colors. -That is, there''s no way to set the linecolor in (0-255, 0-255, 0-255) format. At least, that I have found.
There''s a big long list of hex colors in one javascript/HTML book I have, but the methods shown for using those colors in hex format in that book do not work at all.

Share this post


Link to post
Share on other sites

  
Graphics2D g2d = (Graphics2D)g;
g2d.setColor (new Color (0.0f, 1.0f, 0.0f));
g2d.fillRect (1, 1, X, Y);


Assuming g is the Graphics object passed to the paint method of an applet. Otherwise just use the Graphics2D object you already have.

Share this post


Link to post
Share on other sites