by Dinesh Thakur Category: Java 2D Example

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.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
  public void paint(Graphics g) {
    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("", 200, 205);
                Graphics2D G2D = (Graphics2D)g;
                int w = getSize().width;
                int h = getSize().height;
                Image image = (new ImageIcon("DineshThakur.jpg")).getImage();
                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();
               Shape shape = TextL.getOutline(AffineTran);

How to draw text on an image

About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.C.A, MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.