0

I'm trying to use images located in the resources folder in a gradle project I just created.

The project tree is as follows:

D:.
├───main
│   ├───java
│   │   └───chess_game_gui
│   │       ├───GameGUI
│   │       ├───MainComponents
│   │       └───Pieces
│   └───resources
└───test
    ├───java
    │   └───chess_game_gui
    │       └───app
    └───resources

Inside a file that is inside the GameGUI folder, i have the following line of code:

private static final PieceLabel whitePawn = new PieceLabel(new ImageIcon("../../../resources/white_pawn.png"), Piece.WHITE);

Which is, to my understanding, supposed to get 3 folder up, end up at the main folder, and then access the resources folder. But it fails (the images do not appear in my app. I know it's a problem with the path since I just created this gradle project, it used to work without the gradle project structure.)

nortain32
  • 69
  • 1
  • 7
  • Does this answer your question? [Java Swing ImageIcon, where to put images?](https://stackoverflow.com/questions/17912041/java-swing-imageicon-where-to-put-images). You are using resources incorrectly. The resources directory doesn't even exist at runtime. – aled Jun 21 '23 at 15:09
  • 1
    In a Gradle project, the resources folder is typically considered as the root of the classpath, and the relative paths within the application code are resolved based on that root. Just put the images inside the `src/main/resources` folder, then `private static final PieceLabel whitePawn = new PieceLabel(new ImageIcon("/white_pawn.png"), Piece.WHITE); ` – Byte Ninja Jun 21 '23 at 15:25
  • *new ImageIcon("/white_pawn.png")* is incorrect. That's *file-wise* access, which is not what you want. You want `new ImageIcon(PieceLabel.class.getResource("/white_pawn.png"));` (resource access [that's a `URL`]) – g00se Jun 21 '23 at 15:41

0 Answers0