This is not the way to do good collision detection.
There is a good article on pixel level detection at Java Game Engineering
http://www.javage.net/You only need to use PixelGrabber with Image, with a BufferedImage you can use getData().getPixel(x,y,pixel).
PixelGrabbers work with any kind of image with something like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| int width = = img.getWidth(null); int height = = img.getHeight(null); int[] imageData = new int[width * height]; PixelGrabber pg = new PixelGrabber(img, 0, 0, width, height, imageData, 0, width); try { pg.grabPixels(); } catch (InterruptedException e) { System.err.println("interrupted waiting for pixels!"); } for(int i=0; i<width; i++) { for(int j=0; j<height; j++) { int pixel = imageData[i + (j * width)]; int alpha = (pixel >> 24) & 0xff; int red = (pixel >> 16) & 0xff; int green = (pixel >> 8) & 0xff; int blue = (pixel ) & 0xff; } } |