JavaFX Tutorial - JavaFX Circle Arc








Arc

The following code shows how to draw an arc which is centered around 50,50, has a radius of 25 and extends from the angle 45 to the angle 315 (270 degrees long).

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.shape.Arc;
import javafx.scene.shape.ArcType;
import javafx.stage.Stage;
//from  w w w .  j  av  a 2s .co m
public class Main extends Application {
    public static void main(String[] args) {
        Application.launch(args);
    }
    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("Text Fonts");

        Group g = new Group();
        Scene scene = new Scene(g, 550, 250,Color.web("0x0000FF",1.0));

        Arc arc = new Arc();
        arc.setCenterX(50.0f);
        arc.setCenterY(50.0f);
        arc.setRadiusX(25.0f);
        arc.setRadiusY(25.0f);
        arc.setStartAngle(45.0f);
        arc.setLength(270.0f);
        arc.setType(ArcType.ROUND);
        
        g.getChildren().add(arc);
        
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

The code above generates the following result.

null




Circle

Circle class creates a new circle with the specified radius and center location measured in pixels.

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
//  w  w  w.j  ava  2  s .  co m
public class Main extends Application {
    public static void main(String[] args) {
        Application.launch(args);
    }
    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("Text Fonts");
        Group root = new Group();
        Scene scene = new Scene(root, 550, 250,Color.web("0x0000FF"));

        Circle circle = new Circle();
        circle.setCenterX(100.0f);
        circle.setCenterY(100.0f);
        circle.setRadius(50.0f);
        
        root.getChildren().add(circle);

        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

The code above generates the following result.

null




Example

The following code shows how to use Circle constructor to passin radius and center.

import java.util.List;
//www  .ja va  2  s  .c  om
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;

public class Main extends Application {

    public static void main(String[] args) {
        Application.launch(args);
    }
    
    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("Title");
        
        final Circle circ = new Circle(40, 40, 30);
        final Group root = new Group(circ);
        final Scene scene = new Scene(root, 400, 300);        
              
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

The code above generates the following result.

null

Example 2

Circle with DropShadow

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.effect.DropShadow;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;
/*from   w w w. j  a  v a2 s  . c  om*/
public class Main extends Application {
  public static void main(String[] args) {
    Application.launch(args);
  }

  @Override
  public void start(Stage primaryStage) {
    primaryStage.setTitle("");
    Group root = new Group();
    Scene scene = new Scene(root, 300, 250, Color.WHITE);

    Group g = new Group();

    DropShadow ds1 = new DropShadow();
    ds1.setOffsetY(4.0);

    Circle c = new Circle();
    c.setEffect(ds1);
    c.setCenterX(50.0);
    c.setCenterY(125.0);
    c.setRadius(30.0);
    c.setFill(Color.RED);
    c.setCache(true);


    g.getChildren().add(c);
    

    root.getChildren().add(g);
    primaryStage.setScene(scene);
    primaryStage.show();
  }
}

The getBoundsInParent() method returns the bounding region of a node, such as its width and height.

The getBoundsInParent() calculation for the height and width includes the node's actual dimensions plus any effects, translations, and transformations. For example, a shape that has a drop shadow effect increases its width by including the shadow.

The code above generates the following result.

null