Skip to content

Commit

Permalink
Fix resources loading in jar
Browse files Browse the repository at this point in the history
That took a while
  • Loading branch information
Something-Inconspicuous committed Jan 9, 2024
1 parent 1a1f8de commit a2435aa
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 11 deletions.
31 changes: 31 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,37 @@
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>io.github.somethinginconspicuous.Main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>

</plugins>
</pluginManagement>

<resources>
<resource>
<directory>src/main/resources/</directory>
<includes>
<include>Press_Start_2P/PressStart2P-Regular.ttf</include>
<include>images/*.png</include>
</includes>
</resource>
</resources>
</build>


</project>
11 changes: 7 additions & 4 deletions src/main/java/io/github/somethinginconspicuous/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
//import java.awt.GridLayout;
import java.util.LinkedList;
import java.util.Queue;
Expand Down Expand Up @@ -46,11 +46,14 @@ public class Main extends JFrame {

static {
try {
FONT = Font.createFont(Font.TRUETYPE_FONT, new File("src/main/resources/Press_Start_2P/PressStart2P-Regular.ttf")).deriveFont(24.0f);
// URL url = Main.class.getResource("");
InputStream is = Main.class.getResourceAsStream("/Press_Start_2P/PressStart2P-Regular.ttf");
Font font = Font.createFont(Font.TRUETYPE_FONT, is);
//FONT = Font.createFont(Font.TRUETYPE_FONT, new File("src/main/resources/Press_Start_2P/PressStart2P-Regular.ttf")).deriveFont(24.0f);
FONT = font.deriveFont(24.0f);
GraphicsEnvironment.getLocalGraphicsEnvironment().registerFont(FONT);
} catch (FontFormatException | IOException e) {
FONT = Font.getFont("Serif");
e.printStackTrace();
throw new RuntimeException("Could not load font", e);
}

}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.github.somethinginconspicuous.game.items;

import java.io.InputStream;

import javax.swing.ImageIcon;

import io.github.somethinginconspicuous.game.Item;
Expand All @@ -17,7 +19,7 @@ public static Coat getInstance(){

@Override
public ImageIcon getImageIcon() {
return new ImageIcon("src/main/resources/images/coat_hack.png");
return new ImageIcon(getClass().getResource("/images/coat_hack.png"));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public static Debris getInstance(){

@Override
public ImageIcon getImageIcon() {
return new ImageIcon("src/main/resources/images/debris_hack.png");
return new ImageIcon(getClass().getResource("/images/debris_hack.png"));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public static Key getInstance() {

@Override
public ImageIcon getImageIcon() {
return new ImageIcon("src/main/resources/images/imageedit_25_6825500106_hack.png");
return new ImageIcon(getClass().getResource("/images/imageedit_25_6825500106_hack.png"));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public static Keycard getInstance() {

@Override
public ImageIcon getImageIcon() {
return new ImageIcon("src/main/resources/images/keycard_hack.png");
return new ImageIcon(getClass().getResource("/images/keycard_hack.png"));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public static Map getInstance() {

@Override
public ImageIcon getImageIcon() {
return new ImageIcon("src/main/resources/images/pxil-frame-0.png");
return new ImageIcon(getClass().getResource("/images/pxil-frame-0.png"));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public static Sandwich getInstance(){

@Override
public ImageIcon getImageIcon() {
return new ImageIcon("src/main/resources/images/sandwich_hack.png");
return new ImageIcon(getClass().getResource("/images/sandwich_hack.png"));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public static Watch getInstance() {

@Override
public ImageIcon getImageIcon() {
return new ImageIcon("src/main/resources/images/watch_hack.png");
return new ImageIcon(getClass().getResource("/images/watch_hack.png"));
}

@Override
Expand Down

0 comments on commit a2435aa

Please sign in to comment.