so im Grunde bin ich ein Spielbrett mit javafx erstellen und ich habe eine Zelle Zustandsklasse, die einen Zeichenwert im Moment abhängig von was in der Zelle zurückgibt. Also im Grunde, wenn die Zelle leer ist, gibt es ' '
zurück, wenn ich einen Spieler darin hat, gibt es '@'
und so weiter für verschiedene Zellenzustände zurück. Ich habe mich nur gefragt, wie ich ein Bild anstelle von Zeichen zurückgeben könnte.Wie man ein Bild
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;
}
}
Siehe diese [Frage] (http://stackoverflow.com/questions/13258790/how-to-use-getters-to-return-an-image-in-java) –
@ user7790438 mit Ausnahme dieser verknüpften Frage, wenn für ein AWT-Bild und diese Frage bezieht sich auf JavaFX, das einen anderen Image-Typ hat. – jewelsea
@jewelsea Könntest du mir ein kurzes Beispiel geben, wie ich eine Zelle zu einem Bild machen könnte? Ich bin nur ein bisschen verwirrt. – javanoob