Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Tracking Multiple Objects  RSS feed

 
Foley Ma
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I could find lots of tutorials on drawing and interacting with a single graphical object, but not when you have several objects, so any help would be greatly appreciated.

I have a JPanel that draws many text, lines, and circles all over the screen. I use TextLayout, Lines2D, and Ellipse2D to place the objects on the JPanel. I want the objects to be interactive and listen to events. For example, when a user clicks on a string I want additional information to pop up that pertains to that string.

My question is, when a user clicks on the object, how do I know which object it clicks on? I create a TextLayout for each text. Is this the wrong approach? Every tutorial I read has one textlayout and check for hittests against the text layout. But those tutorial also only displays one string. Similar graphics tutorial I read can erase and redraw an object, because that is the only object they have on the screen.

But when I have tens of objects, how do I know exactly which object was clicked on? Would I have to look through each object and check for hit test? Thanks!
 
Paul Clapham
Sheriff
Posts: 22472
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Foley Ma wrote:But when I have tens of objects, how do I know exactly which object was clicked on? Would I have to look through each object and check for hit test?


Yes. In fact you would have to do that even if you only had two objects.
 
Foley Ma
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's what I suspected. Thank you!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!