I made this game which should imitate a spaceship and obstacles (planets). Move spaceship and try to hit everything. I will add some others features and functionality (better images or whatever).
I have several question.
My IDE (IntelliJ) gives me some warning and I try to remove them. One of them is "access can be package-private". I know what it means, it tries to "narrow down" the access. So, according to this I should have written for example
void myMethod()
instead of public void myMethod()
.
The question : should I be obedient and change everything?
I am confused because a lot of code is written in "bad" way.
Simply, listen to the compiler, right?
Other warning is "field can be converted to a local variable".
Don't make (private final int SOMETHING = 1231
) and make this in appropiate method, right? I want to ask because some variables have to be in the first lines of class (must be accessed generally by whole class) and some in one method. For me it's cleaner when I have all variables like this in one place, but compiler tells to write some here and some in methods.
Listen to the compiler one more time, right?
I would like to ask if my serialization is in proper way, and reading and writing to files.
What are the best classes which can deal with simple wav or mp3 file? I want to add some music to that.
Exceptions - I know I have to catch exceptions or add throws
but what should I do with that exception at the end? Make some message like JOptionPane
or what?
Each opinion is appreciated. Thanks in advance.
Main
class
package com.company;
import java.awt.BorderLayout;
import javax.swing.JFrame;
public class Main extends JFrame {
private static int WIDTH_GAME_FRAME = 800;
private static int HEIGHT_GAME_FRAME = 700;
private GamePanel gamePanel;
public Main() throws Exception {
this.setTitle("SpaceWar");
this.setLayout(new BorderLayout());
this.setSize(WIDTH_GAME_FRAME, HEIGHT_GAME_FRAME);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
gamePanel = new GamePanel();
this.addWindowListener(new java.awt.event.WindowAdapter() {
@Override
public void windowClosing(java.awt.event.WindowEvent windowEvent) {
gamePanel.exitGame();
}
});
this.add(gamePanel);
this.setResizable(false);
this.setVisible(true);
}
public static void main(String[] args) {
try {
new Main();
} catch (Exception e) {
e.printStackTrace();
}
}
public static int getWidthGameFrame() {
return WIDTH_GAME_FRAME;
}
public static int getHeightGameFrame() {
return HEIGHT_GAME_FRAME;
}
}
GamePanel
class
package com.company;
import java.awt.*;
import java.util.LinkedList;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import javax.swing.JPanel;
public class GamePanel extends JPanel {
public enum stateOfGame {GAME, MENU, HIGH_SCORE}
public enum SpaceShipWay {LEFT, RIGHT}
public static stateOfGame state = stateOfGame.MENU;
private SpaceShip spaceShip;
private int points = 0;
private Menu menu;
private String playerName;
private AllTimeLeaders allTimeLeaders;
private HealthBar healthBar;
private LinkedList<PlayerScore> loadedHighScoreList;
private HighScoreFile loadedHighScoreFile;
private FileReaderAndWriter fileReaderAndWriter;
private LinkedList<Bullet> bullets;
private LinkedList<Obstacle> obstacles;
private CollisionDetector collisionDetector;
public GamePanel() throws Exception {
this.setBackground(Color.BLACK);
menu = new Menu();
this.addMouseListener(new MouseInput(this));
fileReaderAndWriter = new FileReaderAndWriter();
fileReaderAndWriter.load();
loadedHighScoreFile = fileReaderAndWriter.getHighScoreFile();
if (loadedHighScoreFile == null)
loadedHighScoreFile = new HighScoreFile();
else
loadedHighScoreList = loadedHighScoreFile.getHighScoreList();
allTimeLeaders = new AllTimeLeaders(this, loadedHighScoreList);
spaceShip = new SpaceShip();
healthBar = new HealthBar();
collisionDetector = new CollisionDetector(this, spaceShip, healthBar);
bullets = collisionDetector.getBullets();
obstacles = collisionDetector.getObstacles();
KeyInput keyInput = new KeyInput(this, spaceShip);
ScheduledThreadPoolExecutor keyExecutor, bulletExecutor, obstacleExecutor;
keyExecutor = new ScheduledThreadPoolExecutor(5);
bulletExecutor = new ScheduledThreadPoolExecutor(5);
obstacleExecutor = new ScheduledThreadPoolExecutor(5);
Runnable bulletMove = () -> {
for (Bullet bullet : bullets)
bullet.move();
collisionDetector.checkBulletCollision();
};
Runnable obstacleMove = () -> {
for (Obstacle obstacle : obstacles)
if (obstacle.isObstacleActive())
obstacle.move();
else
obstacle.setObstacle();
collisionDetector.checkSpaceShipCollision();
collisionDetector.checkObstacleCollision();
};
keyExecutor.scheduleAtFixedRate(keyInput, 0L, 10L, TimeUnit.MILLISECONDS);
bulletExecutor.scheduleAtFixedRate(bulletMove, 0L, 15L, TimeUnit.MILLISECONDS);
obstacleExecutor.scheduleAtFixedRate(obstacleMove, 0L, 10L, TimeUnit.MILLISECONDS);
setFocusable(true);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (state == stateOfGame.GAME) {
spaceShip.paint(g);
healthBar.paint(g);
for (Bullet bullet : bullets)
if (bullet.isBulletActive())
bullet.paint(g);
for (Obstacle obstacle : obstacles)
if (obstacle.isObstacleActive())
obstacle.paint(g);
} else if (state == stateOfGame.MENU) {
menu.paint(g);
} else if (state == stateOfGame.HIGH_SCORE) {
allTimeLeaders.paint(g);
}
}
public void startBullet() {
for (Bullet bullet : bullets)
if (!bullet.isBulletActive()) {
bullet.setBySpaceShip(spaceShip);
break;
}
}
public void addPoints() {
int ADD_POINTS = 5;
points += ADD_POINTS;
}
public int getPoints() {
return points;
}
public String getName() {
return playerName;
}
public void setName(String s) {
playerName = s;
}
public void endOfLife() {
loadedHighScoreFile.add(playerName, points);
loadedHighScoreFile.sortHighScore();
allTimeLeaders.update(loadedHighScoreList);
state = GamePanel.stateOfGame.HIGH_SCORE;
}
public void exitGame() {
fileReaderAndWriter.save(loadedHighScoreFile);
System.exit(0);
}
public void resetGame() {
collisionDetector.resetObstacles();
healthBar.resetHealthBar();
}
}
Spaceship
class
package com.company;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
public class SpaceShip {
private int xSpaceShipPosition = 400;
private int ySpaceShipPosition = 600;
private BufferedImage spaceShipImage;
public SpaceShip() throws Exception {
loadImages();
}
public void move(GamePanel.SpaceShipWay way) {
final int SPACE_SHIP_MOVE = 5;
if (GamePanel.SpaceShipWay.LEFT == way && xSpaceShipPosition >= 0)
xSpaceShipPosition -= SPACE_SHIP_MOVE;
else if (GamePanel.SpaceShipWay.RIGHT == way && xSpaceShipPosition <= 730)
xSpaceShipPosition += SPACE_SHIP_MOVE;
}
public int getXSpaceShipPosition() {
return xSpaceShipPosition;
}
public int getYSpaceShipPosition() {
return ySpaceShipPosition;
}
public void paint(Graphics g) {
g.drawImage(spaceShipImage, xSpaceShipPosition, ySpaceShipPosition, null);
}
public void loadImages() throws Exception {
spaceShipImage = ImageIO.read(getClass().getResource
("/com/images/spaceship.png"));
}
}
Bullet
class
package com.company;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
public class Bullet {
private Boolean isActive = false;
private final int BULLET_MOVE = 10;
private final int TO_CENTER = 29;
private int xBulletPosition, yBulletPosition;
private BufferedImage bulletImage;
public Bullet() throws Exception {
loadImages();
}
public void setBySpaceShip(SpaceShip spaceShip) {
xBulletPosition = spaceShip.getXSpaceShipPosition() + TO_CENTER;
yBulletPosition = spaceShip.getYSpaceShipPosition();
isActive = true;
}
public void move() {
if (yBulletPosition < 0)
isActive = false;
if (isActive) {
yBulletPosition -= BULLET_MOVE;
}
}
public int getBulletXPosition() {
return xBulletPosition;
}
public int getBulletYPosition() {
return yBulletPosition;
}
public boolean isBulletActive() {
return isActive;
}
public void changeBulletActive() {
isActive = !isActive;
}
public void paint(Graphics g) {
g.drawImage(bulletImage, xBulletPosition, yBulletPosition, null);
}
public void loadImages() throws Exception {
bulletImage = ImageIO.read(getClass().getResource
("/com/images/bullet.png"));
}
}
Obstacle
class
package com.company;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Random;
public class Obstacle {
private boolean isActive = true;
private int chooser;
private int xObstaclePosition;
private int yObstaclePosition;
private int xDirection;
private int yDirection;
private final int RIGHT_WALL = Main.getWidthGameFrame() - 32;
private Random randomGenerator;
private BufferedImage obstacleImage;
private BufferedImage venus, mars, planet;
private BufferedImage earth, jupiter, uranus;
public Obstacle() throws Exception {
loadImages();
randomGenerator = new Random();
this.xObstaclePosition = 2 * (randomGenerator.nextInt(250) + 50);
this.yObstaclePosition = -2 * (randomGenerator.nextInt(125) + 25);
this.xDirection = randomGenerator.nextInt(5) + 2;
this.yDirection = randomGenerator.nextInt(5) + 2;
chooser = randomGenerator.nextInt(6);
chooseImage();
}
public void move() {
if (xObstaclePosition + xDirection <= 0 || xObstaclePosition + xDirection >= RIGHT_WALL)
xDirection = -xDirection;
if (yObstaclePosition > Main.getHeightGameFrame())
isActive = false;
xObstaclePosition += xDirection;
yObstaclePosition += yDirection;
}
public int getXObstaclePosition() {
return xObstaclePosition;
}
public int getYObstaclePosition() {
return yObstaclePosition;
}
public boolean isObstacleActive() {
return isActive;
}
public int getXDirection() {
return xDirection;
}
public int getYDirection() {
return yDirection;
}
public void changeXDirection(int xDirection) {
this.xDirection = xDirection;
}
public void changeYDirection(int yDirection) {
this.yDirection = yDirection;
}
public void changeObstacleActive() {
isActive = !isActive;
}
public void setObstacle() {
this.xObstaclePosition = randomGenerator.nextInt(500) + 100;
this.yObstaclePosition = -(randomGenerator.nextInt(250) + 50);
this.xDirection = randomGenerator.nextInt(5) + 1;
this.yDirection = randomGenerator.nextInt(5) + 1;
isActive = true;
}
private void chooseImage() {
switch (chooser) {
case 0:
obstacleImage = venus;
break;
case 1:
obstacleImage = earth;
break;
case 2:
obstacleImage = jupiter;
break;
case 3:
obstacleImage = uranus;
break;
case 4:
obstacleImage = mars;
break;
case 5:
obstacleImage = planet;
break;
}
}
public void paint(Graphics g) {
g.drawImage(obstacleImage, xObstaclePosition, yObstaclePosition, null);
}
private void loadImages() throws Exception {
venus = ImageIO.read(getClass().getResource
("/com/images/venus.png"));
earth = ImageIO.read(getClass().getResource
("/com/images/earth.png"));
jupiter = ImageIO.read(getClass().getResource
("/com/images/jupiter.png"));
uranus = ImageIO.read(getClass().getResource
("/com/images/uranus.png"));
mars = ImageIO.read(getClass().getResource
("/com/images/mars.png"));
planet = ImageIO.read(getClass().getResource
("/com/images/planet.png"));
}
}
HealthBar
class
package com.company;
import java.awt.*;
public class HealthBar {
private int health = 200;
private int healthSubstaction = 20;
public void substractHealth() {
health -= healthSubstaction;
}
public void resetHealthBar() {
health = 200;
}
public boolean isEnd() {
return health < 10;
}
public void paint(Graphics g) {
g.setColor(Color.gray);
g.fillRect(5, 5, 200, 50);
g.setColor(Color.green);
g.fillRect(5, 5, health, 50);
g.setColor(Color.white);
g.drawRect(5, 5, 200, 50);
}
}
Menu
class
package com.company;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
public class Menu {
private BufferedImage playImage, helpImage, exitImage;
public static final int X_IMAGE_POSITION = 251;
public Menu() throws IOException {
playImage = ImageIO.read(getClass().getResource
("/com/images/play.png"));
helpImage = ImageIO.read(getClass().getResource
("/com/images/help.png"));
exitImage = ImageIO.read(getClass().getResource
("/com/images/exit.png"));
}
public void paint(Graphics g) {
g.drawImage(playImage, X_IMAGE_POSITION, 220, null);
g.drawImage(helpImage, X_IMAGE_POSITION, 340, null);
g.drawImage(exitImage, X_IMAGE_POSITION, 460, null);
g.setColor(Color.white);
Font highScore = new Font("arial", Font.BOLD, 40);
g.setFont(highScore);
g.drawString("High-Score", 300, 600);
g.drawRect(300, 560, 220, 50);
}
}
KeyInput
class
package com.company;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JComponent;
import javax.swing.KeyStroke;
import java.util.EnumMap;
public final class KeyInput implements Runnable {
private static final String PRESSED = "pressed";
private static final String RELEASED = "released";
private final EnumMap<Key, Boolean> keyMap;
private final GamePanel gPanel;
private final SpaceShip spaceShip;
private int countForBullet = 0;
public KeyInput(GamePanel gPanel, SpaceShip spaceShip) {
this.keyMap = new EnumMap<>(Key.class);
this.gPanel = gPanel;
this.spaceShip = spaceShip;
resetKeyInput();
ActionMap actionMap = gPanel.getActionMap();
int condition = JComponent.WHEN_IN_FOCUSED_WINDOW;
InputMap inputMap = gPanel.getInputMap(condition);
for (Key key : Key.values()) {
KeyStroke pressedKeyStroke = KeyStroke.getKeyStroke
(key.getKeyCode(), 0, false);
KeyStroke releasedKeyStroke = KeyStroke.getKeyStroke
(key.getKeyCode(), 0, true);
inputMap.put(pressedKeyStroke, key.getText() + PRESSED);
inputMap.put(releasedKeyStroke, key.getText() + RELEASED);
actionMap.put(key.getText() + PRESSED, new MyArrowBinding(key, false));
actionMap.put(key.getText() + RELEASED, new MyArrowBinding(key, true));
}
}
@Override
public void run() {
if (GamePanel.state == GamePanel.stateOfGame.GAME) {
int order = 1;
gPanel.repaint();
for (Key key : keyMap.keySet()) {
if (keyMap.get(key) && order == 1) {
spaceShip.move(GamePanel.SpaceShipWay.LEFT);
gPanel.repaint();
} else if (keyMap.get(key) && order == 2) {
spaceShip.move(GamePanel.SpaceShipWay.RIGHT);
gPanel.repaint();
} else if (keyMap.get(key) && order == 3) {
if (++countForBullet == 10) {
gPanel.startBullet();
countForBullet = 0;
}
gPanel.repaint();
}
order++;
}
}
}
private void resetKeyInput() {
keyMap.put(Key.LEFT, false);
keyMap.put(Key.RIGHT, false);
keyMap.put(Key.SPACE, false);
}
private class MyArrowBinding extends AbstractAction {
private final Key key;
private final boolean released;
public MyArrowBinding(Key key, boolean released) {
this.key = key;
this.released = released;
}
@Override
public void actionPerformed(ActionEvent aEvt) {
keyMap.put(key, !released);
}
}
private enum Key {
LEFT("Left", KeyEvent.VK_LEFT),
RIGHT("Right", KeyEvent.VK_RIGHT),
SPACE("Space", KeyEvent.VK_SPACE);
private final String text;
private final int keyCode;
Key(String text, int keyCode) {
this.text = text;
this.keyCode = keyCode;
}
public String getText() {
return text;
}
public int getKeyCode() {
return keyCode;
}
}
}
MouseInput
class
package com.company;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
public class MouseInput extends MouseAdapter {
private JTextField playerName = new JTextField();
private Object[] message = {"Name of player : ", playerName};
private final GamePanel gPanel;
private final String helpInfo = "This is a simple SpaceWar game.\n" +
"Use arrows to move the spaceship and Spaca to hit.\n";
public MouseInput(GamePanel gPanel) {
this.gPanel = gPanel;
}
@Override
public void mousePressed(MouseEvent e) {
int xMousePosition = e.getX();
int yMousePosition = e.getY();
if (GamePanel.state == GamePanel.stateOfGame.MENU &&
xMousePosition > Menu.X_IMAGE_POSITION && xMousePosition < 551) {
if (yMousePosition > 220 && yMousePosition < 290) {
int option = JOptionPane.showConfirmDialog(null, message,
"Name of players", JOptionPane.OK_CANCEL_OPTION);
if (option == JOptionPane.OK_OPTION && !playerName.getText().isEmpty()) {
gPanel.setName(playerName.getText());
gPanel.resetGame();
GamePanel.state = GamePanel.stateOfGame.GAME;
}
} else if (yMousePosition > 340 && yMousePosition < 410) {
JOptionPane.showMessageDialog(gPanel, helpInfo,
"Help information", JOptionPane.INFORMATION_MESSAGE);
} else if (yMousePosition > 460 && yMousePosition < 530) {
gPanel.exitGame();
} else if (yMousePosition > 560 && yMousePosition < 620)
GamePanel.state = GamePanel.stateOfGame.HIGH_SCORE;
gPanel.repaint();
}
if (GamePanel.state == GamePanel.stateOfGame.HIGH_SCORE &&
xMousePosition > 550 && xMousePosition < 710 &&
yMousePosition > 560 && yMousePosition < 610) {
GamePanel.state = GamePanel.stateOfGame.MENU;
gPanel.repaint();
}
}
}
PlayerScore
class
package com.company;
import java.io.Serializable;
public class PlayerScore implements Comparable<PlayerScore>, Serializable {
private String nameOfPlayer;
private int score;
private static final long serialVersionUID = 1L;
PlayerScore(String nameOfPlayer, int score) {
this.nameOfPlayer = nameOfPlayer;
this.score = score;
}
public int getScore() {
return score;
}
public String getNameOfPlayer() {
return nameOfPlayer;
}
@Override
public int compareTo(PlayerScore highScore) {
if (this.score > highScore.getScore())
return 1;
else if (this.score < highScore.getScore())
return -1;
else
return 0;
}
}
CollisionDetector
class
package com.company;
import java.util.LinkedList;
public class CollisionDetector {
private final int IMAGE_RANGE = 60, OBSTACLE_RANGE = 32;
private final int NUMBER_OF_OBSTACLES = 6;
private LinkedList<Bullet> bullets;
private LinkedList<Obstacle> obstacles;
private SpaceShip spaceShip;
private HealthBar healthBar;
private GamePanel gPanel;
public CollisionDetector(GamePanel gPanel, SpaceShip spaceShip, HealthBar healthBar) throws Exception {
this.gPanel = gPanel;
this.spaceShip = spaceShip;
this.healthBar = healthBar;
bullets = new LinkedList<>();
obstacles = new LinkedList<>();
fillBullet();
fillObstacles();
}
public void checkSpaceShipCollision() {
for (Obstacle obstacle : obstacles) {
if (obstacle.isObstacleActive() && GamePanel.state == GamePanel.stateOfGame.GAME &&
obstacle.getYObstaclePosition() + OBSTACLE_RANGE > spaceShip.getYSpaceShipPosition()
&& obstacle.getYObstaclePosition() < spaceShip.getYSpaceShipPosition() + IMAGE_RANGE
&& obstacle.getXObstaclePosition() < spaceShip.getXSpaceShipPosition() + IMAGE_RANGE
&& obstacle.getXObstaclePosition() + OBSTACLE_RANGE > spaceShip.getXSpaceShipPosition()) {
if (!healthBar.isEnd()) {
healthBar.substractHealth();
obstacle.changeObstacleActive();
} else {
gPanel.endOfLife();
}
gPanel.repaint();
}
}
}
public void checkObstacleCollision() {
for (int i = 0; i < NUMBER_OF_OBSTACLES - 1; i++)
for (int j = i + 1; j < NUMBER_OF_OBSTACLES; j++)
if (GamePanel.state == GamePanel.stateOfGame.GAME &&
obstacles.get(i).getYObstaclePosition() + OBSTACLE_RANGE > obstacles.get(j).getYObstaclePosition()
&& obstacles.get(i).getYObstaclePosition() < obstacles.get(j).getYObstaclePosition() + OBSTACLE_RANGE
&& obstacles.get(i).getXObstaclePosition() < obstacles.get(j).getXObstaclePosition() + OBSTACLE_RANGE
&& obstacles.get(i).getXObstaclePosition() + OBSTACLE_RANGE > obstacles.get(j).getXObstaclePosition()) {
int tempXDirection = obstacles.get(i).getXDirection();
int tempYDirection = obstacles.get(i).getYDirection();
obstacles.get(i).changeXDirection(obstacles.get(j).getXDirection());
obstacles.get(i).changeYDirection(obstacles.get(j).getYDirection());
obstacles.get(j).changeXDirection(tempXDirection);
obstacles.get(j).changeYDirection(tempYDirection);
}
}
public void checkBulletCollision() {
for (Bullet bullet : bullets)
for (Obstacle obstacle : obstacles) {
if (GamePanel.state == GamePanel.stateOfGame.GAME && bullet.isBulletActive() && obstacle.getXObstaclePosition() < bullet.getBulletXPosition() &&
bullet.getBulletXPosition() < obstacle.getXObstaclePosition() + OBSTACLE_RANGE &&
obstacle.getYObstaclePosition() < bullet.getBulletYPosition() &&
bullet.getBulletYPosition() < obstacle.getYObstaclePosition() + OBSTACLE_RANGE) {
bullet.changeBulletActive();
obstacle.changeObstacleActive();
gPanel.addPoints();
}
}
}
private void fillBullet() throws Exception {
int numberOfBullets = 10;
for (int i = 0; i < numberOfBullets; i++)
bullets.add(new Bullet());
}
private void fillObstacles() throws Exception {
for (int i = 0; i < NUMBER_OF_OBSTACLES; i++)
obstacles.add(new Obstacle());
}
public void resetObstacles() {
for (Obstacle obstacle : obstacles)
obstacle.setObstacle();
}
public LinkedList<Bullet> getBullets() {
return bullets;
}
public LinkedList<Obstacle> getObstacles() {
return obstacles;
}
}
AllTimeLeaders
class
package com.company;
import java.awt.*;
import java.util.LinkedList;
public class AllTimeLeaders {
private GamePanel gamePanel;
private LinkedList<PlayerScore> highScoreList;
AllTimeLeaders(GamePanel gamePanel, LinkedList<PlayerScore> highScoreList) {
this.gamePanel = gamePanel;
this.highScoreList = highScoreList;
}
public void update(LinkedList<PlayerScore> highScoreList) {
this.highScoreList = highScoreList;
}
public void paint(Graphics g) {
Font titleFont = new Font("arial", Font.BOLD, 50);
Font resultFont = new Font("arial", Font.BOLD, 20);
Font allTimeLeadersFont = new Font("arial", Font.BOLD, 30);
Font backString = new Font("arial", Font.BOLD, 50);
g.setColor(Color.WHITE);
g.setFont(backString);
g.drawString("BACK", 550, 600);
g.drawRect(550, 560, 160, 50);
g.setFont(titleFont);
g.drawString("GAME OVER", Main.getWidthGameFrame() / 2 - 180, 100);
g.setFont(allTimeLeadersFont);
g.drawString("All-time results", 270, 250);
g.setFont(resultFont);
g.drawString("Your score : " + gamePanel.getPoints(), 50, 180);
int i = 1;
for (PlayerScore playerScore : highScoreList) {
g.drawString(i + ". " + playerScore.getNameOfPlayer(), 50, 280 + 40 * i);
g.drawString("" + playerScore.getScore(), 200, 280 + 40 * i);
if (++i == 11)
break;
}
}
}
HighScoreFile
class
package com.company;
import java.io.Serializable;
import java.util.Collections;
import java.util.LinkedList;
public class HighScoreFile implements Serializable {
private static final long serialVersionUID = 43242L;
private LinkedList<PlayerScore> highScoreList = new LinkedList<>();
HighScoreFile() {
sortHighScore();
}
public void add(String playerName, int playerScore) {
highScoreList.add(new PlayerScore(playerName, playerScore));
}
public void sortHighScore() {
Collections.sort(highScoreList, Collections.reverseOrder());
}
public LinkedList<PlayerScore> getHighScoreList() {
return highScoreList;
}
}
FileReaderAndWriter
class
package com.company;
import java.io.*;
public class FileReaderAndWriter {
private final String fileName = "/Score.sav";
private HighScoreFile loadedHighScoreFile = null;
public void save(Serializable objectToSerialize) {
try {
FileOutputStream fileOutputStream = new FileOutputStream(createDataFolder() + fileName);
ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
objectOutputStream.writeObject(objectToSerialize);
objectOutputStream.flush();
objectOutputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public void load() throws ClassNotFoundException, IOException {
if (checkFileExists()) {
FileInputStream fileInputStream = new FileInputStream(createDataFolder() + fileName);
ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
loadedHighScoreFile = (HighScoreFile) objectInputStream.readObject();
objectInputStream.close();
}
}
private boolean checkFileExists() {
return new File(createDataFolder() + fileName).isFile();
}
private String createDataFolder() {
String homeLocation = System.getProperty("user.home");
String OS = System.getProperty("os.name").toLowerCase();
if (OS.contains("win"))
homeLocation = System.getenv("appdata");
else if (OS.contains("mac"))
homeLocation += "~/Library/Application Support";
else if (OS.contains("nix") || OS.contains("aix") || OS.contains("nux"))
homeLocation += "~/.";
File dir = new File(homeLocation);
String root = "/SpaceWar";
dir = new File(dir, root);
if (!dir.exists())
dir.mkdir();
return dir.getAbsolutePath();
}
public HighScoreFile getHighScoreFile() {
return loadedHighScoreFile;
}
}