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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T08:30:16+00:00 2026-05-14T08:30:16+00:00

I have checked similarly named questions, but they don’t answer this use case. Basically,

  • 0

I have checked similarly named questions, but they don’t answer this use case.

Basically, I was to overlay some text (text) at a given coordinate (x,y) I have the below function in a package;

protected BufferedImage Process2(BufferedImage image){
    Graphics2D gO = image.createGraphics();
    gO.setColor(Color.red);
    gO.setFont(new Font( "SansSerif", Font.BOLD, 12 ));
    gO.drawString(this.text, this.x, this.y);
    System.err.println(this.text+this.x+this.y);
    return image;
}

I feel like im missing something patently obvious; every reference to Graphics2D I can find is dealing with either games or writing directly to a file but I just want a BufferedImage returned. with the overlay ‘rendered’

In the current code, the image appears out the end unchanged.

Thanks!

  • 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-14T08:30:17+00:00Added an answer on May 14, 2026 at 8:30 am

    The method drawString() uses x and y for the leftmost character’s baseline. Numbers typically have no descenders; if the same is true of text, a string drawn at position (0,0) will be rendered entirely outside the image. See this example.

    Addendum: You may be having trouble with an incompatible color model in your image. One simple expedient is to render the image and then modify it in situ.

    Hello

    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.EventQueue;
    import java.awt.Font;
    import java.awt.FontMetrics;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.image.BufferedImage;
    import java.io.IOException;
    import java.net.URL;
    import javax.imageio.ImageIO;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    /**
     * @see https://stackoverflow.com/questions/2658663
     */
    public class TextOverlay extends JPanel {
    
        private BufferedImage image;
    
        public TextOverlay() {
            try {
                image = ImageIO.read(new URL(
                    "http://cdn.sstatic.net/stackexchange/img/logos/so/so-logo.png"));
            } catch (IOException e) {
                e.printStackTrace();
            }
            image = process(image);
        }
    
        @Override
        public Dimension getPreferredSize() {
            return new Dimension(image.getWidth(), image.getHeight());
        }
    
        private BufferedImage process(BufferedImage old) {
            int w = old.getWidth() / 3;
            int h = old.getHeight() / 3;
            BufferedImage img = new BufferedImage(
                w, h, BufferedImage.TYPE_INT_ARGB);
            Graphics2D g2d = img.createGraphics();
            g2d.drawImage(old, 0, 0, w, h, this);
            g2d.setPaint(Color.red);
            g2d.setFont(new Font("Serif", Font.BOLD, 20));
            String s = "Hello, world!";
            FontMetrics fm = g2d.getFontMetrics();
            int x = img.getWidth() - fm.stringWidth(s) - 5;
            int y = fm.getHeight();
            g2d.drawString(s, x, y);
            g2d.dispose();
            return img;
        }
    
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.drawImage(image, 0, 0, null);
        }
    
        private static void create() {
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(new TextOverlay());
            f.pack();
            f.setVisible(true);
        }
    
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    create();
                }
            });
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have checked the whole site and googled on the net but was unable
I know there have been similar questions posted but I think the issue I'm
I've already checked this.. similar question but I am not convinced about the answers...
My question is similar to this one but for a Rails app. I have
I have checked with the wikipedia article , and it seems like it is
I have checked in a huge Eclipse project from my desktop computer to the
I have checked the following during turning on Windows features: IIS,IIS Compatibility and under
I have checked IE8, FF 3.5 , Opera 9 none of them have support
I have checked out now a local working copy of a codebase that lives
I have checked the source codes of ASIHTTPRequest and could not make sure which

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.