Draw a Circle in Javafx
JavaFX - 2D Shapes Circle
A circumvolve is the locus of all points at a fixed distance (radius of circle) from a fixed point (the centre of circle). In other words, a circle is a line forming a closed loop, every point on which is a fixed distance from a centre signal.
A circle is defined past two parameters namely −
-
Middle − It is a point inside the circle. All points on the circle are equidistant (same distance) from the centre point.
-
Radius − The radius is the altitude from the centre to any bespeak on the circumvolve. It is half the diameter.
In JavaFX, a circle is represented by a class named Circle. This class belongs to the package javafx.scene.shape.
By instantiating this form, yous tin 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 center of a circumvolve.
-
radius − The radius of the circle in pixels.
To draw a circle, you lot demand to pass values to these properties, either past passing them to the constructor of this class, in the same order, at the fourth dimension of instantiation, equally follows −
Circle circle = new Circumvolve(centerx, centery, radius);
Or, by using their corresponding setter methods as follows −
setCenterX(value); setCenterY(value); setRadius(value);
Steps to Draw a Circle
Follow the steps given below to depict a Circle in JavaFX.
Stride ane: Creating a Form
Create a Java form and inherit the Application form of the package javafx.application and implement the start() method of this class as follows.
public class ClassName extends Awarding { @Override public void starting time(Stage primaryStage) throws Exception { } } Pace 2: Creating a Circle
You can create a circle in JavaFX by instantiating the class named Circumvolve which belongs to a package javafx.scene.shape, instantiate this course equally follows.
//Creating a circle object Circumvolve circumvolve = new Circle();
Step three: Setting Backdrop to the Circumvolve
Specify the x, 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 4: Creating a Group Object
In the first() method, create a group object past instantiating the class 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 Grouping form, in order to add together it to the grouping as follows −
Group root = new Group(circumvolve);
Step 5: Creating a Scene Object
Create a Scene by instantiating the form named Scene which belongs to the package javafx.scene. To this class, pass the Grouping object (root), created in the previous pace.
In improver to the root object, you lot tin can as well pass two double parameters representing height and width of the screen forth with the object of the Grouping class as follows.
Scene scene = new Scene(group ,600, 300);
Step 6: Setting the Championship of the Stage
Y'all can set 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 as Sample Awarding as follows.
primaryStage.setTitle("Sample Application"); Step 7: Adding Scene to the Stage
Y'all tin add a Scene object to the phase using the method setScene() of the form named Stage. Add the Scene object prepared in the previous steps using this method as follows.
primaryStage.setScene(scene);
Step viii: Displaying the Contents of the Stage
Display the contents of the scene using the method named prove() of the Stage course as follows.
primaryStage.testify();
Pace 9: Launching the Application
Launch the JavaFX application past calling the static method launch() of the Application form from the main method as follows.
public static void main(String args[]){ launch(args); } Case
Post-obit is a program which generates a circumvolve using JavaFX. Save this lawmaking in a file with the name CircleExample.coffee.
import javafx.awarding.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.stage.Stage; import javafx.scene.shape.Circle; public grade CircleExample extends Application { @Override public void start(Phase stage) { //Drawing a Circle Circle circumvolve = new Circle(); //Setting the properties of the circumvolve circle.setCenterX(300.0f); circumvolve.setCenterY(135.0f); circle.setRadius(100.0f); //Creating a Group object Grouping root = new Grouping(circle); //Creating a scene object Scene scene = new Scene(root, 600, 300); //Setting championship to the Phase phase.setTitle("Cartoon a Circle"); //Adding scene to the stage stage.setScene(scene); //Displaying the contents of the stage stage.testify(); } public static void main(Cord args[]){ launch(args); } } Compile and execute the saved java file from the command prompt using the post-obit commands.
javac CircleExample.java java CircleExample
On executing, the above plan generates a javaFx window displaying a circle every bit shown beneath.
javafx_2d_shapes.htm
Useful Video Courses
Video
Video
Video
cunninghamhimpeas.blogspot.com
Source: https://www.tutorialspoint.com/javafx/2dshapes_circle.htm
0 Response to "Draw a Circle in Javafx"
Post a Comment