Skip to content

Commit

Permalink
refactor: remove redundant get resources
Browse files Browse the repository at this point in the history
  • Loading branch information
lcaohoanq committed Sep 3, 2024
1 parent a7cf5b1 commit 544ee00
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 126 deletions.
32 changes: 32 additions & 0 deletions src/main/java/com/lcaohoanq/fxsnakegame/styles/UIImages.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,46 @@
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);

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");
}

}
}
40 changes: 8 additions & 32 deletions src/main/java/com/lcaohoanq/fxsnakegame/views/base/Board.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -16,16 +17,13 @@
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;
import java.awt.event.KeyAdapter;
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;
Expand Down Expand Up @@ -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
Expand All @@ -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();
}
Expand All @@ -95,7 +91,7 @@ private void initBoard() {
setPreferredSize(UISizes.SIZE_BOARD);

setLayout(new BorderLayout());
loadImages();
uiImages.loadImages();
initGame();

initBottomPanel();
Expand Down Expand Up @@ -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;
Expand All @@ -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);
}
}

Expand Down
29 changes: 10 additions & 19 deletions src/main/java/com/lcaohoanq/fxsnakegame/views/game/Apartment.java
Original file line number Diff line number Diff line change
@@ -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);
}
}

Expand Down
21 changes: 6 additions & 15 deletions src/main/java/com/lcaohoanq/fxsnakegame/views/game/Box.java
Original file line number Diff line number Diff line change
@@ -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);
}
}

Expand Down
21 changes: 6 additions & 15 deletions src/main/java/com/lcaohoanq/fxsnakegame/views/game/Mill.java
Original file line number Diff line number Diff line change
@@ -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();
Expand All @@ -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);
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@ public NoMaze() {
super();
}

@Override
protected void loadImages() {
super.loadImages();
}

@Override
protected void checkCollision() {

Expand Down
29 changes: 10 additions & 19 deletions src/main/java/com/lcaohoanq/fxsnakegame/views/game/Rails.java
Original file line number Diff line number Diff line change
@@ -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);
}
}

Expand Down
Loading

0 comments on commit 544ee00

Please sign in to comment.