Solved

Draw string with Graphics2D

Posted on 2004-09-16
7
186 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
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 13

Accepted Solution

by:
Webstorm earned 500 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

757 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now