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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T22:11:03+00:00 2026-06-11T22:11:03+00:00

I would like to generate text image same as the JLabel label without displaying

  • 0

I would like to generate text image same as the JLabel label without displaying JLabel.

I tried same Font, same drawing method.
But generated image is not same as JLabel.

My sourcecode is below.
* ‘super.paintComponent(g)’ has been commented out for clarity that it is the same way. Output image is same.
* Below drawing by ‘View.paint’ method, but I’m tried ‘SwingUtilities2.drawString’ too. Two results are the same.

    /* Label */
    JLabel label = new JLabel(text) {
        @Override
        public void paintComponent(Graphics g) {
            //super.paintComponent(g);
            View v = BasicHTML.createHTMLView(this, getText());
            v.paint(g, new Rectangle(0, 0, getWidth(), getFontMetrics(
                            getFont()).getAscent()));
        }
    };
    label.setFont(new Font("Consolas", Font.PLAIN, 13));

    /* Image */
    FontMetrics fm = label.getFontMetrics(font);
    BufferedImage image = new BufferedImage(fm.stringWidth(text),
                fm.getHeight(), BufferedImage.TYPE_INT_RGB);
    Graphics2D g2d = image.createGraphics();
    g2d.setFont(label.getFont());

    // Clear background.
    g2d.setPaint(label.getBackground());
    g2d.fillRect(0, 0, image.getWidth(), image.getHeight());

    // Draw string.
    g2d.setClip(new Rectangle(0, 0, image.getWidth(), image.getHeight()));
    View v = BasicHTML.createHTMLView(label, text);
    v.paint(g2d, new Rectangle(0, 0, image.getWidth(), 
            g2d.getFontMetrics().getAscent()));

    // ... output image to file ...

Result image is following.
[JLabel]
enter image description here
[Generated image]
enter image description here

Generated image is slightly thin-faced, as compared to JLabel’s capture.
How can I generate text image same as the JLabel label?

Thank you for your consideration.

  • 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-11T22:11:04+00:00Added an answer on June 11, 2026 at 10:11 pm

    I’m not sure, but you might need to create a compatible buffered image (compatible to the display)

    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice gs = ge.getDefaultScreenDevice();
    GraphicsConfiguration gc = gs.getDefaultConfiguration();
    
    // Create an image that does not support transparency
    BufferedImage bimage = gc.createCompatibleImage(100, 100, Transparency.OPAQUE);
    

    This will at least get you on a close with the graphics been used to render to the screen

    You might also like to pay around with the rendering quality as well

    Kleopatra did a post on a similar question awhile ago, you might to try and hunt it down

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

Sidebar

Related Questions

I would like to generate an image from text that I get from a
i would like to generate a very simple report with some images and text
I would like to generate a short, unique ID without having to check for
I would like to generate the mysql-password hash from js. I know the method
I would like to generate text images like the following (source: http://www.zune.net/en-US ): The
For a given method (eg. Settings.cpp getSettingByName()), I would like to get a text
I would like to generate a text file containing all 19,683 Tic-Tac-Toe board layouts
I would like to generate a patch with all commits from a local branch.
I would like to generate POJO, XML for a given database. Database: ( contents
I would like to generate a graphical sitemap for my website. There are two

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.