Newer
Older
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.layout.GridPane;
import java.io.IOException;
import java.net.URL;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import model.Board;
public class GameController implements Initializable {
/* ----- FXML ATTRIBUTES ----- */
@FXML
private GridPane board;
@FXML
/* ----- ATTRIBUTES ----- */
/**
*
*/
private static final String LEVEL_FILENAME = "data/level";
*
*/
private static final int HOW_MANY_LEVELS = 1;
/**
*
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/**
*
*/
private Color[] colors;
/**
*
*/
private IntegerProperty levelNumber;
public GameController() {
levelNumber = new SimpleIntegerProperty();
}
public void nextLevel() {
levelNumber.set(levelNumber.get() + 1);
try {
model = new Board(LEVEL_FILENAME + levelNumber.get() + ".txt");
} catch (IOException e) {
System.out.println(LEVEL_FILENAME + levelNumber.get() + ".txt not found");
}
}
private void initColors() {
colors = new Color[model.getCarsNumber()];
colors[0] = Color.RED;
for (int i = 1; i < colors.length; i++) {
Random random = new Random();
colors[i] = Color.rgb(random.nextInt(256), random.nextInt(256), random.nextInt(256));
}
}
private void initBoard() {
for (int i = 0; i < model.getSize(); i++)
for (int j = 0; j < model.getSize(); j++)
setCaseColor(i, j, model.getValue(i, j));
}
private void setCaseColor(int i, int j, int value) {
Iterator<Node> itr = board.getChildren().iterator();
for (int k = 0; k < i * model.getSize() + j; k++)
itr.next();
((Rectangle) itr.next()).setFill(colors[value]);
}
@Override
public void initialize(URL location, ResourceBundle resources) {
double size = 300.0 / model.getSize();
for (int i = 0; i < model.getSize(); i++)
for (int j = 0; j < model.getSize(); j++)
board.add(new Rectangle(size, size), i, j);
labelLevelNumber.textProperty().bind(levelNumber.asString());
initColors();
initBoard();