Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Performance and the fly likes Multiple MouseListeners vs single listener Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Performance
Bookmark "Multiple MouseListeners vs single listener" Watch "Multiple MouseListeners vs single listener" New topic
Author

Multiple MouseListeners vs single listener

Unnar Björnsson
Ranch Hand

Joined: Apr 30, 2005
Posts: 164
I am making a little tile-based game where each tile is a JPanel and I need to know which tile was clicked with the mouse. What I did is have each tile implement the MouseListener Interface, that way I can simply use event.getComponent() to get the correct instance. However since each map in this game can have like 50x50 tiles (2500 tiles) I wonder if this is memory demanding and perhaps I should just use one listener and map the click coordinates to the correct tile on the map.
What do you think?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

I think that 2,500 listeners is a trivially small number when you have 2,000,000,000 bytes to fit them into.

There might be other reasons to use only one listener for the whole panel, or other reasons to use one listener per tile, but I don't think that memory usage is really important.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Multiple MouseListeners vs single listener
 
Similar Threads
Struts Tiles Inheritance problem
Collision detection
What should a 2d tile-based map basically have???
Need help with centering screen
Developing Games in Java does anyone know if this book is any good?