When generating an image, I just want to visualize it and not save it

-2

When using this code I save the image, but instead of saving I just want to visualize it.

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"));
    System.out.println("Image Created");
    
asked by anonymous 30.11.2016 / 20:43

2 answers

3

Use the awt class Desktop , eg:

Desktop.getDesktop().open(new File("C:\images\suaImage.jpg"));
    
30.11.2016 / 20:53
3

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);
    }

}
    
30.11.2016 / 21:06