Advertisement
Guest User

Untitled

a guest
May 17th, 2017
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.49 KB | None | 0 0
  1. package view;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5.  
  6. import controller.CoordInf;
  7. import controller.databaseConnection;
  8. import javafx.event.ActionEvent;
  9. import javafx.event.EventHandler;
  10. import javafx.geometry.Insets;
  11. import javafx.scene.Node;
  12. import javafx.scene.Scene;
  13. import javafx.scene.canvas.Canvas;
  14. import javafx.scene.control.Button;
  15. import javafx.scene.control.Label;
  16. import javafx.scene.control.PasswordField;
  17. import javafx.scene.control.TextField;
  18. import javafx.scene.effect.DropShadow;
  19. import javafx.scene.input.KeyCode;
  20. import javafx.scene.input.KeyEvent;
  21. import javafx.scene.layout.Pane;
  22. import javafx.scene.layout.VBox;
  23. import javafx.scene.paint.Color;
  24. import javafx.scene.shape.Circle;
  25. import javafx.scene.shape.Line;
  26. import javafx.scene.shape.Polyline;
  27. import javafx.scene.text.Text;
  28. import javafx.stage.Stage;
  29. import javafx.stage.WindowEvent;
  30. import model.Coordinate;
  31.  
  32. public class GUI {
  33. Pane pane = new Pane();
  34. Scene scene = new Scene(pane, 1500, 800);
  35.  
  36. // toms stuff
  37. HashMap<String, Polyline> Hex = new HashMap<String, Polyline>();
  38. double RC;
  39. double boxWidth;
  40. double boxHeight;
  41. int lineWidth = 0;
  42. int lineHeight = 0;
  43. int color = 0;
  44. double screenWidth = scene.getWidth();
  45. double screenHeight = scene.getHeight();
  46. int Lines = 11;
  47. float sizeBox = 1.5f;
  48. int StepSize = (int) calculateStepSize(Lines, sizeBox);
  49. boolean devButtonPressed = true;
  50. boolean cityBuilder = false;
  51. boolean roadBuilder = false;
  52. boolean villageBuilder = false;
  53. ArrayList<Line> arrayLines = new ArrayList<>();
  54. databaseConnection Tiles = new databaseConnection("jdbc:mysql://databases.aii.avans.nl/sficken_db", "sficken",
  55. "Ab12345");
  56. ArrayList<String> colorOfTiles = Tiles.select("SELECT * FROM tegel;", "idgrondstofsoort");
  57. String[] colorOfTile = colorOfTiles.toArray(new String[19]);
  58.  
  59. ArrayList<String> xCordsString = Tiles.select("SELECT * FROM tegel;", "x");
  60. String[] StringxCords = xCordsString.toArray(new String[19]);
  61.  
  62. ArrayList<String> yCordsString = Tiles.select("SELECT * FROM tegel;", "y");
  63. String[] StringyCords = yCordsString.toArray(new String[19]);
  64.  
  65. int[] xCords = new int[19];
  66. int[] yCords = new int[19];
  67. boolean townBuilderPressed = true;
  68. boolean isCity = false;
  69. boolean isVillage = false;
  70. ArrayList<Circle> townCircles = new ArrayList<Circle>();
  71. // einde tom's stuff
  72.  
  73.  
  74. public void run(Stage primaryStage) {
  75. try {
  76.  
  77. primaryStage.setTitle("Inloggen");
  78. scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
  79. scoreScene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
  80. loginScene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
  81. registerScene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
  82. menuScene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
  83.  
  84. pane.getStyleClass().add("paneWithPapyrus");
  85.  
  86. // creates a scene using the pane and a size of 1500w and 800h
  87. scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
  88. scoreScene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
  89. scorePane.getStyleClass().add("pane");
  90.  
  91. // makes three buttons that are used to either place a town, road,
  92. // or city
  93.  
  94. Button townButton = new Button("Bouw dorp");
  95. townButton.setPrefSize(100, 100);
  96. townButton.setWrapText(true);
  97. pane.getChildren().add(townButton);
  98. townButton.setTranslateX(1100);
  99. townButton.setTranslateY(600);
  100. townButton.setEffect(dropShadow);
  101. townButton.setOnAction(new EventHandler<ActionEvent>() {
  102.  
  103. @Override
  104. public void handle(ActionEvent event) {
  105. System.out.println("Dorp gebouwt!");
  106. villageBuilder = true;
  107. }
  108. });
  109.  
  110. Button roadButton = new Button("Bouw weg");
  111. roadButton.setPrefSize(100, 100);
  112. roadButton.setWrapText(true);
  113. pane.getChildren().add(roadButton);
  114. roadButton.setTranslateX(1400);
  115. roadButton.setTranslateY(600);
  116. roadButton.setEffect(dropShadow);
  117. roadButton.setOnAction(new EventHandler<ActionEvent>() {
  118.  
  119. @Override
  120. public void handle(ActionEvent event) {
  121. System.out.println("weg gebouwt!");
  122.  
  123. roadBuilder = true;
  124. }
  125. });
  126.  
  127. Button devMode = new Button("Dev Mode");
  128. devMode.setPrefSize(100, 100);
  129. devMode.setWrapText(true);
  130. pane.getChildren().add(devMode);
  131. devMode.setTranslateX(1240);
  132. devMode.setTranslateY(383);
  133. devMode.setOnAction(new EventHandler<ActionEvent>() {
  134.  
  135. @Override
  136. public void handle(ActionEvent event) {
  137. if (devButtonPressed) {
  138. pane.getChildren().addAll(arrayLines);
  139. devButtonPressed = false;
  140. } else {
  141. pane.getChildren().removeAll(arrayLines);
  142. devButtonPressed = true;
  143. }
  144. }
  145. });
  146.  
  147. Button cityButton = new Button("Bouw stad");
  148. cityButton.setPrefSize(100, 100);
  149. cityButton.setWrapText(true);
  150. pane.getChildren().add(cityButton);
  151. cityButton.setTranslateX(1250);
  152. cityButton.setTranslateY(600);
  153. cityButton.setEffect(dropShadow);
  154. cityButton.setOnAction(new EventHandler<ActionEvent>() {
  155.  
  156. @Override
  157. public void handle(ActionEvent event) {
  158. if (townBuilderPressed) {
  159. cityBuilder = true;
  160. int stepSize = -1 * StepSize / 4;
  161. buildTowns(1, 3, stepSize);
  162. buildTowns(1, 4, stepSize);
  163. buildTowns(2, 3, stepSize);
  164. buildTowns(2, 2, stepSize);
  165. buildTowns(2, 5, stepSize);
  166. buildTowns(2, 6, stepSize);
  167. buildTowns(3, 8, stepSize);
  168. buildTowns(3, 7, stepSize);
  169. buildTowns(3, 5, stepSize);
  170. buildTowns(3, 4, stepSize);
  171. buildTowns(3, 2, stepSize);
  172. buildTowns(3, 1, stepSize);
  173. buildTowns(4, 9, stepSize);
  174. buildTowns(4, 7, stepSize);
  175. buildTowns(4, 6, stepSize);
  176. buildTowns(4, 4, stepSize);
  177. buildTowns(4, 3, stepSize);
  178. buildTowns(4, 1, stepSize);
  179. buildTowns(5, 9, stepSize);
  180. buildTowns(5, 8, stepSize);
  181. buildTowns(5, 6, stepSize);
  182. buildTowns(5, 5, stepSize);
  183. buildTowns(5, 3, stepSize);
  184. buildTowns(5, 2, stepSize);
  185. buildTowns(6, 10, stepSize);
  186. buildTowns(6, 8, stepSize);
  187. buildTowns(6, 7, stepSize);
  188. buildTowns(6, 5, stepSize);
  189. buildTowns(6, 4, stepSize);
  190. buildTowns(6, 2, stepSize);
  191. buildTowns(7, 10, stepSize);
  192. buildTowns(7, 9, stepSize);
  193. buildTowns(7, 7, stepSize);
  194. buildTowns(7, 6, stepSize);
  195. buildTowns(7, 4, stepSize);
  196. buildTowns(7, 3, stepSize);
  197. buildTowns(8, 11, stepSize);
  198. buildTowns(8, 9, stepSize);
  199. buildTowns(8, 8, stepSize);
  200. buildTowns(8, 6, stepSize);
  201. buildTowns(8, 5, stepSize);
  202. buildTowns(8, 3, stepSize);
  203. buildTowns(9, 11, stepSize);
  204. buildTowns(9, 10, stepSize);
  205. buildTowns(9, 8, stepSize);
  206. buildTowns(9, 7, stepSize);
  207. buildTowns(9, 5, stepSize);
  208. buildTowns(9, 4, stepSize);
  209. buildTowns(10, 10, stepSize);
  210. buildTowns(10, 9, stepSize);
  211. buildTowns(10, 7, stepSize);
  212. buildTowns(10, 6, stepSize);
  213. buildTowns(11, 9, stepSize);
  214. buildTowns(11, 8, stepSize);
  215. pane.getChildren().addAll(townCircles);
  216. townBuilderPressed = false;
  217. } else {
  218. pane.getChildren().removeAll(townCircles);
  219. townBuilderPressed = true;
  220. }
  221. }
  222. });
  223.  
  224. Text text = new Text("Speler acties");
  225. text.getStyleClass().add("playerActions");
  226. text.setTranslateX(1200);
  227. text.setTranslateY(550);
  228. pane.getChildren().add(text);
  229.  
  230. // tekent, hexen & lijnen
  231. drawField(11, 800, (int) screenHeight, (int) StepSize / 2, 1.3f); //TODO: om functie te laten zien in scrollbar
  232.  
  233. primaryStage.setScene(scene);
  234. primaryStage.show();
  235. primaryStage.setFullScreen(false);
  236. primaryStage.setResizable(false);
  237. } catch (Exception e) {
  238. e.printStackTrace();
  239. }
  240. }
  241.  
  242. Canvas townLayer = new Canvas(300, 250);
  243.  
  244. public void addTradeButton() {
  245. TradeScreen.makeButton(pane);
  246. }
  247.  
  248. public void buildTowns(int x, int y, int offSet) {
  249. townLayer.toFront();
  250. for (int i = 0; i < 1; i++) {
  251. for (int j = 0; j < 1 * 100; j++) {
  252. int radius = StepSize / 4;
  253. int cords[] = selectCoords(x, y);
  254. placeTownCircles(cords[0], (int) ((radius) + correctY(cords[1])) + offSet, radius);
  255. }
  256. }
  257. }
  258.  
  259. public void placeTownCircles(int xCord, int yCord, int radius) {
  260. if (isCity) {
  261.  
  262. } else {
  263. Circle townCircle = new Circle();
  264. townCircle.setCenterX(xCord);
  265. townCircle.setCenterY(yCord);
  266. townCircle.setRadius(radius);
  267. System.out.println();
  268. if (isVillage) {
  269. townCircle.setFill(Color.GOLD);
  270. } else {
  271. townCircle.setFill(Color.WHITE);
  272. }
  273. townCircles.add(townCircle);
  274. }
  275. }
  276.  
  277. public void placeTokens(int xCord, int yCord, int radius, int index) {
  278. String cordTest = xCord + "_" + yCord;
  279. int center = 0;
  280. if(cordTest.equals("6_6")){
  281. index++;
  282. }
  283. if (!(cordTest.equals("6_6"))) {
  284. Label tokenText;
  285. int[] coords = selectCoords(xCord, yCord);
  286. Circle tokenCircle = new Circle(coords[0], correctY(coords[1]), radius);
  287. tokenCircle.setFill(Color.WHITE);
  288. tokenCircle.setStrokeWidth(2f);
  289. tokenCircle.setStroke(Color.BLACK);
  290. pane.getChildren().add(tokenCircle);
  291.  
  292. if (Tiles.select("SELECT * FROM tegel;", "idgetalfiche") != null && index < 18) {
  293. if(Tiles.select("SELECT * FROM getalfiche;", "waarde").get(index).length() <= 1){
  294. center = 4;
  295. }
  296. tokenText = new Label("" + Tiles.select("SELECT * FROM getalfiche;", "waarde").get(index));
  297. tokenText.setTranslateX(coords[0] - radius/2+center);
  298. tokenText.setTranslateY(correctY(coords[1]) - radius/2);
  299. pane.getChildren().add(tokenText);
  300. } else {
  301. index++;
  302. }
  303. }
  304. }
  305.  
  306. public void drawField(int Lines, int width, int height, int offSet, float sizeBox) {
  307. this.sizeBox = sizeBox;
  308. drawLineSystem(Lines, width, height, sizeBox);
  309. int StepSize = (int) calculateStepSize(Lines, sizeBox) + offSet;
  310. int radius = StepSize / 3;
  311. this.StepSize = StepSize;
  312. this.lineWidth = width;
  313. this.lineHeight = height;
  314. this.Lines = Lines;
  315.  
  316. // tekent alle hexagons
  317. for (int i = 0; i < 19; i++) {
  318. xCords[i] = Integer.parseInt(StringxCords[i]);
  319. yCords[i] = Integer.parseInt(StringyCords[i]);
  320.  
  321. // kleur maken
  322. if (colorOfTile[i].equals("H")) {
  323. color = 0;
  324. } else if (colorOfTile[i].equals("W")) {
  325. color = 1;
  326. } else if (colorOfTile[i].equals("G")) {
  327. color = 2;
  328. } else if (colorOfTile[i].equals("E")) {
  329. color = 3;
  330. } else if (colorOfTile[i].equals("B")) {
  331. color = 4;
  332.  
  333. } else {
  334. color = 5;
  335. }
  336. placeHex(xCords[i], yCords[i], StepSize, offSet);
  337. placeTokens(xCords[i], yCords[i], radius, i);
  338. }
  339. }
  340.  
  341. public void placeHex(int x, int y, double size, int offSet) {
  342. int[] coordinates = selectCoords(x, y);
  343. drawhex((float) size, coordinates[0], coordinates[1], offSet);
  344. }
  345.  
  346. ArrayList<Coordinate> lineX = new ArrayList<>();
  347. ArrayList<Coordinate> lineY = new ArrayList<>();
  348.  
  349. public void drawLineSystem(int lines, int width, int height, float sizeBox) {
  350. this.sizeBox = sizeBox;
  351.  
  352. // nodige berekeningen voor de lijnen
  353. boxWidth = width;
  354. double boxBegin = 200;
  355. boxHeight = height;
  356.  
  357. // rechte lijnen
  358. for (int i = 0; i < lines; i++) {
  359. double w = calculateX(lines, i, sizeBox) + boxBegin * 2;
  360. lineX.add(new Coordinate((int) w, 0, (int) w, (int) boxHeight));
  361. }
  362.  
  363. // schuine lijnen (30 graden)
  364. for (int i = 5; i <= (lines * 2) + 4; i += 2) {
  365. float w = (float) (calculateX(lines, i, sizeBox) + boxBegin * 2);
  366. double h = Math.abs(Math.tan(Math.toRadians(30)) * w);
  367. lineY.add(new Coordinate((int) w, 0, 0, (int) h));
  368. }
  369.  
  370. lineX.forEach(coordinate -> {
  371. drawLine(coordinate, 1);
  372. });
  373.  
  374. lineY.forEach(coordinate -> {
  375. drawLine(coordinate, 0);
  376. });
  377. }
  378.  
  379. // de Line class van java beschouwt zijn 0, 0 punt als linksboven
  380. // in plaats van linksonder hierdoor moet ik al mijn y waardes inverteren om
  381. // er iets bruikbaars uit te laten komen
  382. public double correctY(double y) {
  383. return (y + screenHeight) - (y * 2);
  384. }
  385.  
  386. public int[] selectCoords(int x, int y) {
  387. Coordinate yCord = lineY.get(y - 1);
  388. Coordinate xCord = lineX.get(x - 1);
  389. float h = (float) (Math.abs(Math.tan(Math.toRadians(30))) * (yCord.getX() - xCord.getX()));
  390.  
  391. return new int[] { xCord.getX(), (int) h };
  392. }
  393.  
  394. private double calculateX(int lines, int i, float sizeBox) {
  395. return calculateStepSize(lines, sizeBox) * i;
  396. }
  397.  
  398. private double calculateStepSize(int lines, float sizeBox) {
  399. return boxWidth / (lines * sizeBox);
  400. }
  401.  
  402. public void drawLine(Coordinate coordinate, int color) {
  403. Line line = new Line(coordinate.getX(), coordinate.getY() + screenHeight, coordinate.getX2(), correctY(coordinate.getY2()));
  404.  
  405. arrayLines.add(line);
  406. if (color == 1) {
  407. line.setStroke(Color.RED);
  408. } else if (color == 0) {
  409. line.setStroke(Color.BLUE);
  410. } else if (color == 2) {
  411. line.setStroke(Color.WHITE);
  412. }
  413. }
  414.  
  415. double vectors[] = new double[15];
  416.  
  417. public void drawhex(float radius, int x, int y, int offSet) {
  418. float margin = radius * 0.159f;
  419. for (int i = 0; i < 3; i++) {
  420. double HEX_RAD_DELTA = (float) (Math.PI / 3);
  421. int count = 0;
  422. for (double rad = 0; rad < 6; rad += HEX_RAD_DELTA) {
  423. vectors[count] = (float) (Math.sin(rad) * (radius + margin) + (x));
  424. vectors[count + 1] = (float) (Math.cos(rad) * (radius + margin) + (correctY(y)));
  425. count += 2;
  426. }
  427. vectors[12] = vectors[0];
  428. vectors[13] = vectors[1];
  429. }
  430. drawHex();
  431. }
  432.  
  433. public void drawHex() {
  434. Polyline hex = new Polyline();
  435. if (color == 0) {
  436. hex.setFill(Color.DARKGREEN);
  437. } else if (color == 1) {
  438. hex.setFill(Color.GREEN);
  439. } else if (color == 2) {
  440. hex.setFill(Color.YELLOW);
  441. } else if (color == 3) {
  442. hex.setFill(Color.GREY);
  443. } else if (color == 4) {
  444. hex.setFill(Color.DARKORANGE);
  445. } else if (color == 5) {
  446. hex.setFill(Color.LIGHTYELLOW);
  447. }
  448. for (int index = 0; index < 13; index++) {
  449. hex.getPoints().addAll(vectors[index]);
  450. Line lineLastHex = new Line(vectors[0], vectors[1], vectors[10], vectors[11]);
  451. lineLastHex.setStrokeWidth(2.5);
  452. pane.getChildren().add(lineLastHex);
  453. hex.setStroke(Color.BLACK);
  454. hex.setStrokeWidth(1.5);
  455. }
  456. pane.getChildren().add(hex);
  457. }
  458. private void setGlobalEventHandler(Node root, Button buttonToFire) {
  459. root.addEventHandler(KeyEvent.KEY_PRESSED, ev -> {
  460. if (ev.getCode() == KeyCode.ENTER) {
  461. buttonToFire.fire();
  462. ev.consume();
  463. }
  464. });
  465. }
  466. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement