0

I'm making a game rendered in a trimetric perspective (like isometric, but one set of tile sides is steeper than the other). I want to add functionality for when the mouse rolls over tiles or objects (highlighting, pop-ups and the like).

Both the objects and tiles will have complicated shapes, and I'm looking for the best way to mark when the mouse passes over them, especially since top-most objects should take precedence over background objects.

Currently, the tiles and objects are rendered by the Graphics.drawImage function on a canvas after being loaded as buffered images, for every frame.

I keep seeing mouseEvent.getSource on the alternate explanations, but it seems to require creating graphics objects for every highlightable object. I'm concerned this may be inefficient when hundreds of objects and tiles are on the screen that can be potentially selected.

Specifically, my questions are these:

Is creating hundreds, if not thousands, of graphics objects per frame a bad way to use the getSource function?

Is there a better way to determine which instance of a class is being selected, given mouse coordinates? Such as a separate array of all pixels and the object/tile ID to which they belong (I estimated the size to be close to 3 MB, with constant updating as the screen moves around)?

  • You could mask the image and generate a "highligting" using a `AlphaComosite`, something like [this example](http://stackoverflow.com/questions/14225518/tinting-image-in-java-improvement/14225857#14225857) – MadProgrammer Jan 21 '15 at 03:25
  • It's not the effect I'm trying to replicate, it's the function. I want to identify what I have to highlight from the mouse information (window coordinates) and the map information (abstract object locations). – BermudaTri Jan 21 '15 at 03:28
  • 1
    Presumably you have you objects in some kind of list or other structure with which you can iterate through them. You need to ascertain the bounds of each image (x/y and width/height) that each image takes, you can use `java.awt.Rectangle` to maintain the information. You would then use the `contains` method (of `Rectangle`) to determine if the `MouseEvent#getPoint` is within a given objects bounds... – MadProgrammer Jan 21 '15 at 03:29
  • Also, remember, you will need to ensure that you are layering the object look ups, so that the the objects at the top get checked first. – MadProgrammer Jan 21 '15 at 03:30
  • This sounds like an answer! I assume I could use polygons as well as rectangles? As for the image order, I could just reverse the drawing order from the render method. EDIT: Wouldn't this mean running 'contains' on everything on the screen? Isn't that an expensive thing to do every frame? – BermudaTri Jan 21 '15 at 03:35
  • You should be able to and you'd have to test it, but unless the mouse pressed, would you bother checking? – MadProgrammer Jan 21 '15 at 04:06

0 Answers0