Java Swing GUI

How to add text to an image in Java

In this tutorial, we are going to see how to add text to an image in Java. You just have to get the Graphics object of the image and draw your string on the image. The following example adds the text “Welcome To StackHowTo!” to the image “image.png”.


Java Program to add text to an image
import java.awt.*;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;

public class AddTextToImg
  public static void main(String[] args) throws Exception 
    //read the image
    BufferedImage image = File("image.png"));

    //get the Graphics object
    Graphics g = image.getGraphics();
    //set font
    //display the text at the coordinates(x=50, y=150)
    g.drawString("Welcome To WayToLearnX!", 50, 150);
    //write the image
    ImageIO.write(image, "png", new File("image1.png"));



mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *