Java Question: Reading pixels from a buffer

If you have any questions on programming, this is the place to ask them, whether you're a newbie or an experienced programmer. Discussion on programming in general is also welcome. We will help you with programming homework, but we will not do your work for you! Any porting requests must be made in Developmental Ideas.
Post Reply
Pyrosurfer
DCEmu Nutter
DCEmu Nutter
Posts: 964
Joined: Sun Aug 11, 2002 5:07 pm
Has liked: 0
Been liked: 0

Java Question: Reading pixels from a buffer

Post by Pyrosurfer » Fri Dec 17, 2004 3:13 pm

Is there a way that I can get the RGB value of a pixel on a Graphics or Image buffer? I have read that the BufferedImage data type allows for this with the getRGB method, but I don't know how to load an image (gif, jpeg) with that data type.

I am trying to create a game with sprites. Blankspaces in the gif will be represented by white. I want to check for {255,255,255} white pixels. I will replace these pixels with the respective pixels from a background gif.
User avatar
Ender
DCEmu Super Poster
DCEmu Super Poster
Posts: 1314
Joined: Mon Dec 10, 2001 4:01 pm
Location: Canada, first igloo on your left.
Has liked: 0
Been liked: 0
Contact:

Post by Ender » Fri Dec 17, 2004 3:26 pm

I know this isn't really helpful at all, but given the differences between Java and C++, I'd just recommend making the jump to C++ (it isn't really that big of a jump) and doing it there. I personally hate Java for doing anything graphical.
Image
Pyrosurfer
DCEmu Nutter
DCEmu Nutter
Posts: 964
Joined: Sun Aug 11, 2002 5:07 pm
Has liked: 0
Been liked: 0

Post by Pyrosurfer » Fri Dec 17, 2004 3:43 pm

I know both Java and C++. I am doing this in java. It really would be a fall to C++. Please no more "use another language" comments.
User avatar
Ender
DCEmu Super Poster
DCEmu Super Poster
Posts: 1314
Joined: Mon Dec 10, 2001 4:01 pm
Location: Canada, first igloo on your left.
Has liked: 0
Been liked: 0
Contact:

Post by Ender » Fri Dec 17, 2004 3:55 pm

okee doke. sorry bout that. have you tried getRGB?

<nevermind, I really should read...>

BufferedImage(int width, int height, int imageType) seems fairly basic to load a gif or a jpeg to me...

Looking through the java docs, I can't see anything in Image or Graphics that would allow you to do the same, though who knows, you may be able to cast the object somehow....

found this if you're interested in trying BufferedImage http://forum.java.sun.com/thread.jspa?t ... ID=1053770
Pyrosurfer
DCEmu Nutter
DCEmu Nutter
Posts: 964
Joined: Sun Aug 11, 2002 5:07 pm
Has liked: 0
Been liked: 0

Post by Pyrosurfer » Fri Dec 17, 2004 4:20 pm

I might try that, thanks.
Well its not just loading the image, the BufferedImage type can't use any of Images methods. It doesn't make sense, it is a subclass of image.

I think you also have to use a Graphics2d object for BufferedImage, so i am hoping there is another way.

EDIT: I just noticed this is for swing, I am using awt. I know swing as far as GUI stuff/textbox. I might have to make the transition to swing.

EDIT: Maybe I could draw the gif pixel by pixel? There might be a method to get a color of a specified pixel from a gif file (rather than loading to an image buffer first). I could check it, if it is not white it will be drawn to a Graphics object.
Kamjin
DC Developer
DC Developer
Posts: 216
Joined: Wed Dec 17, 2003 5:27 am
Has liked: 0
Been liked: 0

Post by Kamjin » Sun Dec 19, 2004 4:39 pm

Pyrosurfer
DCEmu Nutter
DCEmu Nutter
Posts: 964
Joined: Sun Aug 11, 2002 5:07 pm
Has liked: 0
Been liked: 0

Post by Pyrosurfer » Sun Dec 19, 2004 4:52 pm

I feel like an idiot.

I didn't know you could put transparent pixels in a gif. That'll save a bit of code. Now I just need to learn how to make my image transparent.
Kamjin
DC Developer
DC Developer
Posts: 216
Joined: Wed Dec 17, 2003 5:27 am
Has liked: 0
Been liked: 0

Post by Kamjin » Mon Dec 20, 2004 2:09 am

Someone who codes java more often should jump in, but I remeber there being a mask option in the drawimage() function, which should allow you to specify which pallette index to consider transparent.. well if no-one else jumps in it at least gives you something to search for. :D
ATani
DCGen Creator
DCGen Creator
Posts: 66
Joined: Sat Jan 19, 2002 12:54 am
Location: Near Yosemite, CA
Has liked: 0
Been liked: 0
Contact:

Post by ATani » Mon Dec 20, 2004 3:25 am

Pyrosurfer wrote:I feel like an idiot.

I didn't know you could put transparent pixels in a gif. That'll save a bit of code. Now I just need to learn how to make my image transparent.
Try this snippet of code... I have not tried it myself but it looks like what you are needing:

http://www.rgagnon.com/javadetails/java-0265.html
Pyrosurfer
DCEmu Nutter
DCEmu Nutter
Posts: 964
Joined: Sun Aug 11, 2002 5:07 pm
Has liked: 0
Been liked: 0

Post by Pyrosurfer » Mon Dec 20, 2004 6:44 am

Thanks Atani and Kamjin, I will try both approaches.
Pyrosurfer
DCEmu Nutter
DCEmu Nutter
Posts: 964
Joined: Sun Aug 11, 2002 5:07 pm
Has liked: 0
Been liked: 0

Post by Pyrosurfer » Mon Dec 20, 2004 2:14 pm

I made an animation of sonic running at a fairly moderate speed across a background today. I used the approach that kamjin linked to, it works great. Probably will make a basic sonic game of some sort.
Post Reply