Skip to content
Snippets Groups Projects
Commit 92752605 authored by Nimrod Abramovich's avatar Nimrod Abramovich
Browse files

Initial Commit

parents
No related branches found
No related tags found
No related merge requests found
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="con" path="org.eclipse.jdt.USER_LIBRARY/JavaFx"/>
<classpathentry kind="src" path="src"/>
<classpathentry kind="src" path="Images"/>
<classpathentry kind="output" path="bin"/>
</classpath>
.project 0 → 100644
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>Programming II Coursework</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.8
Images/redo_icon.png

400 B

Images/undo_icon.png

394 B

File added
File added
File added
bin/redo_icon.png

400 B

bin/undo_icon.png

394 B

package coursework;
import javafx.scene.paint.Color;
public class Cage {
private int[][] squareIds;
private int value;
private String operator;
private Square[][] gridSquares;
public Cage(int[][] squareIds) {
this.squareIds = squareIds;
this.gridSquares = Main.getGridSquares();
buildCage();
}
public void buildCage() {
for (int aIndex[] : squareIds) {
for (int bIndex[] : squareIds) {
if (!aIndex.equals(bIndex)) {
int rowDiff = aIndex[1] - bIndex[1];
int colDiff = aIndex[0] - bIndex[0];
if (rowDiff == -1 && colDiff == 0) {
gridSquares[aIndex[0]][aIndex[1]].setRightStroke(2, Color.LIGHTGREY);
}
else if (rowDiff == 1 && colDiff == 0) {
gridSquares[aIndex[0]][aIndex[1]].setLeftStroke(2, Color.LIGHTGREY);
}
else if (rowDiff == 0 && colDiff == -1) {
gridSquares[aIndex[0]][aIndex[1]].setBottomStroke(2, Color.LIGHTGREY);
}
else if (rowDiff == 0 && colDiff == 1) {
gridSquares[aIndex[0]][aIndex[1]].setTopStroke(2, Color.LIGHTGREY);
}
}
}
}
}
}
\ No newline at end of file
package coursework;
import javafx.application.*;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.HLineTo;
import javafx.scene.shape.Line;
import javafx.scene.shape.LineTo;
import javafx.scene.shape.MoveTo;
import javafx.scene.shape.Path;
import javafx.scene.shape.Rectangle;
import javafx.scene.shape.VLineTo;
import javafx.stage.Stage;
public class Main extends Application {
final private double GRID_PERCENTAGE = 0.75;
final static private int N = 4;
private static Square[][] gridSquares = new Square[N][N];
private TextField[] gridNumbers = new TextField[N*N];
private Rectangle gridOutline;
Scene scene;
private boolean isWider;
private Rectangle previous = null;
private GridPane setupGrid() {
GridPane gridPane = new GridPane();
gridPane.setAlignment(Pos.CENTER);
double size = scene.getWidth() * GRID_PERCENTAGE / N;
//gridPane.add(setupGridOutline(size), 0, 0, N, N);
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
gridPane = addToGridPane(gridPane, size, i, j);
}
}
return gridPane;
}
private GridPane addToGridPane(GridPane gridPane, double size, int i, int j) {
Square square = setupGridSquare(size);
TextField textField = setupGridTextField(size);
HBox hbox = setupGridHBox(square, textField);
Group group = square.getGroup();
gridSquares[i][j] = square;
gridNumbers[i*N + j] = textField;
gridPane.add(group, j, i);
gridPane.add(hbox, j, i);
square.getRectangle().toFront();
return gridPane;
}
private HBox setupGridHBox(Square square, TextField textField) {
HBox hbox = new HBox();
hbox.addEventHandler(MouseEvent.MOUSE_CLICKED, e -> {
square.getRectangle().setFill(Color.LIGHTBLUE);
if (previous != null) {
previous.setFill(Color.WHITE);
}
previous = square.getRectangle();
});
hbox.getChildren().add(textField);
hbox.setAlignment(Pos.CENTER);
return hbox;
}
private Rectangle setupGridOutline(double size) {
gridOutline = new Rectangle(size*N + N*2.5, size*N + N*2.5, size*N + N*2.5, size*N + N*2.5);
gridOutline.setStroke(Color.BLACK);
gridOutline.setFill(Color.TRANSPARENT);
gridOutline.setStrokeWidth(2);
gridOutline.toBack();
return gridOutline;
}
private Square setupGridSquare(double size) {
Square square = new Square(size);
square.setTopStroke(2.5, Color.BLACK);
square.setBottomStroke(2.5, Color.BLACK);
square.setLeftStroke(2.5, Color.BLACK);
square.setRightStroke(2.5, Color.BLACK);
return square;
}
private TextField setupGridTextField(double size) {
TextField textField = new TextField();
textField.setMinWidth(size/4);
textField.setPrefSize(size*0.5, size*0.5);
textField.setAlignment(Pos.CENTER);
textField.setStyle("-fx-focus-color: transparent;");
textField.setStyle("-fx-text-box-border: transparent;");
return textField;
}
private HBox setupTopHBox() {
HBox topHBox = new HBox();
topHBox.setAlignment(Pos.BOTTOM_CENTER);
topHBox.setSpacing(10);
Button loadGameFileButton = new Button("Load game - file");
Button showMistakesButton = new Button("Show mistakes");
Button loadGameTextInputButton = new Button("Load game - text input");
loadGameFileButton.setPrefWidth(200);
loadGameTextInputButton.setPrefWidth(loadGameFileButton.getPrefWidth());
topHBox.getChildren().addAll(loadGameFileButton, showMistakesButton, loadGameTextInputButton);
return topHBox;
}
private HBox setupBottomHBox() {
HBox bottomHBox = new HBox();
bottomHBox.setSpacing(10);
bottomHBox.setAlignment(Pos.TOP_CENTER);
Button undoButton = new Button("", new ImageView(new Image("file:Images/undo_icon.png")));
Button redoButton = new Button("", new ImageView(new Image("file:Images/redo_icon.png")));
Button clearButton = new Button("Clear");
clearButton.setPrefHeight(36);
clearButton.setPrefWidth(120);
clearButtonClickEvent(clearButton);
bottomHBox.getChildren().addAll(undoButton, clearButton, redoButton);
return bottomHBox;
}
private void clearButtonClickEvent(Button clearButton) {
clearButton.setOnAction(e -> {
for (TextField textField : gridNumbers) {
textField.setText("");
}
});
}
private void stageWidthResizeEvent(Stage stage, GridPane gridPane) {
stage.widthProperty().addListener((obs, oldVal, newVal) -> {
if (newVal.doubleValue() < stage.getHeight()) {
isWider = false;
resizeGrid(gridPane.getLayoutX(), gridPane.getLayoutY(), gridPane.getWidth());
}
});
}
private void stageHeightResizeEvent(Stage stage, GridPane gridPane) {
stage.heightProperty().addListener((obs, oldVal, newVal) -> {
if (newVal.doubleValue() < stage.getWidth()) {
isWider = true;
resizeGrid(gridPane.getLayoutX(), gridPane.getLayoutY(), gridPane.getWidth());
}
});
}
private void setupCages() {
new Cage(new int[][] {{0,0}, {1,0}});
new Cage(new int[][] {{2,0}, {1,1}, {2,1}});
new Cage(new int[][] {{3,0}, {3,1}});
new Cage(new int[][] {{0,1}, {0,2}});
new Cage(new int[][] {{1,2}});
new Cage(new int[][] {{0,3},{1,3}});
new Cage(new int[][] {{2,2},{2,3},{3,2},{3,3}});
}
public void resizeGrid(double gridX, double gridY, double gridWidth) {
double newValue = isWider ? scene.getHeight() : scene.getWidth();
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
Rectangle rectangle = gridSquares[i][j].getRectangle();
rectangle.setWidth(newValue * GRID_PERCENTAGE / N);
rectangle.setHeight(newValue * GRID_PERCENTAGE / N);
gridSquares[i][j].resizeLines(newValue * GRID_PERCENTAGE / N);
}
}
gridOutline.setWidth(newValue * GRID_PERCENTAGE + N*2.5);
gridOutline.setHeight(newValue * GRID_PERCENTAGE + N*2.5);
}
public static Square[][] getGridSquares() {
return gridSquares;
}
@Override
public void start(Stage stage) throws Exception {
VBox vBox = new VBox();
scene = new Scene(vBox,500 + 50*N,500 + 50*N);
vBox.setSpacing(10);
vBox.setPadding(new Insets(20, 20, 20, 20));
vBox.setAlignment(Pos.CENTER);
GridPane gridPane = setupGrid();
setupCages();
HBox topHBox = setupTopHBox();
HBox bottomHBox = setupBottomHBox();
vBox.getChildren().addAll(topHBox, gridPane, bottomHBox);
stageWidthResizeEvent(stage, gridPane);
stageHeightResizeEvent(stage, gridPane);
stage.setMinWidth(600);
stage.setMinHeight(600);
stage.setScene(scene);
stage.setTitle("Mathdoku");
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
\ No newline at end of file
package coursework;
import javafx.scene.Group;
import javafx.scene.paint.Color;
import javafx.scene.shape.Line;
import javafx.scene.shape.Rectangle;
public class Square {
private double size;
private Rectangle rectangle;
private Line top;
private Line right;
private Line bottom;
private Line left;
private Group group = new Group();
public Square(double size) {
this.size = size;
draw();
}
public void draw() {
rectangle = new Rectangle(0, 0, size, size);
rectangle.setFill(Color.WHITE);
rectangle.setStrokeWidth(0);
top = new Line(0, 0, size, 0);
right = new Line(size, 0, size, size);
bottom = new Line(size, size, 0, size);
left = new Line(0, size, 0, 0);
group.getChildren().addAll(rectangle, top, right, bottom, left);
}
public Group getGroup() {
return group;
}
public Rectangle getRectangle() {
return rectangle;
}
public void resizeLines(double newSize) {
top.setEndX(newSize);
right.setStartX(newSize);
right.setEndX(newSize);
right.setEndY(newSize);
bottom.setStartX(newSize);
bottom.setStartY(newSize);
bottom.setEndY(newSize);
left.setStartY(newSize);
}
public void setTopStroke(double width, Color color) {
top.setStrokeWidth(width);
top.setStroke(color);
}
public void setRightStroke(double width, Color color) {
right.setStrokeWidth(width);
right.setStroke(color);
}
public void setBottomStroke(double width, Color color) {
bottom.setStrokeWidth(width);
bottom.setStroke(color);
}
public void setLeftStroke(double width, Color color) {
left.setStrokeWidth(width);
left.setStroke(color);
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment