From 5a5f9642811f989521b865c86c4dd572b9ffd7eb Mon Sep 17 00:00:00 2001 From: AmirMohammad Hosseini Nasab Date: Sat, 8 Jun 2024 14:52:40 +0330 Subject: [PATCH] refactor(build): Fix Resources issue in .jar --- src/main/java/com/amirhn/GUI/ChessController.java | 5 ++++- .../com/amirhn/GUI/Components/PieceImageIcon.java | 5 ++--- src/main/java/com/amirhn/GUI/Constants.java | 4 ++-- {png => src/main/resources/png}/bB.png | Bin {png => src/main/resources/png}/bK.png | Bin {png => src/main/resources/png}/bN.png | Bin {png => src/main/resources/png}/bP.png | Bin {png => src/main/resources/png}/bQ.png | Bin {png => src/main/resources/png}/bR.png | Bin {png => src/main/resources/png}/wB.png | Bin {png => src/main/resources/png}/wK.png | Bin {png => src/main/resources/png}/wN.png | Bin {png => src/main/resources/png}/wP.png | Bin {png => src/main/resources/png}/wQ.png | Bin {png => src/main/resources/png}/wR.png | Bin {sound => src/main/resources/sound}/capture.wav | Bin {sound => src/main/resources/sound}/move.wav | Bin 17 files changed, 8 insertions(+), 6 deletions(-) rename {png => src/main/resources/png}/bB.png (100%) rename {png => src/main/resources/png}/bK.png (100%) rename {png => src/main/resources/png}/bN.png (100%) rename {png => src/main/resources/png}/bP.png (100%) rename {png => src/main/resources/png}/bQ.png (100%) rename {png => src/main/resources/png}/bR.png (100%) rename {png => src/main/resources/png}/wB.png (100%) rename {png => src/main/resources/png}/wK.png (100%) rename {png => src/main/resources/png}/wN.png (100%) rename {png => src/main/resources/png}/wP.png (100%) rename {png => src/main/resources/png}/wQ.png (100%) rename {png => src/main/resources/png}/wR.png (100%) rename {sound => src/main/resources/sound}/capture.wav (100%) rename {sound => src/main/resources/sound}/move.wav (100%) diff --git a/src/main/java/com/amirhn/GUI/ChessController.java b/src/main/java/com/amirhn/GUI/ChessController.java index 78c3938..24df5fc 100644 --- a/src/main/java/com/amirhn/GUI/ChessController.java +++ b/src/main/java/com/amirhn/GUI/ChessController.java @@ -12,6 +12,8 @@ import com.amirhn.Pieces.Piece; import java.io.File; import java.io.IOException; +import java.net.URL; +import java.util.Objects; import javax.sound.sampled.*; import javax.swing.*; @@ -76,9 +78,10 @@ private void playMoveSound(Move move) { } else { soundPath = Constants.MoveSoundPath; } + URL soundURL = getClass().getResource(soundPath); AudioInputStream audioInputStream = null; try { - audioInputStream = AudioSystem.getAudioInputStream(new File(soundPath).getAbsoluteFile()); + audioInputStream = AudioSystem.getAudioInputStream(Objects.requireNonNull(soundURL)); } catch (UnsupportedAudioFileException | IOException e) { e.printStackTrace(); } diff --git a/src/main/java/com/amirhn/GUI/Components/PieceImageIcon.java b/src/main/java/com/amirhn/GUI/Components/PieceImageIcon.java index cbb8b2e..259daaa 100644 --- a/src/main/java/com/amirhn/GUI/Components/PieceImageIcon.java +++ b/src/main/java/com/amirhn/GUI/Components/PieceImageIcon.java @@ -2,11 +2,10 @@ import com.amirhn.GUI.Constants; import javax.swing.*; +import java.util.Objects; /** The type Piece image icon. */ public class PieceImageIcon extends ImageIcon { - private static final String DefaultPath = Constants.PieceImageDirectory; - /** * Instantiates a new Piece image icon. * @@ -14,6 +13,6 @@ public class PieceImageIcon extends ImageIcon { * @param typeLetter the type letter */ public PieceImageIcon(String color, char typeLetter) { - super(DefaultPath + "/" + color.toLowerCase().charAt(0) + typeLetter + ".png"); + super(Objects.requireNonNull(PieceImageIcon.class.getResource(Constants.PieceImageDirectory + "/" + color.toLowerCase().charAt(0) + typeLetter + ".png"))); } } diff --git a/src/main/java/com/amirhn/GUI/Constants.java b/src/main/java/com/amirhn/GUI/Constants.java index d430514..c6b9920 100644 --- a/src/main/java/com/amirhn/GUI/Constants.java +++ b/src/main/java/com/amirhn/GUI/Constants.java @@ -29,10 +29,10 @@ public class Constants { public static final String GameTitle = "Chess"; /** The constant PieceImageDirectory. */ - public static final String PieceImageDirectory = "png"; + public static final String PieceImageDirectory = "/png"; /** The constant SoundDirectory. */ - public static final String SoundDirectory = "sound"; + public static final String SoundDirectory = "/sound"; /** The constant MoveSoundPath. */ public static final String MoveSoundPath = Constants.SoundDirectory + "/" + "move.wav"; diff --git a/png/bB.png b/src/main/resources/png/bB.png similarity index 100% rename from png/bB.png rename to src/main/resources/png/bB.png diff --git a/png/bK.png b/src/main/resources/png/bK.png similarity index 100% rename from png/bK.png rename to src/main/resources/png/bK.png diff --git a/png/bN.png b/src/main/resources/png/bN.png similarity index 100% rename from png/bN.png rename to src/main/resources/png/bN.png diff --git a/png/bP.png b/src/main/resources/png/bP.png similarity index 100% rename from png/bP.png rename to src/main/resources/png/bP.png diff --git a/png/bQ.png b/src/main/resources/png/bQ.png similarity index 100% rename from png/bQ.png rename to src/main/resources/png/bQ.png diff --git a/png/bR.png b/src/main/resources/png/bR.png similarity index 100% rename from png/bR.png rename to src/main/resources/png/bR.png diff --git a/png/wB.png b/src/main/resources/png/wB.png similarity index 100% rename from png/wB.png rename to src/main/resources/png/wB.png diff --git a/png/wK.png b/src/main/resources/png/wK.png similarity index 100% rename from png/wK.png rename to src/main/resources/png/wK.png diff --git a/png/wN.png b/src/main/resources/png/wN.png similarity index 100% rename from png/wN.png rename to src/main/resources/png/wN.png diff --git a/png/wP.png b/src/main/resources/png/wP.png similarity index 100% rename from png/wP.png rename to src/main/resources/png/wP.png diff --git a/png/wQ.png b/src/main/resources/png/wQ.png similarity index 100% rename from png/wQ.png rename to src/main/resources/png/wQ.png diff --git a/png/wR.png b/src/main/resources/png/wR.png similarity index 100% rename from png/wR.png rename to src/main/resources/png/wR.png diff --git a/sound/capture.wav b/src/main/resources/sound/capture.wav similarity index 100% rename from sound/capture.wav rename to src/main/resources/sound/capture.wav diff --git a/sound/move.wav b/src/main/resources/sound/move.wav similarity index 100% rename from sound/move.wav rename to src/main/resources/sound/move.wav