Open image with my Application

0

I'm developing a java application to open image files with the File () class, like how to make my application the default for opening the images on my computer, I do not know how to get by parameter the file path to my application.

Could you help me, Grateful.

    
asked by anonymous 12.07.2018 / 14:02

1 answer

1

First, let's start with a viewer's code:

import java.awt.image.BufferedImage;
import java.awt.EventQueue;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

public class Visualizador {

    public static void main(String[] args) {
        if (args.length == 0) {
            JOptionPane.showMessageDialog(null, "Escolha um arquivo para abrir.", "Visualizador", JOptionPane.INFORMATION_MESSAGE);
            return;
        }
        if (args.length != 1) {
            JOptionPane.showMessageDialog(null, "Não foi possível entender a linha de comando.", "Visualizador", JOptionPane.ERROR);
            return;
        }
        BufferedImage img;
        try {
            img = lerImagem(args[0]);
            if (img == null) throw new IOException();
        } catch (FileNotFoundException e) {
            JOptionPane.showMessageDialog(null, "Não foi possível encontrar o arquivo " + args[0] + ".", "Visualizador", JOptionPane.ERROR);
            return;
        } catch (IOException e) {
            JOptionPane.showMessageDialog(null, "Não foi possível ler o arquivo " + args[0] + ".", "Visualizador", JOptionPane.ERROR);
            return;
        }
        EventQueue.invokeLater(() -> {
            JLabel jl = new JLabel(new ImageIcon(img));
            JFrame jf = new JFrame(args[0]);
            jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            jf.add(jl);
            jf.pack();
            jf.setLocationRelativeTo(null);
            jf.setVisible(true);
        });
    }

    private static BufferedImage lerImagem(String s) throws FileNotFoundException, IOException {
        return lerImagem(new File(s));
    }

    private static BufferedImage lerImagem(File f) throws FileNotFoundException, IOException {
        if (!f.exists()) throw new FileNotFoundException();
        return ImageIO.read(f);
    }
}

Note that the file name is read from the command line.

Compile this:

javac -encoding UTF8 Visualizador.java

Run like this:

java Visualizador teste.png

Where teste.png is your image. If the image exists and is well formed, it will load. If it is not, a cute and friendly error message appears.

The programs that windows associates with file types are .exe , .com , .bat , .pif and .cmd . Since this does not include .jar or .class , create this file visualizador.bat :

java Visualizador %1

So you can run it like this:

visualizador teste.png

Now, let's associate the file. First, access the file type you want and go to the properties menu:

Thenclickthebuttontochangethedefaultprogram:

Andlookforyourapplication(intheabovecasevisualizador.bat)inthelistorclickthebuttontofinditelsewhereonyourPC:

When you double-click the image, your program opens.

    
12.07.2018 / 17:57