Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Using the program below, write a method that takes in a Graphics object, (x, y)

ID: 3714422 • Letter: U

Question

Using the program below, write a method that takes in a Graphics object, (x, y) coordinates, and a dimension (width=height) and draws a tile of the specified shape at the specified position.

Java Program:

// DrawPattern.java

import javafx.application.Application;

import javafx.scene.Scene;

import javafx.scene.layout.StackPane;

import javafx.scene.paint.Color;

import javafx.scene.shape.Rectangle;

import javafx.stage.Stage;

public class DrawPattern extends Application {

    @Override

    public void start(Stage primaryStage) {

        /**

         * Defining a StackPane to arrange each elements

         */

        StackPane pane = new StackPane();

        //original size of the outer square

        double size = 200;

        /**

         * Creating a Black square of 200*200 size

         */

        Rectangle sq1 = new Rectangle(size, size);

        sq1.setFill(Color.BLACK);

        /**

         * creating another square which fits within the previous square rotated

         * 45 degrees

         */

        size = Math.sqrt(1.0 / 2.0) * size;

        Rectangle sq2 = new Rectangle(size, size);

        sq2.setFill(Color.GREY);

        sq2.setRotate(45);

        /**

         * Defining a rectangle that can fit along the diagonal of the outer

         * square (first one). The length of the diagonal of a square can be

         * calculated as length= sq.root(2) * side length. Subtracting 1/4th of

         * the side length will fit the required rectangle inside the square

         */

        size = Math.sqrt(2.0) * sq1.getHeight() - sq1.getHeight() / 4;

        Rectangle rec1 = new Rectangle(sq1.getWidth() / 4, size);

        rec1.setFill(Color.WHITE);

        rec1.setRotate(45);

        /**

         * creating a copy of the previously created rectangle, this time,

         * rotated -45 degree

         */

        Rectangle rec2 = new Rectangle(sq1.getWidth() / 4, size);

        rec2.setFill(Color.WHITE);

        rec2.setRotate(-45);

        /**

         * Adding each shapes to the stack pane in proper order

         */

        pane.getChildren().add(sq1);

        pane.getChildren().add(rec1);

        pane.getChildren().add(rec2);

        pane.getChildren().add(sq2);

        Scene scene = new Scene(pane);

        primaryStage.setScene(scene);

        primaryStage.show();

    }

    public static void main(String[] args) {

        launch(args);

    }

}

Explanation / Answer

/*The given program uses the javafx library for rendering od 2d shapes. Javafx is a powerful tool which is used for making of 2d and 3d shapes. Making of 3d shapes, is bit more complex then the 2d shapes. 2d shapes are easier to draw and can further be used */

/*Coming to the given question, am not copying the complete code given above. Am making the required method that would be needed by the question */

/*First of all before using any kind of graphics in the program, it is always good practice to make a general graphics class, from which we can always refer to create new specific classes.

Therefore the following would be the mehtod for your question*/

//Making a polygon with given coordinates

//The coordinates are given using two double arrays x_coord and y_coord

public void drawTile(Stage stage, double x_coord, double y_coord){ //Only data required to make the polygon

Pane pane = new Pane();

Polygon tile = new Polygon();

pane.getChildren().add(tile); //Here pane is the Pane defined here

//Now adding the coordinates to the polygon

for(int i=0, i<x_coord.length, i++){

//Here the array iterates for the length of the arrays and all the coordinates to the tile

tile.getPoints().addAll(x_coord[i], y_coord[i]);

}

Scene scene = new Scene(pane);

stage.setScene(scene);

stage.show();

}

/* Frankly, this should do it. By using the coordinates of all the points(can be of any number of coordinates) the desired polygon can be made */

/* If any doubts or queries, let me know. I would try my best to clear them