MouseListener needs to interact with many object Java
I have a program with many images drawn onto the screen, as well as other things. I have a mouse listener, but what is the most efficient way of detecting what has been clicked on the screen? Because if I have an image
100px x 50px @ starting at point
I can't say
if (x > 500 && x < 600) etc.. for EVERY image on screen?
Thank you for any help
One way to tackle this kind of problem efficiently is to use a QuadTree, which is a data structure that recursively subdivides the screen. This enables you to only check those images that are in roughly the right part of the screen.
Or a simpler approach would be to simply subdivide the screen into quarters or 16ths, and 'register' each image with the portions of the screen that it covers. This may be less effective if you have any large images, relative to the screen size.
This would probably only be effective if many of your images are static, since the quadtree needs recalculating when image move.
You may find that simply checking every single image is actually fast enough - you didn't say how many images you have, or how long it currently takes to check them all...