Skip to content
This repository has been archived by the owner on Nov 29, 2017. It is now read-only.

Commit

Permalink
Fix for drawLines method
Browse files Browse the repository at this point in the history
  • Loading branch information
vapour101 committed Aug 19, 2017
1 parent 180f113 commit 8c9a906
Showing 1 changed file with 29 additions and 24 deletions.
53 changes: 29 additions & 24 deletions src/main/java/ui/BoardController.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,17 @@
import javafx.scene.paint.Color;
import javafx.util.Pair;
import logic.Board;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.shape.Rectangle;
import util.CoordProjector;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.stage.Stage;

import static util.Coords.getCoords;

public class BoardController implements Initializable {
public Canvas boardCanvas;
public Pane pane;
private Board board;
public primaryStage;

public BoardController() {
board = new Board();
Expand Down Expand Up @@ -75,31 +74,30 @@ private void drawBackground() {
}

private void drawBoardLines() {
double length = getBoardLength();
Pair<Double, Double> topLeft = getTopLeftCorner();

Group root = new Group();
double canvasWidth = boardCanvas.getWidth();
double canvasHeight = boardCanvas.getHeight();
Scene scene = new Scene(root, canvasWidth, canvasHeight, Color.GREEN);
GraphicsContext context = boardCanvas.getGraphicsContext2D();

Rectangle[][] rec = new Rectangle[canvasWidth][canvasHeight];
for (int i = 1; i < 20; i++) {
//Horizontal Lines
Pair<Double, Double> start = CoordProjector.fromBoardCoords(getCoords(1, i), length);
Pair<Double, Double> end = CoordProjector.fromBoardCoords(getCoords(19, i), length);

for(int i=0; i<canvasWidth; i++){
for(int j=0; j<canvasHeight; j++){
start = addCoords(start, topLeft);
end = addCoords(end, topLeft);

rec[i][j] = new Rectangle();
rec[i][j].setX(i * canvasWidth);
rec[i][j].setY(j * canvasWidth);
rec[i][j].setWidth(canvasWidth);
rec[i][j].setHeight(canvasWidth);
rec[i][j].setFill(null);
context.strokeLine(start.getKey(), start.getValue(), end.getKey(), end.getValue());

rec[i][j].setStrokeWidth(3);
rec[i][j].setStroke(Color.BLACK);
root.getChildren().add(rec[i][j]);
}
//Vertical Lines
start = CoordProjector.fromBoardCoords(getCoords(i, 1), length);
end = CoordProjector.fromBoardCoords(getCoords(i, 19), length);

start = addCoords(start, topLeft);
end = addCoords(end, topLeft);

context.strokeLine(start.getKey(), start.getValue(), end.getKey(), end.getValue());
}
primaryStage.setScene(scene);
primaryStage.show();

}

Expand Down Expand Up @@ -141,4 +139,11 @@ public void initialize(URL url, ResourceBundle resourceBundle) {

drawBoard();
}

private Pair<Double, Double> addCoords(Pair<Double, Double> lhs, Pair<Double, Double> rhs) {
double x = lhs.getKey() + rhs.getKey();
double y = lhs.getValue() + rhs.getValue();

return new Pair<>(x, y);
}
}

0 comments on commit 8c9a906

Please sign in to comment.