Skip to content

Commit

Permalink
Added resources manager to cache images
Browse files Browse the repository at this point in the history
  • Loading branch information
petruki committed Jan 29, 2021
1 parent 8725167 commit d43ff07
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 15 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>com.rogerio</groupId>
<artifactId>battleship-java</artifactId>
<version>1.0.3</version>
<version>1.0.4</version>
<name>Battleship Java</name>

<properties>
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/rogerio/GameInitializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.apache.logging.log4j.Logger;

import com.rogerio.ui.MainUI;
import com.rogerio.util.ResourcesCache;

/**
* @author petruki (Roger Floriano)
Expand All @@ -23,6 +24,7 @@ public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ResourcesCache.getInstance().initializeImages();
MainUI frame = new MainUI(SHIPS, SHIP_SIZE);
frame.setVisible(true);
} catch (Exception e) {
Expand Down
9 changes: 6 additions & 3 deletions src/main/java/com/rogerio/model/Target.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
import javax.swing.Icon;
import javax.swing.ImageIcon;

import com.rogerio.util.LoadImage;
import com.rogerio.util.ResourceConstants;
import com.rogerio.util.ResourcesCache;

/**
* @author petruki (Roger Floriano)
Expand Down Expand Up @@ -47,9 +48,11 @@ public Target(int shipId, int rowCoord, int colCoord, SlotType slotType, Icon ic
*/
public static Target createTarget(int slot, int rowCoord, int colCoord) {
if (slot == 0) {
return new Target(slot, rowCoord, colCoord, SlotType.MISSED, new ImageIcon(LoadImage.load("miss.png")));
return new Target(slot, rowCoord, colCoord, SlotType.MISSED,
new ImageIcon(ResourcesCache.getInstance().getImages(ResourceConstants.IMG_MISS)));
} else {
return new Target(slot, rowCoord, colCoord, SlotType.HIT, new ImageIcon(LoadImage.load("ship.png")));
return new Target(slot, rowCoord, colCoord, SlotType.HIT,
new ImageIcon(ResourcesCache.getInstance().getImages(ResourceConstants.IMG_HIT)));
}
}

Expand Down
22 changes: 14 additions & 8 deletions src/main/java/com/rogerio/ui/MainUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import com.rogerio.controller.GameController;
import com.rogerio.model.Scoreboard;
import com.rogerio.util.LoadImage;
import com.rogerio.util.ResourceConstants;
import com.rogerio.util.ResourcesCache;

/**
* This is the App Context which handles all other UI objects.
Expand Down Expand Up @@ -42,7 +44,7 @@ public MainUI(int ships, int shipSize) {
}

private void buildPanel() {
setTitle("Battleship Java");
setTitle("Battleship Java - v1.0.4");
setIconImage(LoadImage.load("ship.png"));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 1024, 880);
Expand All @@ -56,7 +58,8 @@ private void buildPanel() {
scoreUI = new ScoreUI();
endGameScoreUI = new EndGameScoreUI();

contentPane = new BackgroundPanel(LoadImage.load("board.jpg"));
contentPane = new BackgroundPanel(
ResourcesCache.getInstance().getImages(ResourceConstants.IMG_BOARD));
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
Expand Down Expand Up @@ -84,7 +87,7 @@ public void onStartNewGame(ActionEvent event) {
controlUI.setVisible(true);
scoreUI.setVisible(true);

contentPane.setBackground(LoadImage.load("board.jpg"));
contentPane.setBackground(ResourcesCache.getInstance().getImages(ResourceConstants.IMG_BOARD));

Object[][] matrix = gameController.generateMatrix(7, 7, ships, shipSize);

Expand All @@ -94,11 +97,14 @@ public void onStartNewGame(ActionEvent event) {
}

public void onGameFinished(Scoreboard scoreBoard) {
if ((scoreBoard.getHit() - scoreBoard.getMiss()) > 0)
contentPane.setBackground(LoadImage.load("board_end.png"));
else
contentPane.setBackground(LoadImage.load("board_gameover.png"));

if ((scoreBoard.getHit() - scoreBoard.getMiss()) > 0) {
contentPane.setBackground(
ResourcesCache.getInstance().getImages(ResourceConstants.IMG_BOARD_END_GOOD));
} else {
contentPane.setBackground(
ResourcesCache.getInstance().getImages(ResourceConstants.IMG_BOARD_END_BAD));
}

endGameScoreUI.showScore(scoreBoard);
boardUI.setVisible(false);
controlUI.setVisible(false);
Expand Down
9 changes: 6 additions & 3 deletions src/main/java/com/rogerio/util/LoadImage.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.rogerio.ui.MainUI;

public class LoadImage {

private static final Logger logger = LogManager.getLogger(LoadImage.class);
Expand All @@ -21,7 +19,12 @@ public class LoadImage {
*/
public static BufferedImage load(String image) {
try {
return ImageIO.read(MainUI.class.getClassLoader().getResource(image));
//print generated board
if (logger.isDebugEnabled()) {
logger.info(image);
}

return ImageIO.read(LoadImage.class.getClassLoader().getResource(image));
} catch (Exception e) {
logger.error(e);
JOptionPane.showMessageDialog(null, String.format(IMAGE_ERROR, image), "Error", JOptionPane.ERROR_MESSAGE);
Expand Down
22 changes: 22 additions & 0 deletions src/main/java/com/rogerio/util/ResourceConstants.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.rogerio.util;

public enum ResourceConstants {

IMG_BOARD("board"),
IMG_BOARD_END_GOOD("board_good"),
IMG_BOARD_END_BAD("board_bad"),
IMG_MISS("miss"),
IMG_HIT("hit");

private String key;

private ResourceConstants(String key) {
this.key = key;
}

@Override
public String toString() {
return key;
}

}
38 changes: 38 additions & 0 deletions src/main/java/com/rogerio/util/ResourcesCache.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.rogerio.util;

import java.awt.image.BufferedImage;
import java.util.HashMap;
import java.util.Map;

public class ResourcesCache {

private static ResourcesCache instance;

private Map<String, BufferedImage> images;

private ResourcesCache() {
initializeImages();
}

public void initializeImages() {
if (images == null) {
images = new HashMap<String, BufferedImage>();
images.put(ResourceConstants.IMG_BOARD.toString(), LoadImage.load("board.jpg"));
images.put(ResourceConstants.IMG_BOARD_END_BAD.toString(), LoadImage.load("board_gameover.png"));
images.put(ResourceConstants.IMG_BOARD_END_GOOD.toString(), LoadImage.load("board_end.png"));
images.put(ResourceConstants.IMG_MISS.toString(), LoadImage.load("miss.png"));
images.put(ResourceConstants.IMG_HIT.toString(), LoadImage.load("ship.png"));
}
}

public static ResourcesCache getInstance() {
if (instance == null)
instance = new ResourcesCache();
return instance;
}

public BufferedImage getImages(ResourceConstants key) {
return images.get(key.toString());
}

}

0 comments on commit d43ff07

Please sign in to comment.