0

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

}

  • See this [question](http://stackoverflow.com/questions/13258790/how-to-use-getters-to-return-an-image-in-java) – Fady Saad May 02 '17 at 03:53
  • @user7790438 except that linked question if for an awt image and this question is about JavaFX, which has a different Image type. – jewelsea May 02 '17 at 04:08
  • @jewelsea could you give me a quick example of how i would set a cell to an image, i'm just a bit confused. –  May 02 '17 at 07:17

2 Answers2

1

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.

Community
  • 1
  • 1
jewelsea
  • 150,031
  • 14
  • 366
  • 406
  • could you give me a quick example of how i would set a cell to an image, i'm just a bit confused. –  May 02 '17 at 07:18
  • See the Square and SquareSkin classes of this [noughts and crosses game](https://gist.github.com/jewelsea/5115901). That is similar to your cell concept. It's a bit more complicated than you need but hopefully you can understand ig. Basically you need to put the ImageView in a parent node (usually a layout pane such as a StackPane or GridPane) and add the parent node to a scene. – jewelsea May 02 '17 at 11:41
  • Hi, sorry for the inconvenience i just added another part of my code to my question above. Its just that im having a hard time implementing your piece of code into mine. Would you be able to help me with this? i dont actually need the characters to be displayed, just the images –  May 02 '17 at 12:34
  • This now seems different from the original question. The question now seems to be: "How do I creates board game grid in JavaFX?". I think it is best to ask it as a new question. You will likely get more and better answers that way. You can reference this question from the new question. – jewelsea May 03 '17 at 03:10
0

I guess you can use byte array for image returning, and then show it on screen