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 7740287
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T08:44:20+00:00 2026-06-01T08:44:20+00:00

Using Graphics2D, I’d like to show a text inside my image, but I’d like

  • 0

Using Graphics2D, I’d like to show a text inside my image, but I’d like it to be shown in one line. That means if the text is longer than the image, the size will be reduced (adapted)

How could I do ?

So far, here’s what I did, but I’m sure it’s really bad.

int fontSize = 66;
FontRenderContext frc = new FontRenderContext(null, true, true);
element = null;

do { // we turn until the size goes into the image
    fontSize -= 2;
    layout = new TextLayout(myText, new Font(font, Font.BOLD, fontSize), frc);
    element = layout.getPixelBounds(null, 0, 0);

    if (fontSize <= 12) {
        throw new Exception ("Title too long.");
    }
} while(element.width > image.getWidth());

How can I do ?

Thanks for your help.

  • 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-01T08:44:21+00:00Added an answer on June 1, 2026 at 8:44 am

    I’ve had to do the same kind of thing when putting text inside a circle for a particular display. The only thing you can reasonably do better on is, instead of making your search a linear search, make it binary. This will give you better overall performance. Other than this, your approach is fine – I think this is about as good as you can do with fitting text to specific on-screen dimensions in Java.

    Couple of coding semantics issues too – this would probably be more readable if you did it in a regular while-loop.

    Another issue is the Exception you’re throwing. It’d probably be better to put this in a function that returned some value indicating whether the text could be drawn on a particular image or not. It’s never a good thing to use thrown Exceptions to determine program logic if you can get the same behavior while using a simple conditional statement.

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

Sidebar

Related Questions

I resized an image using Java2D Graphics class. But it doesn't look right. BufferedImage
I am generating some images using the Graphics2D interface, and occasionally I'd like to
I'm using Graphics2D.drawImage to draw an image into a BufferedImage. Then I Use ImageIO.write()
i would like to resize a BufferedImage that is draw using the Graphics drawing
I want to rotate text on a JPanel using Graphics2D.. My code is this:
I have a program that draws some vector graphics using System.Drawing and the Graphics
I am trying to produce some example graphics using ggplot2, and one of the
I'm trying to render a bit of text using Core Graphics APIs and I'm
I understand Objective-C memory management but I'm using Core Graphics functionality such as CGRect,
Can someone provide an example of drawing an iPhone-like turning wheel using Core Graphics

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.