Reputation:
so basically i am creating a gameboard using javafx and i have a cell state class that returns a character value at the moment depending on whats in the cell. So basically if the cell is empty it returns ' '
, if i has a player in it it returns '@'
and so on for different cell states. i was just wondering how i could return an image instead of characters.
public class Cell {
CellState cellState;
public Cell(CellState newCellState) {
cellState = newCellState;
}
public CellState getCellState() {
return cellState;
}
public void setCellState(CellState newCellState)
{
cellState = newCellState;
}
public char displayCellState()
{
return getCellStateCharacter(cellState);
}
public char getCellStateCharacter(CellState newCellState)
{
switch (newCellState)
{
case EMPTY:
return ' ';
case PLAYER:
return '@';
case MONSTER:
return '&';
case POISON:
return '*';
case BLOCKED:
return '#';
default:
return ' ';
}
}
}
MY CELL STATE CLASS
public enum CellState
{
EMPTY,
PLAYER,
MONSTER,
POISON,
BLOCKED
};
public class GameBoard {
static final int BOARD_WIDTH = 10;
static final int BOARD_HEIGHT = 10;
Cell[][] boardCells;
int width;
int height;
public GameBoard()
{
boardCells = new Cell[BOARD_WIDTH][BOARD_HEIGHT];
width = BOARD_WIDTH;
height = BOARD_HEIGHT;
}
public void initGameBoard()
{
for (int i = 0; i < height; ++i)
{
for (int j = 0; j < width; ++j)
{
boardCells[j][i] = new Cell(CellState.EMPTY);
}
}
boardCells[0][0].setCellState(CellState.PLAYER);
boardCells[2][4].setCellState(CellState.MONSTER);
boardCells[2][6].setCellState(CellState.MONSTER);
boardCells[7][8].setCellState(CellState.POISON);
boardCells[5][0].setCellState(CellState.BLOCKED);
boardCells[5][1].setCellState(CellState.BLOCKED);
boardCells[5][2].setCellState(CellState.BLOCKED);
boardCells[5][3].setCellState(CellState.BLOCKED);
}
public String displayBoard()
{
String output = "";
output +="| |";
for (int i = 0; i < width; ++i)
{
output +=i + "|";
}
output +="\n";
for (int j = 0; j < height; ++j)
{
output +="|" + j + "|";
for (int k = 0; k < width; ++k)
{
output +=boardCells[k][j].displayCellState() + "|";
}
output +="\n";
}
return output;
}
}
Upvotes: 0
Views: 2633
Reputation: 159536
enums are classes too. So as the CellState enum is representing your states, probably best to encode all of the relevant info for a given state straight into the CellState class. That would include the character and the image.
import javafx.scene.image.Image;
public enum CellState {
EMPTY(' ', "empty.png"),
PLAYER('@', "player.png"),
MONSTER('&', "monster.png"),
POISON('*', "poison.png"),
BLOCKED('#', "blocked.png");
private final char cellChar;
private final Image cellImage;
CellState(char cellChar, String imageLoc) {
this.cellChar = cellChar;
this.image = new Image(imageLoc);
}
public char getChar() {
return cellChar;
}
public Image getImage() {
return cellImage;
}
@Override
public String toString() {
return Character.toString(cellChar);
}
}
Then you keep your cell API as before, but you can drop the getCellStateCharacter()
method and replace its usage with cell.getCellState().getChar()
, and similarly for image it would be cell.getCellState().getImage()
.
To understand where the Image constructor gets the image from, see:
If you want to get an image from the classpath (e.g. when the image is packaged in a jar), you can use:
new Image(getClass().getResourceAsStream("player.png"))
The above will fetch the image from the same folder in the classpath where the CellState.class file is located.
Upvotes: 1
Reputation: 71
I guess you can use byte array for image returning, and then show it on screen
Upvotes: 0