Create a JFrame
, inside it add a JLabel
and set the label content as an image using a ImageIcon
encapsulating your 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);
}
}
Where do you want to view?
– Thiago Luiz Domacoski
@Thiagoluizdomacoski By default in windows.
– Lucas Caresia
You want to open a window with the image (in Swing), that’s it, right?
– Luiz Vieira
Yes, preferably with the default windows viewer.
– Lucas Caresia
To use the default Windows viewer, use the answer of colleague Dilnei (just think that you will have to save the file before). If you want to use a generic form (which works in any OS), use my answer. P.S.: Next time you ask, follow these two tips: 1) be specific, because this way you avoid that people waste time and speed up your help; 2) prepare a [mcve], because not everyone has good will to take your code and try to arrange to compile.
– Luiz Vieira
All right, thanks for the tips.
– Lucas Caresia