Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

How is it possible (with the standard Java2D engine) to use small sprites like graphic objects?

For those who don't know what I mean, in all those Flash-games like on Facebook they put small sprites on the screen which react to mouse-over and clicks. I tried to do the same in Java but can't find a good method. Swing components always spread over the whole bitmap, but I only want to get a reaction from the object when the mouse is over a pixel that's not transparent.

So basically checking every time if the object below the mouse contains a non-transparent pixel (which i believe could be pretty intense in a gameloop or repaint loop). I have no idea how to implement such a thing efficiently.

share|improve this question
add comment

3 Answers

You could try a reduced bounding box. Let's say you have a 20x20 px sprite, with 2 px of transparency around the borders. Set up a 18x18 px bounding box, and check at the end of every buffer update if the mouse pointer coordinates are included in the bounding box coordinates.

share|improve this answer
    
I thought about generating a shape, maybe with some bezier cuves or something like that, but failed to find a proper way to implement such a shape. Shape would give the neccesary methods tho. –  user5907 Mar 7 '11 at 22:09
add comment

You could use PulpCore, a lightwight 2D framework, which will let you use sprites really easily:

http://www.interactivepulp.com/pulpcore/

share|improve this answer
add comment

Is it really that expensive to check one pixel for transparency every frame? Of course, you only check if the cursor is within the sprites bounding box.

Assuming you use BufferedImage to hold your image data, you can dom something along this (sorry, code is really not exact, look up how BufferedImage work):

image.getRaster().getPixel(x, y, array);
alpha = array[4]
if (alpha > 0.5f) { ...

Area can be used for operations between shapes (such as add and subtract). It's fairly easy to make a algorithm that cover an area with as few rectangles as possible (if you don't want it to have the guaranteed best solution). Cover all opaque pixels with rects and leave out the transparent ones. Use the Area class for unifying them into a single path. BUT, I think that checking the sprite for transparency is more efficient.

share|improve this answer
    
+1, though you might find "if ((image.getRGB()>>>24)>128) ..." works a bit faster –  mikera Mar 14 '11 at 20:21
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.