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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:29:23+00:00 2026-05-28T13:29:23+00:00

My Aim is to append text on the images and save the image. I

  • 0

My Aim is to append text on the images and save the image. I am able to append text on image.
My question is How to Specify the Area For the text, and how to make it Multi line, so that if data does not cross to the border.
Code:-

public Test()
 {
s = "Welcome TO Image Rendering";
s1="Title data";
s2="DescriptionA fast paced action thriller about a stunt driver.He finds himself a target for some dangerous men after he agrees to help his beautiful neighbour, Irene. Subtitles: Chinesedata";
         Font f = new Font("Serif",Font.BOLD,22);
         text = new JLabel("Welcome TO Image Rendering");
         text.setFont(f);

         AttributedString astr = new AttributedString(s);
         astr.addAttribute(TextAttribute.FOREGROUND, Color.red, 0, 1);

         MediaTracker mt = new MediaTracker(this);
         image = Toolkit.getDefaultToolkit().createImage("c://Ci_Audio.jpg");
         mt.addImage(image,0);
         try{mt.waitForID(0);}catch(InterruptedException ie){}
         int width = image.getWidth(null);
         int height = image.getHeight(null);
         BufferedImage bimg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
         bimg.createGraphics().drawImage(image, 0, 0, this);
         bimg.getGraphics().setFont(f);
         bimg.getGraphics().drawString(s,10,50);
         bimg.getGraphics().drawString(s1,38,40);
         bimg.getGraphics().drawString(s2,188,84);
         img = new ImageIcon(bimg);
         label = new JLabel(img);
         p = new JPanel();
         p.add(label);
         getContentPane().add(p);
     }
     public static void main(String args[])
         {
         Test tt = new Test();
         tt.setDefaultCloseOperation(EXIT_ON_CLOSE);
         tt.setSize(360,305);
         tt.setVisible(true);
     }
}

Here String s2 Won’t fit to the image screen. Thanks in Advance.

  • 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-05-28T13:29:24+00:00Added an answer on May 28, 2026 at 1:29 pm

    you can call the getFontMetrics() on your current Graphics after you’ve set the font you’re planning to use. FontMetrics.stringWidth( your_string ) will then give you the width of the string in your current font as it would appear in your BufferedImage. You can then build up the string until it exceeds the width of your image and then start another string for the next line. You’ll have to figure out the vertical offsets for the string positions once you’ve split up the string and know how many lines/strings you need to include. Use fontMetrics again this time to get the string height for calculation.

    Here’s a similar question that gives a simple version of splitting:

    Full-justification with a Java Graphics.drawString replacement?

    Here’s a utility class that does a lot of this work for you given the FontMetrics and string
    http://www.java2s.com/Code/Java/2D-Graphics-GUI/WrapstringaccordingtoFontMetrics.htm

    Someone else can comment I’m sure if more recent Java versions have additional functionality related to this.

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

Sidebar

Related Questions

I aim to make an app that would store sms messages with certain words
Aim :- to set background image in my application that fills the device both
AIM: I would like to tail the last line of the file.txt and input
AIM: To output the contents of a log file line by line newest at
My aim is to animate an image to new dimensions on mouse-over as well
My aim is to determine the color of object. And make a classification, for
My aim is to retrieve some data from a global array which is defined
My aim is to retrieve some data from a global array which is defined
my aim is to pull data from mysql and print it in html table.
My aim is have the text inside this div displaying in the middle of

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.