JavaFX circle

0

JavaFX circle Tutorial:

JavaFX Circle class draws a circle of given radius.
Two draw a circle we need radius and center coordinates.

Constructors :

  • Circle()Creates an empty instance of Circle.
  • Circle(double radius)Creates a new instance of Circle with a specified radius.
  • Circle(double centerX, double centerY, double radius)Creates a new instance of Circle with a specified position and radius.
  • Circle(double centerX, double centerY, double radius, Paint fill)Creates a new instance of Circle with a specified position, radius and fill.
  • Circle(double radius, Paint fill)Creates a new instance of Circle with a specified radius and fill.

Creating a circle :


Circle circle = new Circle();
circle.setRadius(40.0f);
circle.setCenterX(70.0f);
circle.setCenterY(60.0f);

It Create a circle of radius 40 with center (70,60).

Example of circle :


package javafxtuts;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;

/**
 *
 * @author JavaFXtuts.com
 */
public class Javafxtuts extends Application {
    
    @Override
    public void start(Stage primaryStage) {
         Pane root = new Pane();
         
        //creating circle using empty constructor 
        Circle circle = new Circle();
        //set circle perameter using setter methods
            circle.setRadius(40.0f);
            circle.setCenterX(70.0f);
            circle.setCenterY(60.0f);
        
        // To set outlier color of circle to Black
        circle.setStroke(Color.BLACK);
        // To set interier color of circle to DARKCYAN
        circle.setFill(Color.DARKCYAN);
        //To set the width of the stroke using setStrokeWidth()
        circle.setStrokeWidth(3);
        root.getChildren().addAll(circle);
        Scene scene = new Scene(root, 250, 220);
        
        primaryStage.setTitle("javafxtuts.com");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }
    
}

Output :

Explaination:
  • First create a new instant of the circle and then using setter methods sets the parameter.
  • After That uses setStroke() and setStrokeWidth() methods to set the color and width of the Stroke.
Share.

Leave A Reply