by Dinesh Thakur

With this code we will study how text is drawn on a any image.

import java.applet.Applet;

import java.awt.*;
import java.awt.event.*;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.RenderingHints;
import java.awt.font.FontRenderContext;
import java.awt.font.TextLayout;
import java.awt.geom.*;
import java.awt.image.*;
import java.awt.Shape;
import javax.swing.*;
public class DrawTextonImageExample extends Applet {
public static void main(String[] args) {
Frame DrawTextonImage = new Frame("Draw TextonImage Example");
DrawTextonImage.setSize(350, 250);
Applet DrawTextonImageExample = new DrawTextonImageExample();
DrawTextonImage.add(DrawTextonImageExample);
DrawTextonImage.setVisible(true);
DrawTextonImage.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
      }
    });
  }
  public void paint(Graphics g) {
    g.setColor(Color.blue);
    g.setFont(new Font("Arial",Font.BOLD,14));
    g.drawString("Draw text on an Image Java Example", 50, 40);
    g.setFont(new Font("Arial",Font.BOLD,10));
    g.drawString("http://ecomputernotes.com", 200, 205);
                Graphics2D G2D = (Graphics2D)g;
                int w = getSize().width;
                int h = getSize().height;
                Image image = (new ImageIcon("DineshThakur.jpg")).getImage();
                G2D.drawImage(image,140,80,this);
                FontRenderContext FontRC = G2D.getFontRenderContext();
                Font font = new Font("Times",Font.BOLD,w/25);
                String str = new String ("Dinesh Thakur");
                TextLayout TextL = new TextLayout(str,font,FontRC);
                float sw = (float)TextL.getBounds().getWidth();
                AffineTransform AffineTran = new AffineTransform();
                AffineTran.setToTranslation(w/2-sw/2,h*3/8);
                G2D.setColor(Color.green);
               Shape shape = TextL.getOutline(AffineTran);
               G2D.draw(shape);
               TextL.draw(G2D,w/2-sw/2,h*5/8);
  }
}

How to draw text on an image