Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package view;
- import java.util.ArrayList;
- import java.util.HashMap;
- import controller.CoordInf;
- import controller.databaseConnection;
- import javafx.event.ActionEvent;
- import javafx.event.EventHandler;
- import javafx.geometry.Insets;
- import javafx.scene.Node;
- import javafx.scene.Scene;
- import javafx.scene.canvas.Canvas;
- import javafx.scene.control.Button;
- import javafx.scene.control.Label;
- import javafx.scene.control.PasswordField;
- import javafx.scene.control.TextField;
- import javafx.scene.effect.DropShadow;
- import javafx.scene.input.KeyCode;
- import javafx.scene.input.KeyEvent;
- import javafx.scene.layout.Pane;
- import javafx.scene.layout.VBox;
- import javafx.scene.paint.Color;
- import javafx.scene.shape.Circle;
- import javafx.scene.shape.Line;
- import javafx.scene.shape.Polyline;
- import javafx.scene.text.Text;
- import javafx.stage.Stage;
- import javafx.stage.WindowEvent;
- import model.Coordinate;
- public class GUI {
- Pane pane = new Pane();
- Scene scene = new Scene(pane, 1500, 800);
- // toms stuff
- HashMap<String, Polyline> Hex = new HashMap<String, Polyline>();
- double RC;
- double boxWidth;
- double boxHeight;
- int lineWidth = 0;
- int lineHeight = 0;
- int color = 0;
- double screenWidth = scene.getWidth();
- double screenHeight = scene.getHeight();
- int Lines = 11;
- float sizeBox = 1.5f;
- int StepSize = (int) calculateStepSize(Lines, sizeBox);
- boolean devButtonPressed = true;
- boolean cityBuilder = false;
- boolean roadBuilder = false;
- boolean villageBuilder = false;
- ArrayList<Line> arrayLines = new ArrayList<>();
- databaseConnection Tiles = new databaseConnection("jdbc:mysql://databases.aii.avans.nl/sficken_db", "sficken",
- "Ab12345");
- ArrayList<String> colorOfTiles = Tiles.select("SELECT * FROM tegel;", "idgrondstofsoort");
- String[] colorOfTile = colorOfTiles.toArray(new String[19]);
- ArrayList<String> xCordsString = Tiles.select("SELECT * FROM tegel;", "x");
- String[] StringxCords = xCordsString.toArray(new String[19]);
- ArrayList<String> yCordsString = Tiles.select("SELECT * FROM tegel;", "y");
- String[] StringyCords = yCordsString.toArray(new String[19]);
- int[] xCords = new int[19];
- int[] yCords = new int[19];
- boolean townBuilderPressed = true;
- boolean isCity = false;
- boolean isVillage = false;
- ArrayList<Circle> townCircles = new ArrayList<Circle>();
- // einde tom's stuff
- public void run(Stage primaryStage) {
- try {
- primaryStage.setTitle("Inloggen");
- scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
- scoreScene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
- loginScene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
- registerScene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
- menuScene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
- pane.getStyleClass().add("paneWithPapyrus");
- // creates a scene using the pane and a size of 1500w and 800h
- scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
- scoreScene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
- scorePane.getStyleClass().add("pane");
- // makes three buttons that are used to either place a town, road,
- // or city
- Button townButton = new Button("Bouw dorp");
- townButton.setPrefSize(100, 100);
- townButton.setWrapText(true);
- pane.getChildren().add(townButton);
- townButton.setTranslateX(1100);
- townButton.setTranslateY(600);
- townButton.setEffect(dropShadow);
- townButton.setOnAction(new EventHandler<ActionEvent>() {
- @Override
- public void handle(ActionEvent event) {
- System.out.println("Dorp gebouwt!");
- villageBuilder = true;
- }
- });
- Button roadButton = new Button("Bouw weg");
- roadButton.setPrefSize(100, 100);
- roadButton.setWrapText(true);
- pane.getChildren().add(roadButton);
- roadButton.setTranslateX(1400);
- roadButton.setTranslateY(600);
- roadButton.setEffect(dropShadow);
- roadButton.setOnAction(new EventHandler<ActionEvent>() {
- @Override
- public void handle(ActionEvent event) {
- System.out.println("weg gebouwt!");
- roadBuilder = true;
- }
- });
- Button devMode = new Button("Dev Mode");
- devMode.setPrefSize(100, 100);
- devMode.setWrapText(true);
- pane.getChildren().add(devMode);
- devMode.setTranslateX(1240);
- devMode.setTranslateY(383);
- devMode.setOnAction(new EventHandler<ActionEvent>() {
- @Override
- public void handle(ActionEvent event) {
- if (devButtonPressed) {
- pane.getChildren().addAll(arrayLines);
- devButtonPressed = false;
- } else {
- pane.getChildren().removeAll(arrayLines);
- devButtonPressed = true;
- }
- }
- });
- Button cityButton = new Button("Bouw stad");
- cityButton.setPrefSize(100, 100);
- cityButton.setWrapText(true);
- pane.getChildren().add(cityButton);
- cityButton.setTranslateX(1250);
- cityButton.setTranslateY(600);
- cityButton.setEffect(dropShadow);
- cityButton.setOnAction(new EventHandler<ActionEvent>() {
- @Override
- public void handle(ActionEvent event) {
- if (townBuilderPressed) {
- cityBuilder = true;
- int stepSize = -1 * StepSize / 4;
- buildTowns(1, 3, stepSize);
- buildTowns(1, 4, stepSize);
- buildTowns(2, 3, stepSize);
- buildTowns(2, 2, stepSize);
- buildTowns(2, 5, stepSize);
- buildTowns(2, 6, stepSize);
- buildTowns(3, 8, stepSize);
- buildTowns(3, 7, stepSize);
- buildTowns(3, 5, stepSize);
- buildTowns(3, 4, stepSize);
- buildTowns(3, 2, stepSize);
- buildTowns(3, 1, stepSize);
- buildTowns(4, 9, stepSize);
- buildTowns(4, 7, stepSize);
- buildTowns(4, 6, stepSize);
- buildTowns(4, 4, stepSize);
- buildTowns(4, 3, stepSize);
- buildTowns(4, 1, stepSize);
- buildTowns(5, 9, stepSize);
- buildTowns(5, 8, stepSize);
- buildTowns(5, 6, stepSize);
- buildTowns(5, 5, stepSize);
- buildTowns(5, 3, stepSize);
- buildTowns(5, 2, stepSize);
- buildTowns(6, 10, stepSize);
- buildTowns(6, 8, stepSize);
- buildTowns(6, 7, stepSize);
- buildTowns(6, 5, stepSize);
- buildTowns(6, 4, stepSize);
- buildTowns(6, 2, stepSize);
- buildTowns(7, 10, stepSize);
- buildTowns(7, 9, stepSize);
- buildTowns(7, 7, stepSize);
- buildTowns(7, 6, stepSize);
- buildTowns(7, 4, stepSize);
- buildTowns(7, 3, stepSize);
- buildTowns(8, 11, stepSize);
- buildTowns(8, 9, stepSize);
- buildTowns(8, 8, stepSize);
- buildTowns(8, 6, stepSize);
- buildTowns(8, 5, stepSize);
- buildTowns(8, 3, stepSize);
- buildTowns(9, 11, stepSize);
- buildTowns(9, 10, stepSize);
- buildTowns(9, 8, stepSize);
- buildTowns(9, 7, stepSize);
- buildTowns(9, 5, stepSize);
- buildTowns(9, 4, stepSize);
- buildTowns(10, 10, stepSize);
- buildTowns(10, 9, stepSize);
- buildTowns(10, 7, stepSize);
- buildTowns(10, 6, stepSize);
- buildTowns(11, 9, stepSize);
- buildTowns(11, 8, stepSize);
- pane.getChildren().addAll(townCircles);
- townBuilderPressed = false;
- } else {
- pane.getChildren().removeAll(townCircles);
- townBuilderPressed = true;
- }
- }
- });
- Text text = new Text("Speler acties");
- text.getStyleClass().add("playerActions");
- text.setTranslateX(1200);
- text.setTranslateY(550);
- pane.getChildren().add(text);
- // tekent, hexen & lijnen
- drawField(11, 800, (int) screenHeight, (int) StepSize / 2, 1.3f); //TODO: om functie te laten zien in scrollbar
- primaryStage.setScene(scene);
- primaryStage.show();
- primaryStage.setFullScreen(false);
- primaryStage.setResizable(false);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- Canvas townLayer = new Canvas(300, 250);
- public void addTradeButton() {
- TradeScreen.makeButton(pane);
- }
- public void buildTowns(int x, int y, int offSet) {
- townLayer.toFront();
- for (int i = 0; i < 1; i++) {
- for (int j = 0; j < 1 * 100; j++) {
- int radius = StepSize / 4;
- int cords[] = selectCoords(x, y);
- placeTownCircles(cords[0], (int) ((radius) + correctY(cords[1])) + offSet, radius);
- }
- }
- }
- public void placeTownCircles(int xCord, int yCord, int radius) {
- if (isCity) {
- } else {
- Circle townCircle = new Circle();
- townCircle.setCenterX(xCord);
- townCircle.setCenterY(yCord);
- townCircle.setRadius(radius);
- System.out.println();
- if (isVillage) {
- townCircle.setFill(Color.GOLD);
- } else {
- townCircle.setFill(Color.WHITE);
- }
- townCircles.add(townCircle);
- }
- }
- public void placeTokens(int xCord, int yCord, int radius, int index) {
- String cordTest = xCord + "_" + yCord;
- int center = 0;
- if(cordTest.equals("6_6")){
- index++;
- }
- if (!(cordTest.equals("6_6"))) {
- Label tokenText;
- int[] coords = selectCoords(xCord, yCord);
- Circle tokenCircle = new Circle(coords[0], correctY(coords[1]), radius);
- tokenCircle.setFill(Color.WHITE);
- tokenCircle.setStrokeWidth(2f);
- tokenCircle.setStroke(Color.BLACK);
- pane.getChildren().add(tokenCircle);
- if (Tiles.select("SELECT * FROM tegel;", "idgetalfiche") != null && index < 18) {
- if(Tiles.select("SELECT * FROM getalfiche;", "waarde").get(index).length() <= 1){
- center = 4;
- }
- tokenText = new Label("" + Tiles.select("SELECT * FROM getalfiche;", "waarde").get(index));
- tokenText.setTranslateX(coords[0] - radius/2+center);
- tokenText.setTranslateY(correctY(coords[1]) - radius/2);
- pane.getChildren().add(tokenText);
- } else {
- index++;
- }
- }
- }
- public void drawField(int Lines, int width, int height, int offSet, float sizeBox) {
- this.sizeBox = sizeBox;
- drawLineSystem(Lines, width, height, sizeBox);
- int StepSize = (int) calculateStepSize(Lines, sizeBox) + offSet;
- int radius = StepSize / 3;
- this.StepSize = StepSize;
- this.lineWidth = width;
- this.lineHeight = height;
- this.Lines = Lines;
- // tekent alle hexagons
- for (int i = 0; i < 19; i++) {
- xCords[i] = Integer.parseInt(StringxCords[i]);
- yCords[i] = Integer.parseInt(StringyCords[i]);
- // kleur maken
- if (colorOfTile[i].equals("H")) {
- color = 0;
- } else if (colorOfTile[i].equals("W")) {
- color = 1;
- } else if (colorOfTile[i].equals("G")) {
- color = 2;
- } else if (colorOfTile[i].equals("E")) {
- color = 3;
- } else if (colorOfTile[i].equals("B")) {
- color = 4;
- } else {
- color = 5;
- }
- placeHex(xCords[i], yCords[i], StepSize, offSet);
- placeTokens(xCords[i], yCords[i], radius, i);
- }
- }
- public void placeHex(int x, int y, double size, int offSet) {
- int[] coordinates = selectCoords(x, y);
- drawhex((float) size, coordinates[0], coordinates[1], offSet);
- }
- ArrayList<Coordinate> lineX = new ArrayList<>();
- ArrayList<Coordinate> lineY = new ArrayList<>();
- public void drawLineSystem(int lines, int width, int height, float sizeBox) {
- this.sizeBox = sizeBox;
- // nodige berekeningen voor de lijnen
- boxWidth = width;
- double boxBegin = 200;
- boxHeight = height;
- // rechte lijnen
- for (int i = 0; i < lines; i++) {
- double w = calculateX(lines, i, sizeBox) + boxBegin * 2;
- lineX.add(new Coordinate((int) w, 0, (int) w, (int) boxHeight));
- }
- // schuine lijnen (30 graden)
- for (int i = 5; i <= (lines * 2) + 4; i += 2) {
- float w = (float) (calculateX(lines, i, sizeBox) + boxBegin * 2);
- double h = Math.abs(Math.tan(Math.toRadians(30)) * w);
- lineY.add(new Coordinate((int) w, 0, 0, (int) h));
- }
- lineX.forEach(coordinate -> {
- drawLine(coordinate, 1);
- });
- lineY.forEach(coordinate -> {
- drawLine(coordinate, 0);
- });
- }
- // de Line class van java beschouwt zijn 0, 0 punt als linksboven
- // in plaats van linksonder hierdoor moet ik al mijn y waardes inverteren om
- // er iets bruikbaars uit te laten komen
- public double correctY(double y) {
- return (y + screenHeight) - (y * 2);
- }
- public int[] selectCoords(int x, int y) {
- Coordinate yCord = lineY.get(y - 1);
- Coordinate xCord = lineX.get(x - 1);
- float h = (float) (Math.abs(Math.tan(Math.toRadians(30))) * (yCord.getX() - xCord.getX()));
- return new int[] { xCord.getX(), (int) h };
- }
- private double calculateX(int lines, int i, float sizeBox) {
- return calculateStepSize(lines, sizeBox) * i;
- }
- private double calculateStepSize(int lines, float sizeBox) {
- return boxWidth / (lines * sizeBox);
- }
- public void drawLine(Coordinate coordinate, int color) {
- Line line = new Line(coordinate.getX(), coordinate.getY() + screenHeight, coordinate.getX2(), correctY(coordinate.getY2()));
- arrayLines.add(line);
- if (color == 1) {
- line.setStroke(Color.RED);
- } else if (color == 0) {
- line.setStroke(Color.BLUE);
- } else if (color == 2) {
- line.setStroke(Color.WHITE);
- }
- }
- double vectors[] = new double[15];
- public void drawhex(float radius, int x, int y, int offSet) {
- float margin = radius * 0.159f;
- for (int i = 0; i < 3; i++) {
- double HEX_RAD_DELTA = (float) (Math.PI / 3);
- int count = 0;
- for (double rad = 0; rad < 6; rad += HEX_RAD_DELTA) {
- vectors[count] = (float) (Math.sin(rad) * (radius + margin) + (x));
- vectors[count + 1] = (float) (Math.cos(rad) * (radius + margin) + (correctY(y)));
- count += 2;
- }
- vectors[12] = vectors[0];
- vectors[13] = vectors[1];
- }
- drawHex();
- }
- public void drawHex() {
- Polyline hex = new Polyline();
- if (color == 0) {
- hex.setFill(Color.DARKGREEN);
- } else if (color == 1) {
- hex.setFill(Color.GREEN);
- } else if (color == 2) {
- hex.setFill(Color.YELLOW);
- } else if (color == 3) {
- hex.setFill(Color.GREY);
- } else if (color == 4) {
- hex.setFill(Color.DARKORANGE);
- } else if (color == 5) {
- hex.setFill(Color.LIGHTYELLOW);
- }
- for (int index = 0; index < 13; index++) {
- hex.getPoints().addAll(vectors[index]);
- Line lineLastHex = new Line(vectors[0], vectors[1], vectors[10], vectors[11]);
- lineLastHex.setStrokeWidth(2.5);
- pane.getChildren().add(lineLastHex);
- hex.setStroke(Color.BLACK);
- hex.setStrokeWidth(1.5);
- }
- pane.getChildren().add(hex);
- }
- private void setGlobalEventHandler(Node root, Button buttonToFire) {
- root.addEventHandler(KeyEvent.KEY_PRESSED, ev -> {
- if (ev.getCode() == KeyCode.ENTER) {
- buttonToFire.fire();
- ev.consume();
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement