0

i made a game with awt and made a jar out of it . but when i run it by right clicking it it doesnt load the assets ( assets are on the jar file) But then i run it with terminal it loads the assets !

i'm using MacOS Monterey and Java 17

i used this code to get a image :

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;


public class Image {
    public BufferedImage image;
    public Image(String Path){
        BufferedImage img = null;
        try {
            img = ImageIO.read(new File(Path));
        } catch (IOException e) {
        }

        this.image = img;
    }
    public static void drawImage(Graphics2D g,Image image,int x,int y,int w,int h){
        g.drawImage(image.image,x,y,w,h,null);
    }
}

and i called them like this:

Image image = new Image("Data/image.PNG");

// And in my draw Method:
public void draw(Graphics2D g){
        Image.drawImage(g,image,0,0,32,32);

}

the game with double clicking the jar on mac game with double clicking(img)

and the game with opening the game with : (java -jar game.jar) game with running on terminal(img)

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Sepandi
  • 9
  • 1
  • 4
  • 1
    Better to load your files/images as resources: https://technojeeves.com/index.php/aliasjava1/78-loading-files-as-resources-in-java-with-netbeans https://technojeeves.com/index.php/aliasjava1/80-loading-files-as-resources-in-java-with-eclipse – g00se May 23 '22 at 14:43
  • 1
    See: [Loading Images Using getResource](https://docs.oracle.com/javase/tutorial/uiswing/components/icon.html#getresource). – camickr May 23 '22 at 14:47
  • im using a game loop and i think this method not going to work with a gameloop – Sepandi May 23 '22 at 14:56
  • Loading images has nothing to do with a game loop. The issue is finding the image to load from your jar file. Also, images should be preloaded once to make the game fast and efficient. – camickr May 23 '22 at 15:02
  • Jar entries are not separate files. You will **never** be able to read a .jar entry using File. Game loops have nothing to do with it. This is literally what the Class.getResource method is for. – VGR May 23 '22 at 15:48

0 Answers0