• 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 all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

Graphics Mouse Click and Location

Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to detect a mouse click and location on a item I've drawn with the Graphics class.
For example, I first draw a small rectangle like fillRect(50, 50, 50, 50); Then I do some more drawing inside the rectangle.
I will have several rectangles on the screen.
Later the mouse is clicked somewhere on a rectangle.
How can I detect that the mouse was clicked and clicked on the rectangle? How can I determine which rectangle was clicked?
Thank You in Advance for Your Help,
[ May 04, 2004: Message edited by: Lou Pe ]
Posts: 9612
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can either keep a list of all your "clickable areas" coordinates and, given some mouse click coordinate, do some math on each of them to see if they were clicked in, or you could make your rectangles "custom components" and add mouse listeners to each of them. Take a look at the Custom Component trail in the Java Tutorial for more on the latter option.
His brain is the size of a cherry pit! About the size of this ad:
Java file APIs (DOC, XLS, PDF, and many more)
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!