title: Creating a sized image out of a string in Java
body: <p>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 <code>BaseText</code> class. For experimental purposes I am using the following code in the <code>Renderer</code> class (that handles drawing all the sprites and such):</p>
  
  <pre><code>BufferGraphics.drawString(((BaseText) Entity).getText(), (int) -(Origin.getX() * PositionTransform), (int) -Origin.getY());
  </code></pre>
  
  <p>I would like to, however, be able to either move this code into the <code>setText(final String Text)</code> method of the <code>BaseText</code> 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).</p>
  
  <p>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.</p>
  
  <p>Basically, what I need follows something along these lines:</p>
  
  <ol>
  <li>Take desired string and feed it into the <code>setText</code> method.</li>
  <li>Take the string and draw it onto an image, sized so that the text will fit into it exactly.</li>
  <li>Set this new image to the <code>Image</code> field in the entity so that the engine can draw it.</li>
  </ol>
  
  <p>Is this even possible? There may be a way to do this with the <code>FontMetrics</code> class or whatever it may be called, but I'm not so sure as I have not used it before.</p>
  
  <p>Edit : Let me clarify: I want to <em>create</em> a BufferedImage based on the size of some text set to a specific font and size, not size the text to fit an image.</p>
  
  <p>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 <em>should</em> 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-</p>
  
  <p>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.</p>
  
  <p>Edit 3:<br>
  ...<br>
  Uh...<br>
  ...<br>
  Oh my.<br>
  I am...<br>
  ...<br>
  Text can not explain how hard I belted myself in the face with my left palm.</p>
  
  <p>BaseText.java</p>
  
  <pre><code>@Override
  public BufferedImage getImage() {return null;}
  </code></pre>
  
  <p>Renderer.java</p>
  
  <pre><code>BufferedImage Image = Entity.getImage();
  </code></pre>
  
  <p>I am<br>
  a huge idiot.<br>
  Thank you, Andrew, for that code. It worked fine.</p>
  
  <p>Edit 4: By the way, here's the final code that I used:</p>
  
  <pre><code>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()));
  }
  </code></pre>