{"id":43189,"date":"2025-06-29T14:41:43","date_gmt":"2025-06-29T14:41:43","guid":{"rendered":"https:\/\/gaviki.com\/blog\/?p=43189"},"modified":"2025-06-29T14:41:44","modified_gmt":"2025-06-29T14:41:44","slug":"objectives-to-learn-how-to-draw-different-shapes-using-gui-components-from-javafx-library","status":"publish","type":"post","link":"https:\/\/gaviki.com\/blog\/objectives-to-learn-how-to-draw-different-shapes-using-gui-components-from-javafx-library\/","title":{"rendered":"Objectives: To learn how to draw different shapes using GUI components from JavaFx Library."},"content":{"rendered":"\n<p>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).<\/p>\n\n\n\n<p><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">The Correct Answer and Explanation is:<\/mark><\/strong><\/p>\n\n\n\n<p>To draw a cube using the <code>Line<\/code> 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\u2019s <code>Line<\/code> class.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Java Code:<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">javaCopyEdit<code>import javafx.application.Application;\nimport javafx.scene.Group;\nimport javafx.scene.Scene;\nimport javafx.scene.paint.Color;\nimport javafx.scene.shape.Line;\nimport javafx.stage.Stage;\n\npublic class CubeDrawing extends Application {\n\n    @Override\n    public void start(Stage stage) {\n        \/\/ Create a Group to hold the lines\n        Group group = new Group();\n\n        \/\/ Set the coordinates for the 8 vertices of the cube\n        double[][] vertices = {\n            {100, 100}, {200, 100}, {200, 200}, {100, 200}, \/\/ Front square\n            {150, 150}, {250, 150}, {250, 250}, {150, 250}  \/\/ Back square\n        };\n\n        \/\/ Draw 12 edges of the cube\n        group.getChildren().add(new Line(vertices[0][0], vertices[0][1], vertices[1][0], vertices[1][1])); \/\/ Front edge 1\n        group.getChildren().add(new Line(vertices[1][0], vertices[1][1], vertices[2][0], vertices[2][1])); \/\/ Front edge 2\n        group.getChildren().add(new Line(vertices[2][0], vertices[2][1], vertices[3][0], vertices[3][1])); \/\/ Front edge 3\n        group.getChildren().add(new Line(vertices[3][0], vertices[3][1], vertices[0][0], vertices[0][1])); \/\/ Front edge 4\n        \n        group.getChildren().add(new Line(vertices[4][0], vertices[4][1], vertices[5][0], vertices[5][1])); \/\/ Back edge 1\n        group.getChildren().add(new Line(vertices[5][0], vertices[5][1], vertices[6][0], vertices[6][1])); \/\/ Back edge 2\n        group.getChildren().add(new Line(vertices[6][0], vertices[6][1], vertices[7][0], vertices[7][1])); \/\/ Back edge 3\n        group.getChildren().add(new Line(vertices[7][0], vertices[7][1], vertices[4][0], vertices[4][1])); \/\/ Back edge 4\n        \n        \/\/ Connect front and back squares to form the cube\n        group.getChildren().add(new Line(vertices[0][0], vertices[0][1], vertices[4][0], vertices[4][1])); \/\/ Vertical edge 1\n        group.getChildren().add(new Line(vertices[1][0], vertices[1][1], vertices[5][0], vertices[5][1])); \/\/ Vertical edge 2\n        group.getChildren().add(new Line(vertices[2][0], vertices[2][1], vertices[6][0], vertices[6][1])); \/\/ Vertical edge 3\n        group.getChildren().add(new Line(vertices[3][0], vertices[3][1], vertices[7][0], vertices[7][1])); \/\/ Vertical edge 4\n\n        \/\/ Set up the scene and stage\n        Scene scene = new Scene(group, 400, 400, Color.WHITE);\n        stage.setTitle(\"Cube Drawing with JavaFX\");\n        stage.setScene(scene);\n        stage.show();\n    }\n\n    public static void main(String[] args) {\n        launch(args);\n    }\n}\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation:<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>JavaFX Setup<\/strong>: The program uses JavaFX for drawing the cube. The main GUI component used is the <code>Line<\/code> class, which allows us to draw a line between two points on the canvas.<\/li>\n\n\n\n<li><strong>Vertices Definition<\/strong>: The cube&#8217;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.<\/li>\n\n\n\n<li><strong>Drawing the Cube<\/strong>:\n<ul class=\"wp-block-list\">\n<li>The cube&#8217;s edges are created by drawing 12 <code>Line<\/code> objects. Each line connects two vertices.<\/li>\n\n\n\n<li>The four lines on the front square connect vertices {0, 1, 2, 3}.<\/li>\n\n\n\n<li>The four lines on the back square connect vertices {4, 5, 6, 7}.<\/li>\n\n\n\n<li>The remaining four lines connect the corresponding vertices of the front and back squares, forming the cube&#8217;s vertical edges.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Scene and Stage<\/strong>: The <code>Group<\/code> object is used to hold all the lines. A <code>Scene<\/code> is created with a white background, and this scene is set on the <code>Stage<\/code>, which is displayed to the user.<\/li>\n\n\n\n<li><strong>Running the Program<\/strong>: When the program is executed, it opens a window showing the 2D representation of a cube, drawn using lines.<\/li>\n<\/ol>\n\n\n\n<p>This approach illustrates how to use JavaFX\u2019s basic graphical components to draw shapes and simulate a 3D object in a 2D space.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"1024\" src=\"https:\/\/gaviki.com\/blog\/wp-content\/uploads\/2025\/06\/learnexams-banner5-435.jpeg\" alt=\"\" class=\"wp-image-43190\" srcset=\"https:\/\/gaviki.com\/blog\/wp-content\/uploads\/2025\/06\/learnexams-banner5-435.jpeg 1024w, https:\/\/gaviki.com\/blog\/wp-content\/uploads\/2025\/06\/learnexams-banner5-435-300x300.jpeg 300w, https:\/\/gaviki.com\/blog\/wp-content\/uploads\/2025\/06\/learnexams-banner5-435-150x150.jpeg 150w, https:\/\/gaviki.com\/blog\/wp-content\/uploads\/2025\/06\/learnexams-banner5-435-768x768.jpeg 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-43189","post","type-post","status-publish","format-standard","hentry","category-quiz-questions"],"_links":{"self":[{"href":"https:\/\/gaviki.com\/blog\/wp-json\/wp\/v2\/posts\/43189","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gaviki.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gaviki.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gaviki.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gaviki.com\/blog\/wp-json\/wp\/v2\/comments?post=43189"}],"version-history":[{"count":1,"href":"https:\/\/gaviki.com\/blog\/wp-json\/wp\/v2\/posts\/43189\/revisions"}],"predecessor-version":[{"id":43191,"href":"https:\/\/gaviki.com\/blog\/wp-json\/wp\/v2\/posts\/43189\/revisions\/43191"}],"wp:attachment":[{"href":"https:\/\/gaviki.com\/blog\/wp-json\/wp\/v2\/media?parent=43189"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gaviki.com\/blog\/wp-json\/wp\/v2\/categories?post=43189"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gaviki.com\/blog\/wp-json\/wp\/v2\/tags?post=43189"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}