Problem loading image

1

I have an application, that when I ran it from the IDE, I load an image with a progress bar from the main screen. The problem is that when you exported the file to an executable, the image disappears but the progress bar keeps appearing. And I do not understand why with IDE if that works, but with the .Jar no.

This is the main class:

  import sun.applet.Main;


  import java.awt.Color;


  import javax.swing.ImageIcon;


  public class PantallaCargandoMain {


    cargando screen;

    public PantallaCargandoMain() {
        inicioPantalla();
        screen.velocidadDeCarga();
    }

    private void inicioPantalla() {

        ImageIcon myImage = new ImageIcon("src/dado/LogoInicio.png");
        screen = new cargando(myImage);
        screen.setLocationRelativeTo(null);
        screen.setProgresoMax(100);
        screen.setVisible(true);
        screen.setBackground(new Color(0, 0, 0, 0));

    }

    public static void main(String[] args) {
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                System.out.println(info.getName());
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }

        Inicio inicio = new Inicio();

        new PantallaCargandoMain();

        inicio.setVisible(true);
        inicio.setLocationRelativeTo(null);

    }
}

And this where I "charge" everything:

    import javax.swing.*;
    import java.awt.*;

       @SuppressWarnings("serial")

    public class cargando extends JWindow {

    BorderLayout borderLayout1 = new BorderLayout();

    JLabel imageLabel = new JLabel();

    JPanel southPanel = new JPanel();

    FlowLayout southPanelFlowLayout = new FlowLayout();

    JProgressBar progressBar = new JProgressBar();

    ImageIcon imageIcon;

    public cargando(ImageIcon imageIcon) {
        this.imageIcon = imageIcon;
        dibujaVentana();
    }

    public void dibujaVentana() {
        // setOpacity(0.1f);

        imageLabel.setIcon(imageIcon);
        this.getContentPane().setLayout(borderLayout1);
        southPanel.setLayout(southPanelFlowLayout);
        southPanel.setBackground(new Color(0, 0, 0, 0));
        this.getContentPane().add(imageLabel, BorderLayout.CENTER);
        this.getContentPane().add(southPanel, BorderLayout.SOUTH);
        southPanel.add(progressBar, null);
        this.pack();
    }

    public void setProgresoMax(int maxProgress) {
        progressBar.setMaximum(maxProgress);
    }

    public void setProgreso(int progress) {
        final int progreso = progress;
        progressBar.setValue(progreso);
    }

    public void setProgreso(String message, int progress) {
        final int progreso = progress;
        final String theMessage = message;
        setProgreso(progress);
        progressBar.setValue(progreso);
        setMessage(theMessage);
    }

    private void setMessage(String message) {
        if (message == null) {
            message = "";
            progressBar.setStringPainted(false);
        } else {
            progressBar.setStringPainted(true);
        }

        progressBar.setString(message);
    }

    public void velocidadDeCarga() {
        for (int i = 0; i <= 100; i++) {
            for (long j = 0; j < 1000000; ++j) {
                String poop = " " + (j + i);
            }
            setProgreso("" + i, i);
        }

        dispose();
    }

}
    
asked by Mario 18.04.2017 в 22:24
source

2 answers

2

To be able to read a resource from jar , you can not treat the resource as File , but you have to read it as InputStream .

That works in eclipse, but not in jar :

File image = new File(getClass().getResource("/image.png").toURI());

That's the way to get resources from a jar:

InputStream in = getClass().getResourceAsStream("/image.png");
    
answered by 19.04.2017 в 02:17
0

In this case, the image does not appear because in the .jar you must obtain the image in this way:

.getClass().getResource("/directorio imagen/imagen")

Therefore this would be an example of the change (using import javax.swing.ImageIcon;):

//  ImageIcon myImage = new ImageIcon("/directorio/LogoInicio.png");   
ImageIcon myImage = new ImageIcon(getClass().getResource("/directorio/LogoInicio.png"))
    
answered by 20.04.2017 в 20:20