Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make drawString() text bold?

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.

like image 802
reesjones Avatar asked Feb 10 '13 19:02

reesjones


People also ask

What is drawString () method?

DrawString(String, Font, Brush, Single, Single, StringFormat) Draws the specified text string at the specified location with the specified Brush and Font objects using the formatting attributes of the specified StringFormat.

What does drawString mean in Java?

void drawString(String str, int x, int y) The drawString() method, shown below, takes as parameters an instance of the String class containing the text to be drawn, and two integer values specifying the coordinates where the text should start.

How do I make text bold in word?

Select the text that you want to make bold, and do one of the following: 1 Move your pointer to the Mini toolbar above your selection and click Bold Bold icon . 2 Click Bold Bold icon in the Font group on the Home tab. 3 Type the keyboard shortcut: CTRL+B. See More...

Is it possible to draw all text in a string?

No, this is not possible. The DrawString function is going to draw all of the text you specify using the formatting flags that you specify. It's an all-or-nothing affair. If you want to make some portions bold and other portions not, then you will need to make multiple calls to the DrawString function.

How do I add text to a Facebook post without bold?

Click “Copy” at the top right of the Output box or copy the formatted text manually by right clicking or pressing “CTRL + C” to copy the text to your clipboard. Go to Facebook.com and enter whatever text you want to have that’s not bold.

How to make a font thinner in illustrator?

How to make a font thinner in Illustrator? You can make a font thinner using the same method as bold text. Create Outline > Effect > Offset Path . Change the number to negative, and your font will be thinner. Bold is beautiful and powerful. You can either use it to catch attention or as graphic background and design element.


3 Answers

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));
like image 109
Jack Avatar answered Oct 19 '22 22:10

Jack


You have to set the font before drawing the text.

g.setFont(font);
like image 29
Dan D. Avatar answered Oct 19 '22 23:10

Dan D.


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.

like image 43
Dumas45 Avatar answered Oct 19 '22 22:10

Dumas45