Skip to content Skip to sidebar Skip to footer

Draw a Circle With Characters in Java

JavaFX - second Shapes Circle


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

A circle is defined past two parameters namely −

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

  • Radius − The radius is the distance from the centre to any point on the circle. Information technology is half the diameter.

Circle

In JavaFX, a circle is represented by a class named Circle. This course belongs to the bundle javafx.scene.shape.

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

This class has 3 properties of the double datatype namely −

  • centerX − The ten coordinate of the center of a circle.

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

  • radius − The radius of the circle in pixels.

To draw a circumvolve, you need to pass values to these properties, either by passing them to the constructor of this class, in the same club, 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 beneath to depict a Circle in JavaFX.

Footstep 1: Creating a Class

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

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

Step ii: Creating a Circumvolve

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

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

Footstep 3: Setting Properties to the Circle

Specify the x, y coordinates of the middle of the circle and the radius of the circle by setting the properties X, Y, and radius using their corresponding setter methods as shown in the following code cake.

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

Step four: Creating a Grouping Object

In the beginning() method, create a group object by instantiating the form named Group, which belongs to the parcel javafx.scene.

Laissez passer the circle (node) object, created in the previous step, as a parameter to the constructor of the Group class, in order to add it to the grouping as follows −

Group root = new Group(circle);        

Step 5: Creating a Scene Object

Create a Scene by instantiating the grade named Scene which belongs to the package javafx.scene. To this form, laissez passer the Group object (root), created in the previous step.

In addition to the root object, you can also pass two double parameters representing summit and width of the screen along with the object of the Grouping class as follows.

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

Step half dozen: Setting the Title of the Stage

You can set up the title to the stage using the setTitle() method of the Phase class. The primaryStage is a Stage object which is passed to the start method of the scene class, as a parameter.

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

primaryStage.setTitle("Sample Application");        

Step 7: Calculation Scene to the Stage

You lot tin can add a Scene object to the stage using the method setScene() of the class named Stage. Add together 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 bear witness() of the Stage class as follows.

primaryStage.show();        

Pace 9: Launching the Awarding

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

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

Case

Following is a programme which generates a circumvolve using JavaFX. Save this lawmaking in a file with the proper noun CircleExample.java.

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

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

javac CircleExample.coffee  java CircleExample        

On executing, the above program generates a javaFx window displaying a circle as shown below.

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

martinezhiseens1942.blogspot.com

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

ارسال یک نظر for "Draw a Circle With Characters in Java"