Begin javafx
This commit is contained in:
parent
64cdf57209
commit
f281a7189c
1 changed files with 120 additions and 0 deletions
120
src/main/java/controller/MainWindowController.java
Normal file
120
src/main/java/controller/MainWindowController.java
Normal file
|
@ -0,0 +1,120 @@
|
|||
package controller;
|
||||
|
||||
import adapter.IModelAdapter;
|
||||
import adapter.ModelAdapter;
|
||||
import javafx.event.ActionEvent;
|
||||
import javafx.event.EventHandler;
|
||||
import javafx.fxml.FXML;
|
||||
import javafx.scene.Group;
|
||||
import javafx.scene.Scene;
|
||||
import javafx.scene.canvas.Canvas;
|
||||
import javafx.scene.canvas.GraphicsContext;
|
||||
import javafx.scene.input.KeyEvent;
|
||||
import javafx.scene.layout.BorderPane;
|
||||
import javafx.scene.paint.Color;
|
||||
import javafx.scene.text.Text;
|
||||
import model.Board;
|
||||
import model.IModel;
|
||||
import observer.IObserver;
|
||||
|
||||
|
||||
/**
|
||||
* Created by loic on 22/09/16.
|
||||
*/
|
||||
public class MainWindowController implements IObserver {
|
||||
|
||||
@FXML Canvas boardCanvas;
|
||||
|
||||
private ModelAdapter adapter;
|
||||
private IModel model;
|
||||
|
||||
private int squareSize=50;
|
||||
private int squarePadding=10;
|
||||
private int[] boardPosition={0,0};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public void loadComponent(ModelAdapter adapter, IModel model, Scene scene){
|
||||
this.adapter=adapter;
|
||||
this.model=model;
|
||||
this.update();
|
||||
scene.setOnKeyPressed(new EventHandler<KeyEvent>() {
|
||||
@Override
|
||||
public void handle(KeyEvent event) {
|
||||
if(event.getCode().toString().equals("UP")){
|
||||
goUp(null);
|
||||
}
|
||||
else if(event.getCode().toString().equals("DOWN")){
|
||||
goDown(null);
|
||||
}
|
||||
else if(event.getCode().toString().equals("LEFT")){
|
||||
goLeft(null);
|
||||
}
|
||||
else if(event.getCode().toString().equals("RIGHT")){
|
||||
goRight(null);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
@FXML protected void goUp(ActionEvent event) {
|
||||
this.adapter.goUp();
|
||||
}
|
||||
@FXML protected void goDown(ActionEvent event) {
|
||||
this.adapter.goDown();
|
||||
}
|
||||
@FXML protected void goLeft(ActionEvent event) {
|
||||
this.adapter.goLeft();
|
||||
}
|
||||
@FXML protected void goRight(ActionEvent event) {
|
||||
this.adapter.goRight();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update() {
|
||||
this.draw();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
private void draw(){
|
||||
|
||||
GraphicsContext gc = boardCanvas.getGraphicsContext2D();
|
||||
gc.clearRect(0,0,500,500);
|
||||
int[][] board=this.model.getBoard();
|
||||
|
||||
|
||||
|
||||
for(int i=0; i<board.length;i++) {
|
||||
for (int j = 0; j < board[i].length; j++) {
|
||||
gc.setFill(Color.GRAY);
|
||||
|
||||
int x=this.boardPosition[0] + (j*this.squareSize);
|
||||
int y=this.boardPosition[1] + (i*this.squareSize);
|
||||
|
||||
if(j>0){
|
||||
x+=j*squarePadding;
|
||||
}
|
||||
if(i>0){
|
||||
y+=i*squarePadding;
|
||||
}
|
||||
|
||||
gc.fillRect(x,y, this.squareSize, this.squareSize);
|
||||
gc.setFill(Color.BLACK);
|
||||
|
||||
int value=board[i][j];
|
||||
if(value<0){
|
||||
value=0;
|
||||
}
|
||||
|
||||
gc.fillText("" + value, x + (this.squareSize / 2), y + (this.squareSize / 2));
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Reference in a new issue