?
Solved

Draw string with Graphics2D

Posted on 2004-09-16
7
Medium Priority
?
233 Views
Last Modified: 2010-03-31
Hi,

I'm having a major obstacle in my Swing application. According to the API, the drawString method in Graphics2D will make the lefthand side of the baseline the x and y (x and y are the arguments of the drawString method).

Is there any way I can make the x and y the top left hand corner of the string? It is something like shifting the anchor point, so that the top left hand corner of the string will be at x and y.
0
Comment
Question by:dr0zaxx
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 92

Expert Comment

by:objects
ID: 12081197
Subtract the height of the string from y.
0
 
LVL 92

Expert Comment

by:objects
ID: 12081279
Use the FontMetrics class to get the height of the string
FontMetrics fm = g2d.getFontMetrics(font);
int height = fm.getHeight();
0
 

Expert Comment

by:Emilda
ID: 12081403
FontMetrics f = g.getFontMetrics();
int fascent = f.getAscent();
       
g.drawString("Hello", x, fascent);
0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 13

Accepted Solution

by:
Webstorm earned 2000 total points
ID: 12082537
Hi dr0zaxx,


FontMetrics f = g.getFontMetrics();
int yf = f.getMaxAscent();
       
g.drawString("Hello", x, yf + y);
0
 
LVL 92

Expert Comment

by:objects
ID: 12089316
to get the actual bounds of the string use:

 Rectangle2D bounds = fm.getStringBounds(s, g2d);

you can then use those bounds to offset the position to paint it.
0
 
LVL 1

Author Comment

by:dr0zaxx
ID: 12095485
Thanks for the responses.
0
 
LVL 13

Expert Comment

by:Webstorm
ID: 12100982
:-)
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month13 days, 8 hours left to enroll

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question