From f281a7189cdfce080e3bb34192e59ce97f350c20 Mon Sep 17 00:00:00 2001 From: loic Date: Thu, 22 Sep 2016 20:00:35 +0200 Subject: [PATCH] Begin javafx --- .../java/controller/MainWindowController.java | 120 ++++++++++++++++++ 1 file changed, 120 insertions(+) create mode 100644 src/main/java/controller/MainWindowController.java diff --git a/src/main/java/controller/MainWindowController.java b/src/main/java/controller/MainWindowController.java new file mode 100644 index 0000000..ccf137d --- /dev/null +++ b/src/main/java/controller/MainWindowController.java @@ -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() { + @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; i0){ + 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)); + + } + } + } + +}