1010import java .awt .event .MouseEvent ;
1111import java .awt .event .MouseListener ;
1212import java .awt .image .BufferStrategy ;
13-
1413import java .util .concurrent .TimeUnit ;
1514
1615import javax .imageio .ImageIO ;
1716
18- import java .io .File ;
17+ import java .io .FileNotFoundException ;
18+ import java .io .InputStream ;
1919
2020import javax .swing .JLabel ;
2121import javax .swing .JOptionPane ;
@@ -46,13 +46,15 @@ public class MazeGame extends Canvas implements Runnable, MouseListener {
4646 // Actual Player/Camera object
4747 public Player player ;
4848
49+ private MapGenerator mapGenerator ;
50+
4951 // Resources
50- public static final String MAPS_PATH = "res/maps.txt" ;
51- public static final String BACKGROUND_IMAGE_PATH = "res/bgimage.jpg" ;
52- public static final String TITLE_FONT_PATH = "res/valorant.ttf" ;
53- public static final String BUTTON_FONT_PATH = "res/nintendo.otf" ;
54- public static final String WALKING_SFX = "res/footsteps.wav" ;
55- public static final String GAME_SOUND = "res/MazeGameSong.wav" ;
52+ public static final String MAPS_PATH = "/ res/maps.txt" ;
53+ public static final String BACKGROUND_IMAGE_PATH = "/ res/bgimage.jpg" ;
54+ public static final String TITLE_FONT_PATH = "/ res/valorant.ttf" ;
55+ public static final String BUTTON_FONT_PATH = "/ res/nintendo.otf" ;
56+ public static final String WALKING_SFX = "/ res/footsteps.wav" ;
57+ public static final String GAME_SOUND = "/ res/MazeGameSong.wav" ;
5658 private Image backgroundImage ;
5759 private Font titleFont ;
5860 private Font buttonFont ;
@@ -86,37 +88,50 @@ public class MazeGame extends Canvas implements Runnable, MouseListener {
8688 // Constructor for this mazegame, which essentially initializes a Window while passing "this" game instance as the canvas
8789 public MazeGame () {
8890 // Initialize our campaign maps
89- this .map = MapGenerator .getMap (currentLevel );
90- levelCount = MapGenerator .maps .length ;
91+ mapGenerator = new MapGenerator ();
92+ this .map = mapGenerator .getMap (currentLevel );
93+ levelCount = mapGenerator .maps .length ;
9194
9295 sp = new SoundPlayer (GAME_SOUND );
9396 soundThread = new Thread (sp );
9497 soundThread .start ();
9598
9699 // Initialize Resources
97- try {
98- File bgImgFile = new File (BACKGROUND_IMAGE_PATH );
99- backgroundImage = ImageIO .read (bgImgFile );
100- } catch (Exception e ) {
100+ try (InputStream bgImgStream = getClass ().getResourceAsStream (BACKGROUND_IMAGE_PATH )) {
101+ if (bgImgStream != null ) {
102+ backgroundImage = ImageIO .read (bgImgStream );
103+ } else {
104+ throw new FileNotFoundException ("Background image not found: " + BACKGROUND_IMAGE_PATH );
105+ }
106+ } catch (Exception e ) {
101107 backgroundImage = null ;
102108 e .printStackTrace ();
103109 }
104110
105- try {
106- File fontFile = new File (TITLE_FONT_PATH );
107- titleFont = Font .createFont (Font .TRUETYPE_FONT , fontFile );
108- titleFont = titleFont .deriveFont (titleFont .getSize () * (WIDTH / 20 ) * 1.0f );
109- } catch (Exception e ) {
111+ try (InputStream fontStream = getClass ().getResourceAsStream (TITLE_FONT_PATH )) {
112+ if (fontStream != null ) {
113+ titleFont = Font .createFont (Font .TRUETYPE_FONT , fontStream );
114+ titleFont = titleFont .deriveFont (titleFont .getSize () * (WIDTH / 20 ) * 1.0f );
115+ } else {
116+ throw new FileNotFoundException ("Title font not found: " + TITLE_FONT_PATH );
117+ }
118+ } catch (Exception e ) {
110119 titleFont = null ;
120+ e .printStackTrace ();
111121 }
112122
113- try {
114- File fontFile = new File (BUTTON_FONT_PATH );
115- buttonFont = Font .createFont (Font .TRUETYPE_FONT , fontFile );
116- buttonFont = buttonFont .deriveFont (buttonFont .getSize () * (WIDTH / 20 ) * 1.0f );
117- } catch (Exception e ) {
123+ try (InputStream fontStream = getClass ().getResourceAsStream (BUTTON_FONT_PATH )) {
124+ if (fontStream != null ) {
125+ buttonFont = Font .createFont (Font .TRUETYPE_FONT , fontStream );
126+ buttonFont = buttonFont .deriveFont (buttonFont .getSize () * (WIDTH / 20 ) * 1.0f );
127+ } else {
128+ throw new FileNotFoundException ("Button font not found: " + BUTTON_FONT_PATH );
129+ }
130+ } catch (Exception e ) {
118131 buttonFont = null ;
132+ e .printStackTrace ();
119133 }
134+
120135
121136 // Create the window
122137 new Window (WIDTH , HEIGHT , TITLE , ICON_PATH , this );
@@ -206,7 +221,7 @@ private void tickGame() {
206221 player .setX (playerStartX );
207222 player .setY (playerStartY );
208223 player .setDirection (playerStartDirection );
209- map = MapGenerator .getMap (currentLevel );
224+ map = mapGenerator .getMap (currentLevel );
210225 }
211226 if (player .getQuitIsPressed () == true ) {
212227 player .setQuitIsPressed (false );
@@ -216,7 +231,7 @@ private void tickGame() {
216231 player .setX (playerStartX );
217232 player .setY (playerStartY );
218233 player .setDirection (playerStartDirection );
219- map = MapGenerator .getMap (currentLevel );
234+ map = mapGenerator .getMap (currentLevel );
220235 isGenMap = false ;
221236 }
222237 }
@@ -404,7 +419,7 @@ else if(e.getX() > quitButX1 && e.getX() < quitButX2 && e.getY() > quitButY1 &&
404419 else if (e .getX () > easyButX1 && e .getX () < easyButX2 && e .getY () > easyButY1 && e .getY () < easyButY2 ) isEasyMode = true ;
405420 else if (e .getX () > hardButX1 && e .getX () < hardButX2 && e .getY () > hardButY1 && e .getY () < hardButY2 ) isEasyMode = false ;
406421 else if (e .getX () > genButX1 && e .getX () < genButX2 && e .getY () > genButY1 && e .getY () < genButY2 ) {
407- map = MapGenerator .generateMaze (35 );
422+ map = mapGenerator .generateMaze (35 );
408423 isPlaying = true ;
409424 isGenMap = true ;
410425 }
0 commit comments