Create a JFrame
, within it add a JLabel
and set the content of the label as an image using a ImageIcon
encapsulating its BufferedImage
. Example:
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Teste {
public static void main(String[] args) throws IOException {
String key = "Lucão MC";
BufferedImage bufferedImage = ImageIO.read(new File("recibo.png"));
Graphics graphics = bufferedImage.getGraphics();
graphics.setColor(Color.BLACK);
graphics.setFont(new Font("Arial Regular", Font.PLAIN, 55));
graphics.drawString(key, 300, 300);
//ImageIO.write(bufferedImage, "png", new File("recibo1.png"));
Teste.showImage(bufferedImage);
System.out.println("Image Created");
}
// Esse método é o que exibe a imagem em uma janela
protected static void showImage(BufferedImage img) {
JFrame frame = new JFrame();
frame.getContentPane().setLayout(new FlowLayout());
frame.getContentPane().add(new JLabel(new ImageIcon(img)));
frame.pack();
frame.setVisible(true);
}
}