Testing for transparent pixel in a JPEG

Hi,

I have a JPanel where a user clicks with the mouse; I check if there is an object (a JPEG image) at this coordinate and if the line segment connecting the current and last clicked coordinate intersects an object.

Let us assume my JPEG image has transparent pixels - if the user clicks at one of these transparent pixels then this shouldn't be a collision. Is there a method in Java 6 testing for transparent pixels?

Thanks!

[453 byte] By [SFLa] at [2007-11-27 10:29:15]
# 1

Well first of all, JPG's don't really have transparent pixels... but if you are using JPG's, I'm gathering that you are considering a particular color in the jpg to be transparent. If this is not what you mean, you should be using GIF or PNG.

Regardless of the underlying picture format, one mechanism for doing this is to use a BufferedImage to hold the picture, then use the getRGB() method of the BufferedImage to determine the color of the pixel at the appropriate point to see if it is the "transparent" color. Don't forget to also treat points that are outside the bounds of picture as transparent.

markt1964a at 2007-7-28 17:56:24 > top of Java-index,Security,Cryptography...
# 2

Hello,

Lucky for you, i posted the same question 2 weeks ago and got a solution :-) . Although, im using GIFs. My problem was that i had a shape on a GIF with transparent pixels, and only wanted a mouse collision if the mouse was not on a transparent pixel.

My solution can be seen here:

http://forum.java.sun.com/thread.jspa?threadID=5190471&tstart=15

In short, Java doesnt have a method for doing what you are asking. You have to make it yourself. With the help of a couple of sources, i was able to make a method which i think is relatively easy to understand, as you will see when you view my post.

Hope this helps.

jazza_guya at 2007-7-28 17:56:24 > top of Java-index,Security,Cryptography...
# 3

Thanks for your replies, jazza_guy, thanks for posting the link to your solution :)

If I work with a graphic in, e.g., PNG-format - could I check whether a java.awt.geom.Line2D intersects the image?

SFLa at 2007-7-28 17:56:24 > top of Java-index,Security,Cryptography...
# 4

The solution i used works for PNG's as well as the getImage method also can load PNGs.

PNGs are good because they can use partial transparency etc. however, my solution would detect a mouse over a PARTIALLY transparent pixel as a collision. the pixel must be totally transparent for my methods to not detec a collission.. if that makes sense.

jazza_guya at 2007-7-28 17:56:24 > top of Java-index,Security,Cryptography...
# 5

Have you tried using a Line2D for collision detection, e.g. whether a Line2D intersects a PNG?

Thanks!

SFLa at 2007-7-28 17:56:24 > top of Java-index,Security,Cryptography...