My goal is to draw some cubes for my Tetris program but I encountered a problem. I am following the MVP principle so my data structure and view is connected via presenter.
My idea is, that the view gets the data from the model so it can draw the blocks. In my implementation, the GraphicsContext and the commands to draw a block are initialized in the initializer method. The canvas gets injected by loading the FXML file and also the myGroup is set up.
Lets look at some code from my view controller:
public void start() throws IOException
{
initCanvas(); // initialize FXML -> injecting myCanvas; initialize myGroup
Stage stage = new Stage();
field = mainPresenter.initializeField();
landed = mainPresenter.initializeLanded();
final long startNanoTime = System.nanoTime();
new AnimationTimer()
{
@Override
public void handle(long now)
{
mainPresenter.update(); // exactly 1 frame of the game
// this can be initialized here but its already to late :(
landed = mainPresenter.initializeLanded();
}
}.start();
canvasScene = new Scene((Parent) myGroup);
stage.setTitle("Tetris");
stage.setScene(canvasScene);
stage.show();
}
The start() method gets called from my main class.
So here is my problem: the GraphicsContext needs the information from the landed field but the GC sits in the initialize method therefore I get a null pointer. -> At the point of the initialize method a cannot access the model.
@FXML
public void initialize()
{
// it would be nice if this here could be possible! :)
landed = mainPresenter.initializeLanded();
gc = myCanvas.getGraphicsContext2D();
// basically some drawing options here
gc.setFill(Color.BLUE);
gc.fillRect(0, 450, 50, 50);
}
I hope I could state my problem properly for you. If something is unclear please ask for more info! This is my first "big" application and im pretty sure there is an easy solution for my problem but simply find see it.