ElementOfMagic
ElementOfMagic

Reputation: 5

Draw arbitrary polygons instead of certain characters when painting a String

I need to draw a text line where some specified characters would be replaced by arbitrary polygons. These polygons must be painted with Graphics directly using drawPolygon. method. While Unicode contains a range of graphical symbols, they are not appropriate for this task.

I was wondering if it was possible to replace a character with a polygon, in any instance of that character's occurrence in a string? For example, if I typed-in the word 'Holly' and hit 'enter', the first letter would be replaced by the polygon. If I then went to type the word 'thistle', the polygon's new position would be in place of the second letter?

Any help/guidance will be greatly appreciated.

Upvotes: 0

Views: 303

Answers (3)

Marcel Stör
Marcel Stör

Reputation: 23565

Assuming the actual polygon is represented in Unicode all you have to do is string replacement.

System.out.println("Hello".replace('H', '\u25C6'));

produces

◆ello

Upvotes: 1

Audrius Meškauskas
Audrius Meškauskas

Reputation: 21778

This is fully doable with FontMetrics that allow to measure the dimensions of the string as printed with the given font. Use FontMetrics to determine to compute the locations of the string fragments and then draw the string fragments and polygons in between.

This approach seems reasonable if the polygons must be somehow very special (maybe non-Unicode characters?), or they required dimensions are very different from the letter dimensions in the used font.

In early days of Java when the Unicode support was yet not very good, it was not uncommon to draw unsupported national characters that way.

Upvotes: 0

vals
vals

Reputation: 64244

If you want to display a polygon, may be the easy way is to choose a Unicode symbol There are lots of them with graphic contents (even a snow man)

Upvotes: 0

Related Questions