Design and Implent an Application That Draws a Circle

JavaFX - 2nd Shapes Circle


A circle is the locus of all points at a fixed distance (radius of circumvolve) from a fixed point (the centre of circle). In other words, a circle is a line forming a closed loop, every bespeak on which is a stock-still distance from a eye point.

A circle is divers by two parameters namely −

  • Eye − It is a point inside the circle. All points on the circumvolve are equidistant (same altitude) from the centre betoken.

  • Radius − The radius is the distance from the center to whatever point on the circumvolve. It is half the diameter.

Circle

In JavaFX, a circumvolve is represented by a form named Circle. This form belongs to the parcel javafx.scene.shape.

By instantiating this grade, you can create a Circle node in JavaFX.

This class has three properties of the double datatype namely −

  • centerX − The x coordinate of the middle of a circle.

  • centerY − The y coordinate of the eye of a circle.

  • radius − The radius of the circumvolve in pixels.

To draw a circle, you need to pass values to these backdrop, either by passing them to the constructor of this grade, in the same guild, at the time of instantiation, as follows −

Circle circle = new Circle(centerx, centery, radius);        

Or, past using their respective setter methods as follows −

setCenterX(value);  setCenterY(value);  setRadius(value);        

Steps to Depict a Circle

Follow the steps given below to depict a Circle in JavaFX.

Step 1: Creating a Class

Create a Java course and inherit the Awarding class of the packet javafx.application and implement the showtime() method of this class as follows.

public class ClassName extends Application {      @Override         public void start(Phase primaryStage) throws Exception {          }     }        

Step two: Creating a Circle

You can create a circle in JavaFX by instantiating the class named Circle which belongs to a packet javafx.scene.shape, instantiate this class as follows.

//Creating a circumvolve object          Circumvolve circumvolve = new Circumvolve();        

Step 3: Setting Properties to the Circle

Specify the ten, y coordinates of the center of the circle and the radius of the circle by setting the properties X, Y, and radius using their respective setter methods as shown in the following lawmaking block.

circle.setCenterX(300.0f);  circle.setCenterY(135.0f);  circumvolve.setRadius(100.0f);        

Step iv: Creating a Group Object

In the start() method, create a group object past instantiating the form named Group, which belongs to the package javafx.scene.

Pass the circumvolve (node) object, created in the previous step, equally a parameter to the constructor of the Group class, in order to add it to the group as follows −

Group root = new Group(circumvolve);        

Footstep 5: Creating a Scene Object

Create a Scene by instantiating the class named Scene which belongs to the package javafx.scene. To this class, pass the Group object (root), created in the previous pace.

In addition to the root object, you can besides pass two double parameters representing height and width of the screen forth with the object of the Group grade as follows.

Scene scene = new Scene(group ,600, 300);        

Step 6: Setting the Championship of the Stage

You tin set the title to the stage using the setTitle() method of the Stage course. The primaryStage is a Stage object which is passed to the start method of the scene class, equally a parameter.

Using the primaryStage object, set the championship of the scene as Sample Application as follows.

primaryStage.setTitle("Sample Application");        

Step 7: Adding Scene to the Stage

You tin add together a Scene object to the phase using the method setScene() of the course named Stage. Add the Scene object prepared in the previous steps using this method as follows.

primaryStage.setScene(scene);        

Step 8: Displaying the Contents of the Stage

Brandish the contents of the scene using the method named evidence() of the Stage class every bit follows.

primaryStage.evidence();        

Footstep 9: Launching the Application

Launch the JavaFX awarding by calling the static method launch() of the Application grade from the main method as follows.

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

Example

Following is a program which generates a circle using JavaFX. Save this code in a file with the proper name CircleExample.coffee.

import javafx.application.Application;  import javafx.scene.Grouping;  import javafx.scene.Scene;  import javafx.phase.Phase;  import javafx.scene.shape.Circumvolve;            public class CircleExample extends Application {     @Override     public void start(Stage stage) {        //Cartoon a Circle        Circle circle = new Circumvolve();                  //Setting the backdrop of the circle        circle.setCenterX(300.0f);        circle.setCenterY(135.0f);        circle.setRadius(100.0f);                  //Creating a Grouping object         Group root = new Group(circumvolve);                  //Creating a scene object        Scene scene = new Scene(root, 600, 300);         //Setting championship to the Stage        stage.setTitle("Drawing a Circle");                  //Adding scene to the stage        stage.setScene(scene);                  //Displaying the contents of the stage        phase.bear witness();    }     public static void primary(String args[]){        launch(args);     }  }        

Compile and execute the saved java file from the command prompt using the following commands.

javac CircleExample.java  java CircleExample        

On executing, the above program generates a javaFx window displaying a circle equally shown beneath.

Drawing Circle

javafx_2d_shapes.htm

Useful Video Courses


Advanced Java Using Eclipse IDE: Learn JavaFX & Databases

Video

Complete Oracle JavaFX Bootcamp! Build Real Projects In 2021

Video

JavaFX Database Management System! Database Design In JavaFX

Video

mcalistertherited.blogspot.com

Source: https://www.tutorialspoint.com/javafx/2dshapes_circle.htm

0 Response to "Design and Implent an Application That Draws a Circle"

ارسال یک نظر

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel