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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T12:11:36+00:00 2026-06-01T12:11:36+00:00

I want to draw string using Graphics with Rectangle border outside the string. This

  • 0

I want to draw string using Graphics with Rectangle border outside the string.

This is what I already do:

     public void paint(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;
        FontMetrics fontMetrics = g2d.getFontMetrics();
        String str = "aString Test";

        int width = fontMetrics.stringWidth(str);
        int height = fontMetrics.getHeight();

        int x = 100;
        int y = 100;

        // Draw String
        g2d.drawString(str, x, y);
        // Draw Rectangle Border based on the string length & width
        g2d.drawRect(x - 2, y - height + 2, width + 4, height);
    }

My problem is, I want to draw string with new line (“\n”) with Rectangle border outside:

This is the code for the new line:

     public void paint(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;
        FontMetrics fontMetrics = g2d.getFontMetrics();
        String str = "aString\nTest";

        int width = fontMetrics.stringWidth(str);
        int height = fontMetrics.getHeight();

        int x = 100;
        int y = 100;

        // Drawing string per line
        for (String line : str.split("\n")) {
            g2d.drawString(line, x, y += g.getFontMetrics().getHeight());
        }
    }

Can anyone help me for this problem? I appreciate your help & suggestion…

Final Code

        int numberOfLines = 0;

        for (String line : str.split("\n")) {
            if(numberOfLines == 0)
                g2d.drawString(line, x, y);
            else
                g2d.drawString(line, x, y += g.getFontMetrics().getHeight());
            numberOfLines++;
        }

        g2d.drawRect(x - 2, y - height * numberOfLines + 2, width + 4, height * numberOfLines);
  • 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-01T12:11:38+00:00Added an answer on June 1, 2026 at 12:11 pm

    If I understand correctly, your issues is with the rectangle’s height.

    Try keeping a record of how many lines you have eg:

    int numberOfLines=0;
    
    for (String line : str.split("\n")) {
        g2d.drawString(line, x , y + (numberOfLines * height));
        numberOfLines++;
    }
    
    g2d.drawRect(x - 2, y - height + 2, width + 4, height * numberOfLines);
    

    This also changes how it works out the y value for drawing the string.

    Would something like that work?

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

Sidebar

Related Questions

I am using Graphics.DrawString to draw my usercontrol's text like this: protected override void
I'm using the the drawstring method of Graphics class to draw a String on
I want to print some data in the form using the code: e.Graphics.DrawString(string.Format(السيد {0},
I want to draw a string. at first, I get the string bounds using
I am using drawInRect from UIKit to draw a string. I want to also
I want to rotate text on a JPanel using Graphics2D.. My code is this:
i can draw a string/image... with drawInRect/drawAtPoint. but how about if i want to
I want to draw line on UIImageView. I already draw line on UIImageView but
I want to draw this in my view to draw this line, I have
I want to draw a text with 32 bit transparency on a graphics object.

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.