简体   繁体   中英

Java - Export to JAR file in eclipse with resources (images and audio files)

I use eclipse on linux Ubuntu and I have this code for loading image and setting it as background in one of my JPanels:

public class MenuState extends JPanel implements ActionListener {

private static final long serialVersionUID = 1L;
private GameStateManager gsm;
private int width;
private int height;

public MenuState(GameStateManager gsm)
{
    this.gsm = gsm;
    width = gsm.getWidth();
    height = gsm.getHeight();
    SizeManager sm = new SizeManager();
    this.setLayout(new BorderLayout());
    sm.set_size(this, width, height);
}

public void paintComponent(Graphics g)
{
    super.paintComponent(g);

    BufferedImage background_image;
    try {
        background_image = ImageIO.read(new File("src/res/img/menu_background.png"));
        g.drawImage(background_image, 0, 0, width, height, null);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

public void actionPerformed(ActionEvent e) {
}

and it works only in eclipse. When I export it to jar file or runnable jar file program does not show images. I also tried to use

this.getClass().getResource()

and similar codes but it does not work in eclipse. But I am maybe doing something wrong.

I also have this code in another class to play audio:

public class AudioManager {

private Clip clip;

public void play(String audio_name, boolean repeat)
{   
    try {
        File audioFile = new File(audio_name);
        AudioInputStream audioStream = AudioSystem.getAudioInputStream(audioFile);
        AudioFormat format = audioStream.getFormat();
        DataLine.Info info = new DataLine.Info(Clip.class, format);
        clip = (Clip) AudioSystem.getLine(info);
        clip.open(audioStream);
        if(repeat)
        {
            clip.loop(clip.LOOP_CONTINUOUSLY);
        }
        clip.start();

    } catch (Exception e) {
        e.printStackTrace();
    }
}

public void stop()
{
    clip.stop();
}

and it plays audio in eclipse but not after I export project.

I suppose File works only for files on disk and not for files in jar file but it is only way I made it works in eclipse.

So, what should I do?

And additional problem: Sound played in eclipse is lagging, I improved it increasing available memory for eclipse but little lags occur when loading program and changing JPanels in JFrame by clicking on JButtons.

Thanks for any advice.

What String do you put into the getResource method ? When you use getResource or getResourceAsStream , you must specify the package path. If your file is in the package "com.test.example", then you must put getResource("/com/test/example/my_file.png") .

So, with your background image, you must load your image with :

background_image = ImageIO.read(this.getClass().getResource("/res/img/menu_background.png"));

You should do the same way with your audio file.

I also recommend you to load your image outside of the paintComponent method. Otherwise, each time your panel is repainted, the image is reloaded.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM