Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8647857
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:10:33+00:00 2026-06-12T13:10:33+00:00

I have a region in my JPanel bounded by the point (0,0) and (width,height).

  • 0

I have a region in my JPanel bounded by the point (0,0) and (width,height). It is a square.

I have a word

String s;

I’d like to find the maximum font size that I can use for s. Now, I know there is a way to do it using FontMetrics and making a for loop to keep increasing the size of the font until it doesn’t fit inside the region. But this is SO inefficient and there must be a way to compute the font size of a given font type, such as “Courier” that will fit in this region.

Example of BAD way:

Font f = new Font("Courier", Font.PLAIN, 1);
FontMetrics fm = this.getFontMetrics(f); //this is a JPanel
do {
    f = new Font("Courier", Font.PLAIN, f.getSize()+1);
    fm = this.getFontMetrics(f);
while(fm.stringWidth(s) < width && fm.getHeight() < height);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T13:10:35+00:00Added an answer on June 12, 2026 at 1:10 pm

    I had the same problem and found a solution that is a little bit optimized, compared to just iterating over all font sizes. I try to converge towards the optimal font size by adjusting diffs that I either add or subtract until I find a diff font size below 1.

    Graphics2D graphics = image.createGraphics();
    graphics.setColor(Color.black);
    
    if (subtitleFont == null) {
        //create rectangle first (from a separate library
        int[] rect = matrix.getEnclosingRectangle();
        // define the maximum rect for the text
        Rectangle2D maxRect = new Rectangle2D.Float(0, 0, w - 7, h - rect[0] - rect[3] - 10);
    
        subtitleX = 0;
        subtitleY = 0;
        // starting with a very big font due to a high res image
        float size = 80f * 4f;
        // starting with a diff half the size of the font
        float diff = size / 2;
        subtitleFont = graphics.getFont().deriveFont(Font.BOLD).deriveFont(size);
        FontMetrics fontMetrics = graphics.getFontMetrics(subtitleFont);
        Rectangle2D stringBounds = null;
    
        while (Math.abs(diff) > 1) {
            subtitleFont = subtitleFont.deriveFont(size);
            graphics.setFont(subtitleFont);
            fontMetrics = graphics.getFontMetrics(subtitleFont);
            stringBounds = fontMetrics.getStringBounds(options.subtitle, graphics);
            stringBounds = new Rectangle2D.Float(0f, 0f, (float) (stringBounds.getX() + stringBounds.getWidth()), (float) ( stringBounds.getHeight()));
    
            if (maxRect.contains(stringBounds)) {
                if (0 < diff) {
                    diff = Math.abs(diff);
                } else if (diff < 0) {
                    diff = Math.abs(diff) / 2;
                }
            } else {
                if (0 < diff) {
                    diff = - Math.abs(diff) / 2;
                } else if (diff < 0) {
                    if (size <= Math.abs(diff)) {
                        diff = - Math.abs(diff) / 2;
                    } else {
                        diff = - Math.abs(diff);
                    }
                }
            }
            size += diff;
        }
    
        subtitleX = (int) ((w/2) - (stringBounds.getWidth() / 2));
        subtitleY = (int) (h - maxRect.getHeight() + fontMetrics.getAscent());
    }
    
    graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
            RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    graphics.drawString(options.subtitle, subtitleX, subtitleY);
    

    I have tried that with different resolutions of the image and the sizes of the font. It takes 10 to 12 iterations until a font is found that will fit the max rectangle. I hope it will be helpful to somebody.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string that looks a little like this, world:region:bash It divides folder
Suppose I have the following: A region defined by minimum and maximum latitude and
In silverlight prism application I have a region (ContentControl) and would like to remove
Anyone know a good way to find out where i have an extra #region
Let say that I have an enclosed region (like a div region), and within
I have a coordinate region that I have determined contains the limits of what
In my Shell window I have a workspace region to allow view switching via
I am using NHibernate and have a cache region specified in my NHibernate configuration:
I'm using Oracle APEX and I have a report region in a page that
I have the following query: DECLARE @Region VARCHAR(MAX) SET @Region = '' SELECT @Region

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.