Question

I have a drawString() method in my paintComponent method. Is there a way to make the text drawn by the drawString() bold? Also, is there a way to make the text bigger? I would like to avoid using JLabels, unless it is absolutely necessary.

Was it helpful?

Solution

According to documentation of drawString:

Draws the text given by the specified string, using this graphics context's current font and color. The baseline of the leftmost character is at position (x, y) in this graphics context's coordinate system

Indeed, Graphics class has the setFont(Font font) method available:

g.setFont(new Font("default", Font.BOLD, 16));

OTHER TIPS

You have to set the font before drawing the text.

g.setFont(font);

There are methods: setFont(Font) - Method in class java.awt.Component Sets the font of this component. setFont(Font) - Method in class java.awt.Container Sets the font of this container. setFont(Font) - Method in class java.awt.Graphics Sets this graphics context's font to the specified font.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top