From 544ee006cf24587abdf288edf18c7833ca535171 Mon Sep 17 00:00:00 2001 From: lcaohoanq Date: Tue, 3 Sep 2024 11:19:59 +0700 Subject: [PATCH] refactor: remove redundant get resources --- .../fxsnakegame/styles/UIImages.java | 32 +++++++++++++++ .../fxsnakegame/views/base/Board.java | 40 ++++--------------- .../fxsnakegame/views/game/Apartment.java | 29 +++++--------- .../lcaohoanq/fxsnakegame/views/game/Box.java | 21 +++------- .../fxsnakegame/views/game/Mill.java | 21 +++------- .../fxsnakegame/views/game/NoMaze.java | 5 --- .../fxsnakegame/views/game/Rails.java | 29 +++++--------- .../fxsnakegame/views/game/Tunnel.java | 33 ++++++--------- 8 files changed, 84 insertions(+), 126 deletions(-) diff --git a/src/main/java/com/lcaohoanq/fxsnakegame/styles/UIImages.java b/src/main/java/com/lcaohoanq/fxsnakegame/styles/UIImages.java index 49035c2..a035911 100644 --- a/src/main/java/com/lcaohoanq/fxsnakegame/styles/UIImages.java +++ b/src/main/java/com/lcaohoanq/fxsnakegame/styles/UIImages.java @@ -4,9 +4,12 @@ import java.awt.Image; import java.awt.Toolkit; import java.net.URL; +import java.util.Objects; import javax.swing.ImageIcon; import com.lcaohoanq.fxsnakegame.views.base.MyFrame; +import lombok.Getter; +@Getter public class UIImages { public static final URL iconURL = MyFrame.class.getResource(ResourcePaths.URL_KEY_ICON); public static final Image icon = Toolkit.getDefaultToolkit().createImage(iconURL); @@ -14,4 +17,33 @@ public class UIImages { public static final URL snakeURL = MyFrame.class.getResource(ResourcePaths.URL_SNAKE_LOGO); public static final Image logo = Toolkit.getDefaultToolkit().getImage(snakeURL); public static final ImageIcon logoIcon = new ImageIcon(logo); + + private Image ball; // Snake body image + private Image apple; // Regular apple image + private Image head; // Snake head image + private Image bigApple; // Big apple + private Image wall; + + public void loadImages() { + + try{ + ball = new ImageIcon( + Objects.requireNonNull(getClass().getResource(ResourcePaths.URL_DOT))).getImage(); + + apple = new ImageIcon( + Objects.requireNonNull(getClass().getResource(ResourcePaths.URL_APPLE))).getImage(); + + head = new ImageIcon( + Objects.requireNonNull(getClass().getResource(ResourcePaths.URL_HEAD))).getImage(); + + bigApple = new ImageIcon( + Objects.requireNonNull(getClass().getResource(ResourcePaths.URL_BIG_APPLE))).getImage(); + + wall = new ImageIcon( + Objects.requireNonNull(getClass().getResource(ResourcePaths.URL_WALL))).getImage(); + }catch (Exception e){ + System.out.println("Resources at Board not found"); + } + + } } diff --git a/src/main/java/com/lcaohoanq/fxsnakegame/views/base/Board.java b/src/main/java/com/lcaohoanq/fxsnakegame/views/base/Board.java index f3ef8a4..72dad9d 100644 --- a/src/main/java/com/lcaohoanq/fxsnakegame/views/base/Board.java +++ b/src/main/java/com/lcaohoanq/fxsnakegame/views/base/Board.java @@ -6,6 +6,7 @@ import com.lcaohoanq.fxsnakegame.styles.UIBorders; import com.lcaohoanq.fxsnakegame.styles.UIColors; import com.lcaohoanq.fxsnakegame.styles.UIFonts; +import com.lcaohoanq.fxsnakegame.styles.UIImages; import com.lcaohoanq.fxsnakegame.styles.UILabels; import com.lcaohoanq.fxsnakegame.styles.UISizes; import com.lcaohoanq.fxsnakegame.utils.ApiUtils; @@ -16,7 +17,6 @@ import java.awt.Color; import java.awt.FlowLayout; import java.awt.Graphics; -import java.awt.Image; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; @@ -24,8 +24,6 @@ import java.awt.event.KeyEvent; import java.io.InputStream; import java.net.http.HttpResponse; -import java.util.Objects; -import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JLabel; import javax.swing.JOptionPane; @@ -65,10 +63,6 @@ public abstract class Board extends JPanel implements ActionListener { // Game state variables private int score = 0; // Player's score private Timer bigAppleTimer; // Timer for big apple appearance - private Image ball; // Snake body image - private Image apple; // Regular apple image - private Image head; // Snake head image - private Image bigApple; // Big apple image // UI components private JLabel gameOverLabel; // Label to display the "Game Over" message private JPanel gameOverPanel; // Panel for UI components at the game over @@ -83,6 +77,8 @@ public abstract class Board extends JPanel implements ActionListener { private final JPanel bottomPanel = new JPanel(); // Panel for UI components at the bottom private final JPanel gameOverButtonPanel = new JPanel(); // Panel for UI components at the game over + protected final UIImages uiImages = new UIImages(); + public Board() { initBoard(); } @@ -95,7 +91,7 @@ private void initBoard() { setPreferredSize(UISizes.SIZE_BOARD); setLayout(new BorderLayout()); - loadImages(); + uiImages.loadImages(); initGame(); initBottomPanel(); @@ -235,26 +231,6 @@ private void initGameOverPanel() { add(gameOverButtonPanel, BorderLayout.CENTER); } - protected void loadImages() { - - try{ - ball = new ImageIcon( - Objects.requireNonNull(getClass().getResource(ResourcePaths.URL_DOT))).getImage(); - - apple = new ImageIcon( - Objects.requireNonNull(getClass().getResource(ResourcePaths.URL_APPLE))).getImage(); - - head = new ImageIcon( - Objects.requireNonNull(getClass().getResource(ResourcePaths.URL_HEAD))).getImage(); - - bigApple = new ImageIcon( - Objects.requireNonNull(getClass().getResource(ResourcePaths.URL_BIG_APPLE))).getImage(); - }catch (Exception e){ - System.out.println("Resources at Board not found"); - } - - } - private void initGame() { dots = 3; @@ -280,16 +256,16 @@ public void paintComponent(Graphics g) { private void doDrawing(Graphics g) { if (inGame) { if (apple_count % 5 == 0 && apple_count != 0) { - g.drawImage(bigApple, bigApple_x, bigApple_y, this); + g.drawImage(uiImages.getBigApple(), bigApple_x, bigApple_y, this); } else { - g.drawImage(apple, apple_x, apple_y, this); + g.drawImage(uiImages.getApple(), apple_x, apple_y, this); } scoreLabel.setText("Score: " + score); for (int z = 0; z < dots; z++) { if (z == 0) { - g.drawImage(head, x[z], y[z], this); + g.drawImage(uiImages.getHead(), x[z], y[z], this); } else { - g.drawImage(ball, x[z], y[z], this); + g.drawImage(uiImages.getBall(), x[z], y[z], this); } } diff --git a/src/main/java/com/lcaohoanq/fxsnakegame/views/game/Apartment.java b/src/main/java/com/lcaohoanq/fxsnakegame/views/game/Apartment.java index 5def96b..eb914e9 100644 --- a/src/main/java/com/lcaohoanq/fxsnakegame/views/game/Apartment.java +++ b/src/main/java/com/lcaohoanq/fxsnakegame/views/game/Apartment.java @@ -1,61 +1,52 @@ package com.lcaohoanq.fxsnakegame.views.game; import com.lcaohoanq.fxsnakegame.constants.ResourcePaths; -import java.awt.Graphics; -import java.awt.Image; -import java.io.InputStream; -import javax.swing.ImageIcon; import com.lcaohoanq.fxsnakegame.styles.UISizes; import com.lcaohoanq.fxsnakegame.views.base.Board; +import java.awt.Graphics; +import java.io.InputStream; public class Apartment extends Board { protected int wallThickness = 20; - private Image wall; public Apartment(){ super(); } - @Override - public void loadImages() { - super.loadImages(); - wall = new ImageIcon(getClass().getResource(ResourcePaths.URL_WALL)).getImage(); - } - @Override public void paintComponent(Graphics g) { super.paintComponent(g); //draw the horizontal walls at y = 0, from x = 0 to x 80 for (int i = 0; i < 80; i += 20) { - g.drawImage(wall, i, 0, this); + g.drawImage(uiImages.getWall(), i, 0, this); } //draw the horizontal walls at y = 120, from x = 160 to x 340 for (int i = 160; i < UISizes.WIDTH_BOARD - 80; i += 20) { - g.drawImage(wall, i, 0, this); + g.drawImage(uiImages.getWall(), i, 0, this); } //draw the vertical walls at x = 0, from y = 0 to y 120 for (int i = 0; i < 100; i += 20) { - g.drawImage(wall, 0, i, this); + g.drawImage(uiImages.getWall(), 0, i, this); } //draw the horizontal walls at y = 140, from x = 0 to x 200 for (int i = 0; i < 200; i += 20) { - g.drawImage(wall, i, 140, this); + g.drawImage(uiImages.getWall(), i, 140, this); } //draw the horizontal walls at y = 140, from x = 300 to max width for (int i = 300; i < UISizes.WIDTH_BOARD; i += 20) { - g.drawImage(wall, i, 140, this); + g.drawImage(uiImages.getWall(), i, 140, this); } //draw the vertical walls at x = 200, from y = 0 to y 160 for (int i = 0; i < 160; i += 20) { - g.drawImage(wall, 200, i, this); + g.drawImage(uiImages.getWall(), 200, i, this); } //draw the horizontal walls at y = 400, from x = 0 to max width for (int i = 0; i < UISizes.WIDTH_BOARD; i += 20) { - g.drawImage(wall, i, 260, this); + g.drawImage(uiImages.getWall(), i, 260, this); } //draw the vertical walls at x = 300, from y = 260 to y 500 for (int i = 260; i < UISizes.HEIGHT_BOARD - 50; i += 20) { - g.drawImage(wall, 300, i, this); + g.drawImage(uiImages.getWall(), 300, i, this); } } diff --git a/src/main/java/com/lcaohoanq/fxsnakegame/views/game/Box.java b/src/main/java/com/lcaohoanq/fxsnakegame/views/game/Box.java index b49465d..7a183f1 100644 --- a/src/main/java/com/lcaohoanq/fxsnakegame/views/game/Box.java +++ b/src/main/java/com/lcaohoanq/fxsnakegame/views/game/Box.java @@ -1,37 +1,28 @@ package com.lcaohoanq.fxsnakegame.views.game; import com.lcaohoanq.fxsnakegame.constants.ResourcePaths; -import java.awt.Graphics; -import java.awt.Image; -import java.io.InputStream; -import javax.swing.ImageIcon; import com.lcaohoanq.fxsnakegame.styles.UISizes; import com.lcaohoanq.fxsnakegame.views.base.Board; +import java.awt.Graphics; +import java.io.InputStream; public class Box extends Board { protected int wallThickness = 20; - private Image wall; public Box() { super(); } - @Override - protected void loadImages() { - super.loadImages(); - wall = new ImageIcon(getClass().getResource(ResourcePaths.URL_WALL)).getImage(); - } - @Override public void paintComponent(Graphics g) { super.paintComponent(g); for (int i = 0; i < UISizes.WIDTH_BOARD; i += 20) { - g.drawImage(wall, i, 0, this); - g.drawImage(wall, i, UISizes.HEIGHT_BOARD - 70, this); + g.drawImage(uiImages.getWall(), i, 0, this); + g.drawImage(uiImages.getWall(), i, UISizes.HEIGHT_BOARD - 70, this); } for (int i = 0; i < UISizes.HEIGHT_BOARD - 70; i += 20) { - g.drawImage(wall, 0, i, this); - g.drawImage(wall, UISizes.WIDTH_BOARD - 20, i, this); + g.drawImage(uiImages.getWall(), 0, i, this); + g.drawImage(uiImages.getWall(), UISizes.WIDTH_BOARD - 20, i, this); } } diff --git a/src/main/java/com/lcaohoanq/fxsnakegame/views/game/Mill.java b/src/main/java/com/lcaohoanq/fxsnakegame/views/game/Mill.java index cb5bfd9..1a2c3e7 100644 --- a/src/main/java/com/lcaohoanq/fxsnakegame/views/game/Mill.java +++ b/src/main/java/com/lcaohoanq/fxsnakegame/views/game/Mill.java @@ -1,16 +1,13 @@ package com.lcaohoanq.fxsnakegame.views.game; import com.lcaohoanq.fxsnakegame.constants.ResourcePaths; -import java.awt.Graphics; -import java.awt.Image; -import java.io.InputStream; -import javax.swing.ImageIcon; import com.lcaohoanq.fxsnakegame.styles.UISizes; import com.lcaohoanq.fxsnakegame.views.base.Board; +import java.awt.Graphics; +import java.io.InputStream; public class Mill extends Board { protected int wallThickness = 20; - private Image wall; public Mill() { super(); @@ -22,30 +19,24 @@ public Mill() { downDirection = true; } - @Override - public void loadImages() { - super.loadImages(); - wall = new ImageIcon(getClass().getResource(ResourcePaths.URL_WALL)).getImage(); - } - @Override public void paintComponent(Graphics g) { super.paintComponent(g); //draw the left horizontal wall from x = 0 to x = 140 at y = 420 for (int i = 0; i < 160; i += 20) { - g.drawImage(wall, i, UISizes.HEIGHT_BOARD - 50 - 80, this); + g.drawImage(uiImages.getWall(), i, UISizes.HEIGHT_BOARD - 50 - 80, this); } //draw the right horizontal wall from x = 340 to x = 500 at y = 60 for (int i = 340; i < UISizes.WIDTH_BOARD; i += 20) { - g.drawImage(wall, i, 60, this); + g.drawImage(uiImages.getWall(), i, 60, this); } //draw the vertical walls at x = 140, from y = 0 to y = 340 for (int i = 0; i < 340; i += 20) { - g.drawImage(wall, 140, i, this); + g.drawImage(uiImages.getWall(), 140, i, this); } //draw the vertical walls at x = 340, from y = 500 to y = 160 for (int i = 160; i < 500; i += 20) { - g.drawImage(wall, 340, i, this); + g.drawImage(uiImages.getWall(), 340, i, this); } } diff --git a/src/main/java/com/lcaohoanq/fxsnakegame/views/game/NoMaze.java b/src/main/java/com/lcaohoanq/fxsnakegame/views/game/NoMaze.java index ae1f9aa..ec3db77 100644 --- a/src/main/java/com/lcaohoanq/fxsnakegame/views/game/NoMaze.java +++ b/src/main/java/com/lcaohoanq/fxsnakegame/views/game/NoMaze.java @@ -11,11 +11,6 @@ public NoMaze() { super(); } - @Override - protected void loadImages() { - super.loadImages(); - } - @Override protected void checkCollision() { diff --git a/src/main/java/com/lcaohoanq/fxsnakegame/views/game/Rails.java b/src/main/java/com/lcaohoanq/fxsnakegame/views/game/Rails.java index 9c7be4a..8db95cb 100644 --- a/src/main/java/com/lcaohoanq/fxsnakegame/views/game/Rails.java +++ b/src/main/java/com/lcaohoanq/fxsnakegame/views/game/Rails.java @@ -1,50 +1,41 @@ package com.lcaohoanq.fxsnakegame.views.game; import com.lcaohoanq.fxsnakegame.constants.ResourcePaths; -import java.awt.Graphics; -import java.awt.Image; -import java.io.InputStream; -import javax.swing.ImageIcon; import com.lcaohoanq.fxsnakegame.styles.UISizes; import com.lcaohoanq.fxsnakegame.views.base.Board; +import java.awt.Graphics; +import java.io.InputStream; public class Rails extends Board { private final int wallThickness = 20; - private Image wall; public Rails() { super(); } - @Override - protected void loadImages() { - super.loadImages(); - wall = new ImageIcon(getClass().getResource(ResourcePaths.URL_WALL)).getImage(); - } - @Override public void paintComponent(Graphics g) { super.paintComponent(g); //draw the top and bottom horizontal walls for (int i = 0; i < UISizes.WIDTH_BOARD; i += 20) { - g.drawImage(wall, i, 0, this); - g.drawImage(wall, i, UISizes.HEIGHT_BOARD - 70, this); + g.drawImage(uiImages.getWall(), i, 0, this); + g.drawImage(uiImages.getWall(), i, UISizes.HEIGHT_BOARD - 70, this); } //draw the 2 left vertical walls from y = 0 to y = 200 and from y = 300 to y = 480 for (int i = 0; i < 200; i += 20) { - g.drawImage(wall, 0, i, this); - g.drawImage(wall, 0, UISizes.HEIGHT_BOARD - 70 - i, this); + g.drawImage(uiImages.getWall(), 0, i, this); + g.drawImage(uiImages.getWall(), 0, UISizes.HEIGHT_BOARD - 70 - i, this); } //draw the 2 right vertical walls from for (int i = 0; i < 200; i += 20) { - g.drawImage(wall, UISizes.WIDTH_BOARD - 20, i, this); - g.drawImage(wall, UISizes.WIDTH_BOARD - 20, UISizes.HEIGHT_BOARD - 70 - i, this); + g.drawImage(uiImages.getWall(), UISizes.WIDTH_BOARD - 20, i, this); + g.drawImage(uiImages.getWall(), UISizes.WIDTH_BOARD - 20, UISizes.HEIGHT_BOARD - 70 - i, this); } //draw the 2 middle horizontal walls for (int i = 100; i < 400; i += 20) { - g.drawImage(wall, i, 140, this); - g.drawImage(wall, i, UISizes.HEIGHT_BOARD - 50 - 160, this); + g.drawImage(uiImages.getWall(), i, 140, this); + g.drawImage(uiImages.getWall(), i, UISizes.HEIGHT_BOARD - 50 - 160, this); } } diff --git a/src/main/java/com/lcaohoanq/fxsnakegame/views/game/Tunnel.java b/src/main/java/com/lcaohoanq/fxsnakegame/views/game/Tunnel.java index 06c908c..63445d8 100644 --- a/src/main/java/com/lcaohoanq/fxsnakegame/views/game/Tunnel.java +++ b/src/main/java/com/lcaohoanq/fxsnakegame/views/game/Tunnel.java @@ -1,17 +1,14 @@ package com.lcaohoanq.fxsnakegame.views.game; import com.lcaohoanq.fxsnakegame.constants.ResourcePaths; +import com.lcaohoanq.fxsnakegame.styles.UISizes; +import com.lcaohoanq.fxsnakegame.views.base.Board; import java.awt.Graphics; -import java.awt.Image; import java.io.InputStream; -import javax.swing.ImageIcon; import javax.swing.JFrame; -import com.lcaohoanq.fxsnakegame.styles.UISizes; -import com.lcaohoanq.fxsnakegame.views.base.Board; public class Tunnel extends Board { protected int wallThickness = 20; - private Image wall; public Tunnel() { super(); @@ -26,39 +23,33 @@ public static void main(String[] args) { frame.setVisible(true); } - @Override - public void loadImages() { - super.loadImages(); - wall = new ImageIcon(getClass().getResource(ResourcePaths.URL_WALL)).getImage(); - } - @Override public void paintComponent(Graphics g) { super.paintComponent(g); //draw the 2 left horizontal walls for (int i = 0; i < 100; i += 20) { - g.drawImage(wall, i, 0, this); - g.drawImage(wall, i, UISizes.HEIGHT_BOARD - 70, this); + g.drawImage(uiImages.getWall(), i, 0, this); + g.drawImage(uiImages.getWall(), i, UISizes.HEIGHT_BOARD - 70, this); } //draw the 2 right horizontal walls for (int i = 400; i < UISizes.WIDTH_BOARD; i += 20) { - g.drawImage(wall, i, 0, this); - g.drawImage(wall, i, UISizes.HEIGHT_BOARD - 70, this); + g.drawImage(uiImages.getWall(), i, 0, this); + g.drawImage(uiImages.getWall(), i, UISizes.HEIGHT_BOARD - 70, this); } //draw the 2 left vertical walls for (int i = 0; i < 100; i += 20) { - g.drawImage(wall, 0, i, this); - g.drawImage(wall, 0, UISizes.HEIGHT_BOARD - 70 - i, this); + g.drawImage(uiImages.getWall(), 0, i, this); + g.drawImage(uiImages.getWall(), 0, UISizes.HEIGHT_BOARD - 70 - i, this); } //draw the 2 right vertical walls for (int i = 0; i < 100; i += 20) { - g.drawImage(wall, UISizes.WIDTH_BOARD - 20, i, this); - g.drawImage(wall, UISizes.WIDTH_BOARD - 20, UISizes.HEIGHT_BOARD - 70 - i, this); + g.drawImage(uiImages.getWall(), UISizes.WIDTH_BOARD - 20, i, this); + g.drawImage(uiImages.getWall(), UISizes.WIDTH_BOARD - 20, UISizes.HEIGHT_BOARD - 70 - i, this); } //draw the 2 middle horizontal walls for (int i = 100; i < 400; i += 20) { - g.drawImage(wall, i, 205, this); - g.drawImage(wall, i, UISizes.HEIGHT_BOARD - 50 - 100 - 105, this); + g.drawImage(uiImages.getWall(), i, 205, this); + g.drawImage(uiImages.getWall(), i, UISizes.HEIGHT_BOARD - 50 - 100 - 105, this); } }