Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ee.taltech.iti0202.geometry;
- import javafx.application.Application;
- import javafx.geometry.Insets;
- import javafx.scene.Scene;
- import javafx.scene.canvas.Canvas;
- import javafx.scene.canvas.GraphicsContext;
- import javafx.scene.control.Alert;
- import javafx.scene.control.Button;
- import javafx.scene.control.TextArea;
- import javafx.scene.control.TextField;
- import javafx.scene.layout.Pane;
- import javafx.scene.paint.Color;
- import javafx.scene.shape.Circle;
- import javafx.scene.shape.Rectangle;
- import javafx.scene.text.Text;
- import javafx.stage.Stage;
- public class Window extends Application {
- Stage window;
- Button button = new Button("Ok");
- Scene scene;
- TextArea area = new TextArea();
- public enum Shapes {
- CIRCLE, SQUARE, TRIANGLE
- }
- public enum Colours {
- BLUE, RED, YELLOW, BLACK, GREEN
- }
- public static void main(String[] args) {
- launch(args);
- }
- @Override
- public void start(Stage primaryStage) {
- window = primaryStage;
- window.setTitle("Drawing application");
- Text t = new Text("Fill the gaps with paramets and press OK");
- TextField shapeQuestion = new TextField();
- shapeQuestion.setPromptText("Enter kind of shape");
- TextField ColourQuestion = new TextField();
- ColourQuestion.setPromptText("Enter colour of Shape");
- TextField sizeQuestion = new TextField();
- sizeQuestion.setPromptText("Enter size of Shape");
- TextField output = new TextField();
- TextField changecirclesize = new TextField();
- TextField changesquaresize = new TextField();
- TextField changetrianglesize = new TextField();
- Pane layout = new Pane();
- layout.setPadding(new Insets(20, 20, 20, 20));
- layout.getChildren().add(t);
- t.setY(20);
- t.setX(175);
- shapeQuestion.setLayoutY(50);
- shapeQuestion.setLayoutX(20);
- shapeQuestion.setPrefSize(200, 20);
- ColourQuestion.setLayoutY(90);
- ColourQuestion.setLayoutX(20);
- ColourQuestion.setPrefSize(200, 20);
- sizeQuestion.setLayoutY(130);
- sizeQuestion.setLayoutX(20);
- sizeQuestion.setPrefSize(200, 20);
- changecirclesize.setLayoutY(450);
- changecirclesize.setLayoutX(100);
- changecirclesize.setPrefSize(50, 20);
- changesquaresize.setLayoutY(450);
- changesquaresize.setLayoutX(400);
- changesquaresize.setPrefSize(50, 20);
- changetrianglesize.setLayoutX(650);
- changetrianglesize.setLayoutY(450);
- changetrianglesize.setPrefSize(50, 20);
- layout.getChildren().addAll(shapeQuestion, ColourQuestion, sizeQuestion);
- button.setLayoutY(170);
- button.setLayoutX(20);
- area.setLayoutX(300);
- area.setLayoutY(50);
- area.setPrefSize(550, 150);
- layout.getChildren().addAll(button, area, changecirclesize, changesquaresize, changetrianglesize);
- button.setOnAction(b -> {
- if (isInt(sizeQuestion) && isShapeEnumelement(shapeQuestion) && isColourEnumElement(ColourQuestion)) {
- String shape = shapeQuestion.getText().toUpperCase();
- String colour = ColourQuestion.getText().toUpperCase();
- int size = Integer.parseInt(sizeQuestion.getText());
- if (isInt1(changecirclesize) && isInt2(changesquaresize) && isInt3(changetrianglesize)) {
- int circlesize = Integer.parseInt(changecirclesize.getText());
- int squaresize = Integer.parseInt(changesquaresize.getText());
- int trianglesize = Integer.parseInt(changetrianglesize.getText());
- }
- switch (shape) {
- case "CIRCLE":
- if (changecirclesize.getText().trim().isEmpty()) {
- System.out.println("no");
- }
- Circle shape1 = circle(shape, colour, size);
- String t1 = information(shape, 0, colour, size);
- area.appendText(t1 + "\n");
- shape1.setCenterY(325);
- shape1.setCenterX(150);
- layout.getChildren().add(shape1);
- break;
- case "SQUARE":
- Rectangle shape2 = rectangle(shape, colour, size);
- String t2 = information(shape, 4, colour, size);
- area.appendText(t2 + "\n");
- shape2.setLayoutY(325);
- shape2.setLayoutX(400);
- layout.getChildren().add(shape2);
- break;
- case "TRIANGLE":
- Canvas shape3 = trinagle(shape, colour, size);
- String t3 = information(shape, 3, colour, size);
- area.appendText(t3 + "\n");
- shape3.setLayoutY(325);
- shape3.setLayoutX(650);
- layout.getChildren().add(shape3);
- break;
- }
- } else {
- Alert alert = new Alert(Alert.AlertType.INFORMATION);
- alert.setTitle("Error");
- alert.setHeaderText(null);
- alert.setContentText("Given invalid paramets!");
- alert.show();
- }
- }
- );
- scene = new Scene(layout, 900, 500);
- window.setScene(scene);
- window.show();
- }
- private boolean isInt(TextField usersizeinput) {
- try {
- int size = Integer.parseInt(usersizeinput.getText());
- return true;
- }catch (NumberFormatException e) {
- return false;
- }
- }
- private boolean isInt1(TextField circlesize) {
- try {
- int size = Integer.parseInt(circlesize.getText());
- return true;
- }catch (NumberFormatException e) {
- return false;
- }
- }
- private boolean isInt2(TextField rec) {
- try {
- int size = Integer.parseInt(rec.getText());
- return true;
- }catch (NumberFormatException e) {
- return false;
- }
- }
- private boolean isInt3(TextField trinagl) {
- try {
- int size = Integer.parseInt(trinagl.getText());
- return true;
- }catch (NumberFormatException e) {
- return false;
- }
- }
- private boolean isShapeEnumelement(TextField usershapeinput) {
- String shapeupper = usershapeinput.getText().toUpperCase();
- for (Shapes shape : Shapes.values()) {
- if (shape.name().equals(shapeupper)) {
- return true;
- }
- }
- return false;
- }
- private boolean isColourEnumElement(TextField usercolourinput) {
- String colourupper = usercolourinput.getText().toUpperCase();
- for (Colours shapecolour : Colours.values()) {
- if (shapecolour.name().equals(colourupper)) {
- return true;
- }
- }
- return false;
- }
- private Circle circle(String shape, String color, int size) {
- Circle circle = new Circle();
- circle.setRadius(size);
- circle.setStroke(Color.BLACK);
- circle.setFill(Color.valueOf(color));
- return circle;
- }
- private Rectangle rectangle(String shape, String color, int size) {
- Rectangle rectangle = new Rectangle();
- rectangle.setHeight(size);
- rectangle.setWidth(size);
- rectangle.setStroke(Color.BLACK);
- rectangle.setFill(Color.valueOf(color));
- return rectangle;
- }
- private Canvas trinagle(String shape, String color, int size) {
- Canvas canvas = new Canvas(300, 300);
- GraphicsContext gc = canvas.getGraphicsContext2D();
- gc.clearRect(0, 0, 350, 350);
- gc.beginPath();
- gc.moveTo(size * 0.5, 0);
- gc.lineTo(size, size);
- gc.lineTo(0, size);
- gc.lineTo(size * 0.5, 0);
- gc.setFill(Color.valueOf(color));
- gc.setStroke(Color.valueOf(color));
- gc.stroke();
- return canvas;
- }
- private void changesize() {
- }
- private String information(String shape, int angles, String color, int size) {
- Text shapeinfo = new Text();
- String info = String.format("Shape: %s, Number of angles: %d, Color: %s, Size: %d", shape, angles, color, size);
- shapeinfo.setText(info);
- return info;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement