Skip to content Skip to sidebar Skip to footer

Draw a Circle in Javafx Using Processing Import Library

JavaFX - 2D Shapes Circle


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

A circumvolve is divers by ii parameters namely −

  • Centre − It is a betoken within the circle. All points on the circumvolve are equidistant (aforementioned distance) from the center point.

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

Circle

In JavaFX, a circle is represented past a form named Circumvolve. This grade belongs to the package javafx.scene.shape.

Past instantiating this class, you can create a Circle node in JavaFX.

This class has 3 backdrop of the double datatype namely −

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

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

  • radius − The radius of the circle in pixels.

To draw a circle, y'all need to pass values to these properties, either by passing them to the constructor of this class, in the aforementioned order, at the fourth dimension of instantiation, as follows −

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

Or, by using their respective setter methods as follows −

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

Steps to Depict a Circle

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

Pace 1: Creating a Course

Create a Java form and inherit the Application grade of the package javafx.application and implement the outset() method of this class equally follows.

public class ClassName extends Application {      @Override         public void outset(Stage primaryStage) throws Exception {          }     }        

Pace 2: Creating a Circumvolve

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

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

Step 3: Setting Properties to the Circle

Specify the 10, y coordinates of the center of the circumvolve and the radius of the circumvolve by setting the properties Ten, Y, and radius using their respective setter methods as shown in the following code block.

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

Step 4: Creating a Group Object

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

Pass the circle (node) object, created in the previous step, as a parameter to the constructor of the Group form, in order to add information technology to the group every bit follows −

Group root = new Grouping(circle);        

Step five: Creating a Scene Object

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

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

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

Step 6: Setting the Title of the Phase

Y'all tin can set the title to the stage using the setTitle() method of the Stage class. The primaryStage is a Stage object which is passed to the commencement method of the scene grade, as a parameter.

Using the primaryStage object, set the title of the scene as Sample Application every bit follows.

primaryStage.setTitle("Sample Awarding");        

Step 7: Adding Scene to the Stage

You tin can add a Scene object to the stage using the method setScene() of the class named Stage. Add the Scene object prepared in the previous steps using this method equally follows.

primaryStage.setScene(scene);        

Step 8: Displaying the Contents of the Stage

Display the contents of the scene using the method named prove() of the Stage course as follows.

primaryStage.bear witness();        

Footstep 9: Launching the Application

Launch the JavaFX awarding past calling the static method launch() of the Application class from the principal method as follows.

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

Example

Following is a plan which generates a circumvolve using JavaFX. Save this code in a file with the proper name CircleExample.java.

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

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

javac CircleExample.coffee  java CircleExample        

On executing, the above programme generates a javaFx window displaying a circle every bit 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

dangelotrepen.blogspot.com

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

Post a Comment for "Draw a Circle in Javafx Using Processing Import Library"