Java Draw String drawStringOnImage(Image image, String string, int x, int y)

Here you can find the source of drawStringOnImage(Image image, String string, int x, int y)

Description

draw String On Image

License

Open Source License

Declaration

public static Image drawStringOnImage(Image image, String string, int x, int y) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.awt.*;
import java.awt.image.*;

public class Main {
    public static Image drawStringOnImage(Image image, String string, int x, int y) {
        BufferedImage image1 = toBufferedImage(image);
        Graphics2D g = image1.createGraphics();
        g.drawString(string, x, y);// w w  w.  j a  v a  2s.c om
        return image1;
    }

    public static BufferedImage toBufferedImage(Image img) {
        if (img instanceof BufferedImage) {
            return (BufferedImage) img;
        }

        BufferedImage bimage = new BufferedImage(img.getWidth(null), img.getHeight(null),
                BufferedImage.TYPE_INT_ARGB);

        Graphics2D bGr = bimage.createGraphics();
        bGr.drawImage(img, 0, 0, null);
        bGr.dispose();

        return bimage;
    }
}

Related

  1. drawStringAt(Graphics g, String t, int x, int y, int where)
  2. drawStringAtPoint(Graphics g, String s, Point p)
  3. drawStringEx(Graphics g1, String s, Font font, Rectangle rect, int align, int valign)
  4. drawStringInBox(Graphics g, String string, int x, int y)
  5. drawStringLeft(Graphics2D g, String text, Rectangle rect, int fontHeight, Color c)
  6. drawStringOnScreen(String s, Color color, long milseconds)
  7. drawStringPair(Graphics2D g, String str1, String str2, int left, int right, int y, int size, Color color, boolean bold)
  8. drawStringRight(final Graphics g, final FontMetrics m, final String str, final int x, final int y)
  9. drawStringRightAlignedVTop(Graphics graphics, String string, int x, int yTop)