| |

VerySource

 Forgot password?
 Register
Search
View: 685|Reply: 2

How to get drawstring into a rectangular area? And centered

[Copy link]

2

Threads

3

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-3-16 09:30:01
| Show all posts |Read mode
Dear friends, how to get the drawstring into a rectangular area? And centered. Replace the excess with "..."

Please give me ideas to solve, or give the best code, thank you!
Reply

Use magic Report

0

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-6-25 03:15:01
| Show all posts
FontMetrics fm = label.getFontMetrics(font); // font is the font you use
int strWidth = fm.stringWidth("String"); // This can get the length of the string (pixels), the height of the string is generally the size of the font
int width = 200; // length of rectangle
int high = 50; // the height of the rectangle
drawString("String", (width-strWidth) / 2, (high-font.getSize()) / 2 + font.getSize());
Reply

Use magic Report

1

Threads

51

Posts

32.00

Credits

Newbie

Rank: 1

Credits
32.00

 United States

Post time: 2020-6-28 14:15:02
| Show all posts
drawString("String", X, Y); can solve the centering problem
Use "..." for the excess part ==> If the figure is exceeded, it is not displayed, only the word exceeds...
It is recommended to draw on a JPanel   add(JPanel, BorderLayout.center)
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list