• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Multiple MouseListeners vs single listener

 
Ranch Hand
Posts: 164
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Marshal
Posts: 28295
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Hey, I'm supposed to be the guide! Wait up! No fair! You have the tiny ad!
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic