import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.font.FontRenderContext;
import java.awt.font.GlyphVector;
import java.awt.geom.Rectangle2D;
import javax.swing.*;
import javax.imageio.ImageIO;
import java.net.URL;
import java.io.File;
class PictureText {
public static void main(String[] args) throws Exception {
URL url = new URL("http://i.stack.imgur.com/Nqf3H.jpg");
final BufferedImage originalImage = ImageIO.read(url);
int width = originalImage.getWidth();
int height = originalImage.getHeight();
final BufferedImage textImage = new BufferedImage(
width,
height,
BufferedImage.TYPE_INT_ARGB);
Graphics2D g = textImage.createGraphics();
FontRenderContext frc = g.getFontRenderContext();
Font font = new Font("Arial", Font.BOLD, 250);
GlyphVector gv = font.createGlyphVector(frc, "Cat");
Rectangle2D box = gv.getVisualBounds();
int xOff = 25+(int)-box.getX();
int yOff = 80+(int)-box.getY();
Shape shape = gv.getOutline(xOff,yOff);
g.setColor(Color.WHITE);
g.setClip(shape);
g.drawImage(originalImage,0,0,null);
g.setClip(null);
g.setStroke(new BasicStroke(2f));
g.setColor(Color.BLACK);
g.setRenderingHint(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g.draw(shape);
g.dispose();
ImageIO.write(textImage,"png",new File("cat-text.png"));
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JLabel originalLabel = new JLabel(
new ImageIcon(originalImage));
JLabel textLabel = new JLabel(
new ImageIcon(textImage));
JPanel gui = new JPanel(new GridLayout(0,1,5,5));
gui.add(originalLabel);
gui.add(textLabel);
JOptionPane.showMessageDialog(null,gui);
}
});
}
}