How to add a line and text to a buffered image?

I have a buffered image that I am going to print.  

Given the buffered image object, what is the easiest way to:

add a line of text at the top
add a line going accross the width of the page
center the original buffered image underneath that?
LVL 5
jjacksnAsked:
Who is Participating?
 
Tommy BraasCommented:
Create a new image. Draw the texts as you wish. Finally draw the old image as desired. Print the new image!
0
 
jjacksnAuthor Commented:
Any sample code on drawing fonts?  (I don't know how).
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
jjacksnAuthor Commented:
yeah, actually, turns out I do have acces to a graphics object where I need it, making this much easier.  happen to know how to center text?
0
 
JavatmConnect With a Mentor Commented:
> happen to know how to center text?

Use : drawString(String s, float x, float y) - Float X and Y is the possition use to place where on the screen you would like it to be. :)
0
 
Tommy BraasCommented:
>> happen to know how to center text?
You would have to calculate the length of the text, using the font information, and the use the length to position the text appropriately.
0
 
maclemaConnect With a Mentor Commented:
Here is a working sample application...

Let me know if you have any questions..

Matt

______________________________________________

import java.io.*;
import javax.imageio.ImageIO;
import javax.swing.JTextField;
import java.awt.*;
import java.awt.image.*;

public class TextOnImage
{
      
      public BufferedImage loadImage(String path) throws IOException
      {
            Image img = ImageIO.read(new File(path));
            return (BufferedImage)img;
      }
      
      public void saveImage(String path, BufferedImage img) throws IOException
      {
            ImageIO.write(img, "PNG", new File(path));
      }
      
      public int getStringWidth(String str, Font f)
      {
            JTextField txt = new JTextField();
            txt.setFont(f);
            FontMetrics fm = txt.getFontMetrics(txt.getFont());
            return fm.stringWidth(str);
      }
      
      public void drawText(String text, BufferedImage img)
      {
            //Create a graphics object
            Graphics2D g2d = (Graphics2D)img.createGraphics();
            
            //create a font
            Font f = new Font("Arial", Font.BOLD, 14);
            
            //determine the total width of the string to be written
            double stringWidth = getStringWidth(text, f);
            
            //turn on anitaliasing (smooth edges)
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            
            //determine the x coordinate to center the string
            double imageWidth = img.getWidth();
            int x = (int)Math.floor((imageWidth-stringWidth)/(double)2);
            int y = 20;
            
            //create a color
            Color c = Color.BLACK;
            
            g2d.setColor(c);
            g2d.setFont(f);
            g2d.drawString(text, x, y);
      }
      
      public static void main(String[] args) throws IOException
      {
            TextOnImage toi = new TextOnImage();
            
            BufferedImage img = toi.loadImage("C:/sample.jpg");
            toi.drawText("Hello World!", img);
            toi.saveImage("C:/sample.png",img);
      }

}
0
 
CEHJConnect With a Mentor Commented:
This might help if you adapted it:

      class OverlayIcon extends ImageIcon {
            private String text;

            private boolean saved;

            /**
             * @param image
             */
            public OverlayIcon(Image image, String text) {
                  super(image);
                  this.text = text;
            }

            /*
             * (non-Javadoc)
             *
             * @see javax.swing.ImageIcon#paintIcon(java.awt.Component,
             *      java.awt.Graphics, int, int)
             */
            public synchronized void paintIcon(Component c, Graphics g, int x, int y) {
                  super.paintIcon(c, g, x, y);
                  g.drawString(text, 10, 10);
            }

            public void saveOverlay() {
                  Graphics g = null;
                  try {
                        BufferedImage bi = new BufferedImage(getIconWidth(), getIconHeight(),
                                    BufferedImage.TYPE_INT_RGB);
                        g = bi.getGraphics();
                        paintIcon(component, g, 0, 0);
                        ImageIO.write(bi, "bmp", new File("overlay.bmp"));
                  } catch (IOException e) {
                        e.printStackTrace();
                  } finally {
                        g.dispose();
                  }
            }

      }
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.