Advertisement
Guest User

Untitled

a guest
Mar 26th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.77 KB | None | 0 0
  1. package ee.taltech.iti0202.geometry;
  2.  
  3. import javafx.application.Application;
  4. import javafx.geometry.Insets;
  5. import javafx.scene.Scene;
  6. import javafx.scene.canvas.Canvas;
  7. import javafx.scene.canvas.GraphicsContext;
  8. import javafx.scene.control.Alert;
  9. import javafx.scene.control.Button;
  10. import javafx.scene.control.TextArea;
  11. import javafx.scene.control.TextField;
  12. import javafx.scene.layout.Pane;
  13. import javafx.scene.paint.Color;
  14. import javafx.scene.shape.Circle;
  15. import javafx.scene.shape.Rectangle;
  16. import javafx.scene.text.Text;
  17. import javafx.stage.Stage;
  18.  
  19. public class Window extends Application {
  20. Stage window;
  21. Button button = new Button("Ok");
  22. Scene scene;
  23. TextArea area = new TextArea();
  24.  
  25.  
  26. public enum Shapes {
  27. CIRCLE, SQUARE, TRIANGLE
  28. }
  29.  
  30. public enum Colours {
  31. BLUE, RED, YELLOW, BLACK, GREEN
  32. }
  33. public static void main(String[] args) {
  34. launch(args);
  35. }
  36.  
  37. @Override
  38. public void start(Stage primaryStage) {
  39. window = primaryStage;
  40. window.setTitle("Drawing application");
  41. Text t = new Text("Fill the gaps with paramets and press OK");
  42.  
  43. TextField shapeQuestion = new TextField();
  44. shapeQuestion.setPromptText("Enter kind of shape");
  45. TextField ColourQuestion = new TextField();
  46. ColourQuestion.setPromptText("Enter colour of Shape");
  47. TextField sizeQuestion = new TextField();
  48. sizeQuestion.setPromptText("Enter size of Shape");
  49. TextField output = new TextField();
  50. TextField changecirclesize = new TextField();
  51. TextField changesquaresize = new TextField();
  52. TextField changetrianglesize = new TextField();
  53.  
  54. Pane layout = new Pane();
  55. layout.setPadding(new Insets(20, 20, 20, 20));
  56. layout.getChildren().add(t);
  57.  
  58. t.setY(20);
  59. t.setX(175);
  60.  
  61. shapeQuestion.setLayoutY(50);
  62. shapeQuestion.setLayoutX(20);
  63. shapeQuestion.setPrefSize(200, 20);
  64.  
  65. ColourQuestion.setLayoutY(90);
  66. ColourQuestion.setLayoutX(20);
  67. ColourQuestion.setPrefSize(200, 20);
  68.  
  69. sizeQuestion.setLayoutY(130);
  70. sizeQuestion.setLayoutX(20);
  71. sizeQuestion.setPrefSize(200, 20);
  72.  
  73. changecirclesize.setLayoutY(450);
  74. changecirclesize.setLayoutX(100);
  75. changecirclesize.setPrefSize(50, 20);
  76.  
  77. changesquaresize.setLayoutY(450);
  78. changesquaresize.setLayoutX(400);
  79. changesquaresize.setPrefSize(50, 20);
  80.  
  81. changetrianglesize.setLayoutX(650);
  82. changetrianglesize.setLayoutY(450);
  83. changetrianglesize.setPrefSize(50, 20);
  84.  
  85. layout.getChildren().addAll(shapeQuestion, ColourQuestion, sizeQuestion);
  86. button.setLayoutY(170);
  87. button.setLayoutX(20);
  88. area.setLayoutX(300);
  89. area.setLayoutY(50);
  90. area.setPrefSize(550, 150);
  91. layout.getChildren().addAll(button, area, changecirclesize, changesquaresize, changetrianglesize);
  92. button.setOnAction(b -> {
  93. if (isInt(sizeQuestion) && isShapeEnumelement(shapeQuestion) && isColourEnumElement(ColourQuestion)) {
  94. String shape = shapeQuestion.getText().toUpperCase();
  95. String colour = ColourQuestion.getText().toUpperCase();
  96. int size = Integer.parseInt(sizeQuestion.getText());
  97. if (isInt1(changecirclesize) && isInt2(changesquaresize) && isInt3(changetrianglesize)) {
  98. int circlesize = Integer.parseInt(changecirclesize.getText());
  99. int squaresize = Integer.parseInt(changesquaresize.getText());
  100. int trianglesize = Integer.parseInt(changetrianglesize.getText());
  101. }
  102.  
  103. switch (shape) {
  104. case "CIRCLE":
  105. if (changecirclesize.getText().trim().isEmpty()) {
  106. System.out.println("no");
  107. }
  108. Circle shape1 = circle(shape, colour, size);
  109. String t1 = information(shape, 0, colour, size);
  110. area.appendText(t1 + "\n");
  111. shape1.setCenterY(325);
  112. shape1.setCenterX(150);
  113. layout.getChildren().add(shape1);
  114. break;
  115. case "SQUARE":
  116. Rectangle shape2 = rectangle(shape, colour, size);
  117. String t2 = information(shape, 4, colour, size);
  118. area.appendText(t2 + "\n");
  119. shape2.setLayoutY(325);
  120. shape2.setLayoutX(400);
  121. layout.getChildren().add(shape2);
  122. break;
  123. case "TRIANGLE":
  124. Canvas shape3 = trinagle(shape, colour, size);
  125. String t3 = information(shape, 3, colour, size);
  126. area.appendText(t3 + "\n");
  127. shape3.setLayoutY(325);
  128. shape3.setLayoutX(650);
  129. layout.getChildren().add(shape3);
  130. break;
  131. }
  132. } else {
  133. Alert alert = new Alert(Alert.AlertType.INFORMATION);
  134. alert.setTitle("Error");
  135. alert.setHeaderText(null);
  136. alert.setContentText("Given invalid paramets!");
  137. alert.show();
  138. }
  139.  
  140. }
  141. );
  142.  
  143. scene = new Scene(layout, 900, 500);
  144. window.setScene(scene);
  145. window.show();
  146.  
  147.  
  148. }
  149.  
  150. private boolean isInt(TextField usersizeinput) {
  151. try {
  152. int size = Integer.parseInt(usersizeinput.getText());
  153. return true;
  154. }catch (NumberFormatException e) {
  155. return false;
  156. }
  157. }
  158.  
  159. private boolean isInt1(TextField circlesize) {
  160. try {
  161. int size = Integer.parseInt(circlesize.getText());
  162. return true;
  163. }catch (NumberFormatException e) {
  164. return false;
  165. }
  166. }
  167.  
  168. private boolean isInt2(TextField rec) {
  169. try {
  170. int size = Integer.parseInt(rec.getText());
  171. return true;
  172. }catch (NumberFormatException e) {
  173. return false;
  174. }
  175. }
  176.  
  177. private boolean isInt3(TextField trinagl) {
  178. try {
  179. int size = Integer.parseInt(trinagl.getText());
  180. return true;
  181. }catch (NumberFormatException e) {
  182. return false;
  183. }
  184. }
  185.  
  186. private boolean isShapeEnumelement(TextField usershapeinput) {
  187. String shapeupper = usershapeinput.getText().toUpperCase();
  188. for (Shapes shape : Shapes.values()) {
  189. if (shape.name().equals(shapeupper)) {
  190. return true;
  191. }
  192. }
  193. return false;
  194. }
  195.  
  196. private boolean isColourEnumElement(TextField usercolourinput) {
  197. String colourupper = usercolourinput.getText().toUpperCase();
  198. for (Colours shapecolour : Colours.values()) {
  199. if (shapecolour.name().equals(colourupper)) {
  200. return true;
  201. }
  202. }
  203. return false;
  204. }
  205.  
  206. private Circle circle(String shape, String color, int size) {
  207. Circle circle = new Circle();
  208. circle.setRadius(size);
  209. circle.setStroke(Color.BLACK);
  210. circle.setFill(Color.valueOf(color));
  211. return circle;
  212. }
  213.  
  214. private Rectangle rectangle(String shape, String color, int size) {
  215. Rectangle rectangle = new Rectangle();
  216. rectangle.setHeight(size);
  217. rectangle.setWidth(size);
  218. rectangle.setStroke(Color.BLACK);
  219. rectangle.setFill(Color.valueOf(color));
  220. return rectangle;
  221. }
  222.  
  223. private Canvas trinagle(String shape, String color, int size) {
  224. Canvas canvas = new Canvas(300, 300);
  225. GraphicsContext gc = canvas.getGraphicsContext2D();
  226. gc.clearRect(0, 0, 350, 350);
  227.  
  228. gc.beginPath();
  229. gc.moveTo(size * 0.5, 0);
  230. gc.lineTo(size, size);
  231. gc.lineTo(0, size);
  232. gc.lineTo(size * 0.5, 0);
  233. gc.setFill(Color.valueOf(color));
  234. gc.setStroke(Color.valueOf(color));
  235. gc.stroke();
  236. return canvas;
  237. }
  238. private void changesize() {
  239.  
  240. }
  241.  
  242. private String information(String shape, int angles, String color, int size) {
  243. Text shapeinfo = new Text();
  244. String info = String.format("Shape: %s, Number of angles: %d, Color: %s, Size: %d", shape, angles, color, size);
  245. shapeinfo.setText(info);
  246. return info;
  247. }
  248. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement