0

I am drawing some strings on a JPanel, but I dont want to these string to be drawn over each other.

How can I check if there is a string on a point on my panel?

Thanks!

Milan Bon
  • 11
  • 1

1 Answers1

0

You need keep track on all strings and their positions, and then use java.awt.FontMetrics to calculate a bounding box of your strings. Then all you have to do is to check the bounding boxes for intersection.

AvrDragon
  • 7,139
  • 4
  • 27
  • 42
  • That wont wotk for me, because I have A LOT of strings :) and my application must be fast. I dont know if this FontMetrics is fast? p.s. I am workting on a google maps like program. – Milan Bon May 15 '12 at 13:54
  • I suppose it is faster then paint to buffer a whole JPanel and then check the color of every pixel. – AvrDragon May 15 '12 at 13:59
  • You have no other choice, for my opinion, you can cache the results if you want to speed things. either, you can fake it a little, and consider the font as a monospace with avarage char width.. therefore myCharWidth * myCityName.length = width.. the height is easy unless you have multiline texts or text wrapping. – Tomer W May 15 '12 at 14:26