Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Draw heart symbol with drawString

I've tried ♡, and \u2661 as text but I'm unable to draw the heart symbol.

g2d.setColor(Color.RED);
g2d.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 70));
g2d.drawString(text, 10, 10);
like image 639
Sorter Avatar asked Dec 17 '25 20:12

Sorter


1 Answers

The problem does not come with the fact that the heart isn't drawn. It is drawn but outside of the visible area.

You have specified a font size of 70 and you are drawing the heart at position (10, 10). The Graphics2D.drawString(str, x, y) Javadoc specifies:

The baseline of the first character is at position (x, y) in the User Space.

This means that the baseline for the heart will be at y = 10 but since you have specified a font size of 70, you won't see it.

You should draw the String at a higher y value, like 100 for example:

g2d.setColor(Color.RED);
g2d.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 70));
g2d.drawString(text, 10, 100);

Side-note: "\u2661" (♡) is the Unicode value for a heart. If you want to draw a filled heart instead, use "\u2665" (♥).

like image 109
Tunaki Avatar answered Dec 20 '25 10:12

Tunaki



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!