Draw a Circle on Java
JavaFX - 2nd Shapes Circumvolve
A circle is the locus of all points at a fixed distance (radius of circumvolve) from a fixed indicate (the centre of circle). In other words, a circle is a line forming a airtight loop, every bespeak on which is a fixed altitude from a centre bespeak.
A circumvolve is defined past two parameters namely −
-
Centre − It is a point inside the circle. All points on the circle are equidistant (same altitude) from the center point.
-
Radius − The radius is the distance from the middle to any point on the circumvolve. It is half the bore.
In JavaFX, a circle is represented past a form named Circle. This class belongs to the package javafx.scene.shape.
By instantiating this class, you can create a Circle node in JavaFX.
This class has 3 properties of the double datatype namely −
-
centerX − The x coordinate of the heart of a circle.
-
centerY − The y coordinate of the center of a circle.
-
radius − The radius of the circle in pixels.
To describe a circle, you need to laissez passer values to these properties, either by passing them to the constructor of this course, in the same order, at the time of instantiation, as follows −
Circle circle = new Circle(centerx, centery, radius);
Or, by using their respective setter methods equally follows −
setCenterX(value); setCenterY(value); setRadius(value);
Steps to Draw a Circumvolve
Follow the steps given below to draw a Circumvolve in JavaFX.
Pace 1: Creating a Class
Create a Java form and inherit the Application form of the package javafx.awarding and implement the showtime() method of this form as follows.
public grade ClassName extends Application { @Override public void start(Phase primaryStage) throws Exception { } }
Step 2: Creating a Circle
Yous can create a circumvolve in JavaFX by instantiating the class named Circle which belongs to a package javafx.scene.shape, instantiate this course every bit follows.
//Creating a circle object Circumvolve circle = new Circle();
Stride 3: Setting Backdrop to the Circle
Specify the x, y coordinates of the center of the circle and the radius of the circle past setting the properties 10, Y, and radius using their corresponding setter methods as shown in the following code block.
circumvolve.setCenterX(300.0f); circle.setCenterY(135.0f); circumvolve.setRadius(100.0f);
Step 4: Creating a Group Object
In the commencement() method, create a group object by instantiating the form named Group, which belongs to the bundle javafx.scene.
Laissez passer the circle (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 past instantiating the class named Scene which belongs to the package javafx.scene. To this course, pass the Group object (root), created in the previous step.
In add-on to the root object, you can also laissez passer two double parameters representing tiptop and width of the screen forth with the object of the Group class every bit follows.
Scene scene = new Scene(group ,600, 300);
Stride 6: Setting the Title of the Phase
You tin set up the title to the stage using the setTitle() method of the Stage form. The primaryStage is a Stage object which is passed to the start method of the scene form, equally a parameter.
Using the primaryStage object, set the championship of the scene every bit Sample Awarding equally follows.
primaryStage.setTitle("Sample Application");
Stride 7: Calculation Scene to the Stage
Yous can add together a Scene object to the phase using the method setScene() of the class named Stage. Add the Scene object prepared in the previous steps using this method every bit follows.
primaryStage.setScene(scene);
Footstep 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.show();
Step 9: Launching the Awarding
Launch the JavaFX application by calling the static method launch() of the Awarding course from the main method as follows.
public static void main(Cord args[]){ launch(args); }
Example
Following is a programme which generates a circumvolve using JavaFX. Save this code in a file with the proper name CircleExample.coffee.
import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.phase.Stage; import javafx.scene.shape.Circumvolve; public class CircleExample extends Awarding { @Override public void start(Stage stage) { //Drawing a Circle Circumvolve circle = new Circumvolve(); //Setting the properties of the circle circle.setCenterX(300.0f); circumvolve.setCenterY(135.0f); circumvolve.setRadius(100.0f); //Creating a Grouping object Grouping 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 stage.bear witness(); } public static void main(String args[]){ launch(args); } }
Compile and execute the saved coffee file from the command prompt using the following commands.
javac CircleExample.java coffee CircleExample
On executing, the above program generates a javaFx window displaying a circle as shown below.
javafx_2d_shapes.htm
Useful Video Courses
Video
Video
Video
Source: https://www.tutorialspoint.com/javafx/2dshapes_circle.htm
0 Response to "Draw a Circle on Java"
Post a Comment