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

  • Home
  • SEARCH
  • 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 7308919
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T23:38:21+00:00 2026-05-28T23:38:21+00:00

I am making a 2d engine using Java based on entities. The physics and

  • 0

I am making a 2d engine using Java based on entities. The physics and sprites are done, but I still need to be able to draw text with the BaseText class. For experimental purposes I am using the following code in the Renderer class (that handles drawing all the sprites and such):

BufferGraphics.drawString(((BaseText) Entity).getText(), (int) -(Origin.getX() * PositionTransform), (int) -Origin.getY());

I would like to, however, be able to either move this code into the setText(final String Text) method of the BaseText entity, i.e. when it is called a new image is created containing the text specified (possibly in different fonts and sizes and such, I haven’t decided).

My problem is this: I would like to be able to resize (scale) the text to my liking. It would also be nice to have the text converted to an image as I can get the dimensions of it and set the size of the text entity itself.

Basically, what I need follows something along these lines:

  1. Take desired string and feed it into the setText method.
  2. Take the string and draw it onto an image, sized so that the text will fit into it exactly.
  3. Set this new image to the Image field in the entity so that the engine can draw it.

Is this even possible? There may be a way to do this with the FontMetrics class or whatever it may be called, but I’m not so sure as I have not used it before.

Edit : Let me clarify: I want to create a BufferedImage based on the size of some text set to a specific font and size, not size the text to fit an image.

Edit 2: Thanks to this fellow Andrew, whom so graciously provided code, I was able to add some code to the engine that, by all means, just plain should work. Again, however, not even with that drawRect in there, the image either remains either transparent or somehow is not getting drawn. Let me supply some breadcrumbs: -snip-

The stupid thing is that all the other sprites and images and such draw fine, so I am not sure how it could be the Renderer.
By the way, that was the paint() method.

Edit 3:
…
Uh…
…
Oh my.
I am…
…
Text can not explain how hard I belted myself in the face with my left palm.

BaseText.java

@Override
public BufferedImage getImage() {return null;}

Renderer.java

BufferedImage Image = Entity.getImage();

I am
a huge idiot.
Thank you, Andrew, for that code. It worked fine.

Edit 4: By the way, here’s the final code that I used:

public void setText(final String Text)
{
    Graphics2D Draw = (Graphics2D) Game.View.getBuffer().getDrawGraphics();
    FontMetrics Metrics = Draw.getFontMetrics();
    Rectangle2D Bounds = Metrics.getStringBounds(Text, Draw);
    BufferedImage NewImage = new BufferedImage((int) Bounds.getWidth(), (int) (Bounds.getHeight() + Metrics.getDescent()), BufferedImage.TYPE_INT_RGB);
    Draw = (Graphics2D) NewImage.getGraphics();
    Draw.setColor(new Color(0xAAFF0000));
    Draw.drawRect(0, 0, NewImage.getWidth(), NewImage.getHeight());
    Draw.drawString(Text, 0, (int) Bounds.getHeight());
    this.Image = NewImage;
    this.Text = Text;
    this.setSize(new Vector(NewImage.getWidth(), NewImage.getHeight()));
}
  • 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-28T23:38:22+00:00Added an answer on May 28, 2026 at 11:38 pm
    1. Use FontMetrics, GlyphView or the preferred size a JLabel (handy for getting the size needed to display formatted text.
    2. Adjust the sizes of the font in step 1 until it fits. Call BufferedImage.createGraphics() to get a Graphics2D object. Paint the String to that.
    3. I do not understand point 3, so won’t comment.

    Here is how it would work with either FontMetrics or a JLabel.

    import java.awt.*;
    import java.awt.image.*;
    import java.awt.geom.Rectangle2D;
    import javax.swing.*;
    
    class TextSize {
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    // Technique 1 - FontMetrics
                    String s = "The quick brown fox jumps over the lazy dog!";
                    BufferedImage bi = new BufferedImage(
                        1,
                        1,
                        BufferedImage.TYPE_INT_RGB);
                    Graphics g = bi.getGraphics();
                    FontMetrics fm = g.getFontMetrics();
                    Rectangle2D b = fm.getStringBounds(s,g);
                    System.out.println(b);
                    bi = new BufferedImage(
                        (int)b.getWidth(),
                        (int)(b.getHeight() + fm.getDescent()),
                        BufferedImage.TYPE_INT_RGB);
                    g = bi.getGraphics();
                    g.drawString(s,0,(int)b.getHeight());
    
                    JOptionPane.showMessageDialog(
                        null,
                        new JLabel(new ImageIcon(bi)));
    
                    // Technique 3 - JLabel
                    JLabel l = new JLabel(s);
                    l.setSize(l.getPreferredSize());
                    bi = new BufferedImage(
                        l.getWidth(),
                        l.getHeight(),
                        BufferedImage.TYPE_INT_RGB);
                    g = bi.getGraphics();
                    g.setColor(Color.WHITE);
                    g.fillRect(0,0,400,100);
                    l.paint(g);
    
                    JOptionPane.showMessageDialog(
                        null,
                        new JLabel(new ImageIcon(bi)));
                }
            });
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm making a javascript game but (not browser-based) but the engine I'm using has
I've been making a game using the rokonandroid game engine, but I randomly get
I'm making a particle engine in Java, and right now I'm using a subclass
I'm currently making an iphone web app based on Google App Engine (python). I
I'm making a program using RBing. But I unable to get 50 links. How
I want to build a product-search engine. I was thinking of using google-site-search but
Context I'm making a game engine using SDL and OpenGL. I'm trying to find
okay i'm making a game using c++ (for the engine) and openGL, now i've
I am making a toy physics engine which works with floating point numbers which
I am developing a Java application using Google App Engine that depends on a

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.