java Grapics2D drawString Font Resizing

Posted on 2011-05-04
Last Modified: 2012-05-11

I'm writing text onto an existing image and I'm using the below to do so.  I've have a requirement to let the text be up to 40 characters on my 200 width image (see dimensions below) and I need to somehow shrink the font based on the text length.  Does FontMetrics support that?  

public class ImageCreator{

public void createImage(String text){
     Graphics2D g = img.getGraphics(); //img is a BufferedImage read in from file system
     [b]Font fnt=new Font("Dante",1,20);
     Color fntC = new Color(4, 4, 109);       
     Dimension d = new Dimension(200, 113);
     drawCenteredString(text, d.width, d.height, g);
public static void drawCenteredString(String s, int w, int h, Graphics g) {
      FontMetrics fm = g.getFontMetrics();
      int x = (w - fm.stringWidth(s)) / 2;
      int y = (fm.getAscent() + (h - (fm.getAscent() + fm.getDescent())) / 2);
      g.drawString(s, x, y);


Open in new window

Question by:cgray1223
    LVL 92

    Expert Comment

    not directly, FontMetric lets you calculate the size of string and you can use that to determine the font size you need to make it fit
    I posted an example in one of your previous questions
    LVL 92

    Expert Comment


    Author Comment

    Ok, I think that satisfies one of my use cases where the user enters one long word without any spaces.  I am able to write to the next line of the image but then that opens up another can of worms.  Do you know if anything will support the case when the text is long but has spaces?  For example,
    text="This is a test of Font Metrics classsssssssssss"

    I'm guessing I'll have to have custom login to break to new lines within the images writable dimension.  
    LVL 92

    Expert Comment

    I'd generally use a JTextArea in those case at let it do the wrapping, then use it to 'paint' to the image
    LVL 47

    Expert Comment

    I think we already went through the same issue in your orevious question - when
    you were fitting the text bewteen some pictures.
    It is the samwe thing here - you just try with different
    fonts and fontmetrics gives you the size - you rememeber
    you decided to break your messaeg  into two lines.

    It is all here:
    LVL 92

    Accepted Solution

    LVL 86

    Expert Comment

    >> and I need to somehow shrink the font based on the text length.  Does FontMetrics support that?  

    I already posted an example of exactly that at http:Q_26989670.html#35508813

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Suggested Solutions

    Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
    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…
    Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
    This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

    760 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

    12 Experts available now in Live!

    Get 1:1 Help Now