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.control.Button; import javafx.scene.input.KeyEvent; import javafx.scene.layout.BorderPane; import javafx.scene.paint.Color; import javafx.scene.text.Font; import javafx.scene.text.Text; import model.Board; import model.IModel; import model.LineAlgorithm; import observer.IObserver; /** * Created by loic on 22/09/16. */ public class MainWindowController implements IObserver { @FXML Canvas boardCanvas; @FXML Text score; @FXML Button restartButton; private ModelAdapter adapter; private IModel model; private int squareSize=100; private int squarePadding=10; private int[] boardPosition={30,0}; private int fontSize=60; 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(); } @FXML private void restartGame(ActionEvent event){ IModel model=new Board(4,4); ModelAdapter adapter=new ModelAdapter((Board) model); adapter.addObserver(this); adapter.addRandomNumber(); this.adapter=adapter; this.model=model; this.update(); } private void draw(){ this.score.setFont(new Font(40)); if(this.adapter.isLoosed()){ this.score.setText("Score : " + this.model.getScore() + "\n You loose !!!"); } else{ this.score.setText("Score : " + this.model.getScore()); } int[][] board=this.model.getBoard(); GraphicsContext gc = boardCanvas.getGraphicsContext2D(); gc.clearRect(0,0,500,500); gc.setFill(Color.rgb(187,173,160)); gc.fillRect(this.boardPosition[0],this.boardPosition[1], ((this.squareSize+squarePadding)*board.length)+squarePadding, ((this.squareSize+squarePadding)*board[0].length)+squarePadding); for(int i=0; i2048) { gc.setFill(Color.WHITE); } else{ gc.setFill(Color.BLACK); } if(value>0){ String strValue=""+value; int localFontSize=fontSize; if(strValue.length()==3){ localFontSize=localFontSize/2; }else if(strValue.length()==4){ localFontSize=localFontSize/3; } else if(strValue.length()>4){ localFontSize=localFontSize/4; } gc.setFont(new Font(localFontSize)); gc.fillText(strValue, x + (this.squareSize / 2) - ((localFontSize/4)+strValue.length()*localFontSize/5) , y + (this.squareSize / 2) + (localFontSize /3)); } } } } }