Objectives: To learn how to draw different shapes using GUI components from JavaFx Library. Problem 1: Using Line Class of GUI, write a program to draw the Cube as shown in the diagram below (use javaFx not applet).

The Correct Answer and Explanation is:

To draw a cube using the Line class from JavaFX, you can represent the edges of the cube as individual lines. A cube has 12 edges, which we will simulate by connecting lines in a 3D space, but we can draw it in a 2D projection. Below is the code that draws a cube using JavaFX’s Line class.

Java Code:

javaCopyEditimport javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.shape.Line;
import javafx.stage.Stage;

public class CubeDrawing extends Application {

    @Override
    public void start(Stage stage) {
        // Create a Group to hold the lines
        Group group = new Group();

        // Set the coordinates for the 8 vertices of the cube
        double[][] vertices = {
            {100, 100}, {200, 100}, {200, 200}, {100, 200}, // Front square
            {150, 150}, {250, 150}, {250, 250}, {150, 250}  // Back square
        };

        // Draw 12 edges of the cube
        group.getChildren().add(new Line(vertices[0][0], vertices[0][1], vertices[1][0], vertices[1][1])); // Front edge 1
        group.getChildren().add(new Line(vertices[1][0], vertices[1][1], vertices[2][0], vertices[2][1])); // Front edge 2
        group.getChildren().add(new Line(vertices[2][0], vertices[2][1], vertices[3][0], vertices[3][1])); // Front edge 3
        group.getChildren().add(new Line(vertices[3][0], vertices[3][1], vertices[0][0], vertices[0][1])); // Front edge 4
        
        group.getChildren().add(new Line(vertices[4][0], vertices[4][1], vertices[5][0], vertices[5][1])); // Back edge 1
        group.getChildren().add(new Line(vertices[5][0], vertices[5][1], vertices[6][0], vertices[6][1])); // Back edge 2
        group.getChildren().add(new Line(vertices[6][0], vertices[6][1], vertices[7][0], vertices[7][1])); // Back edge 3
        group.getChildren().add(new Line(vertices[7][0], vertices[7][1], vertices[4][0], vertices[4][1])); // Back edge 4
        
        // Connect front and back squares to form the cube
        group.getChildren().add(new Line(vertices[0][0], vertices[0][1], vertices[4][0], vertices[4][1])); // Vertical edge 1
        group.getChildren().add(new Line(vertices[1][0], vertices[1][1], vertices[5][0], vertices[5][1])); // Vertical edge 2
        group.getChildren().add(new Line(vertices[2][0], vertices[2][1], vertices[6][0], vertices[6][1])); // Vertical edge 3
        group.getChildren().add(new Line(vertices[3][0], vertices[3][1], vertices[7][0], vertices[7][1])); // Vertical edge 4

        // Set up the scene and stage
        Scene scene = new Scene(group, 400, 400, Color.WHITE);
        stage.setTitle("Cube Drawing with JavaFX");
        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

Explanation:

  1. JavaFX Setup: The program uses JavaFX for drawing the cube. The main GUI component used is the Line class, which allows us to draw a line between two points on the canvas.
  2. Vertices Definition: The cube’s vertices are defined in a 2D array, where each pair of numbers corresponds to the coordinates (x, y) of a vertex. The cube has two squares: one for the front and one for the back. Each square has four vertices.
  3. Drawing the Cube:
    • The cube’s edges are created by drawing 12 Line objects. Each line connects two vertices.
    • The four lines on the front square connect vertices {0, 1, 2, 3}.
    • The four lines on the back square connect vertices {4, 5, 6, 7}.
    • The remaining four lines connect the corresponding vertices of the front and back squares, forming the cube’s vertical edges.
  4. Scene and Stage: The Group object is used to hold all the lines. A Scene is created with a white background, and this scene is set on the Stage, which is displayed to the user.
  5. Running the Program: When the program is executed, it opens a window showing the 2D representation of a cube, drawn using lines.

This approach illustrates how to use JavaFX’s basic graphical components to draw shapes and simulate a 3D object in a 2D space.

By admin

Leave a Reply

Your email address will not be published. Required fields are marked *