Monday, 26 October 2020

JavaFx: Arc shape

 

Arc is a portion of the curve. Arc class is used to define Arc shape.

 

Arc class provides below constructors to define Arc shape.

 

public Arc()

public Arc(double centerX, double centerY, double radiusX, double radiusY, double startAngle, double length)

centerX: the X coordinate of the center point of the arc

centerY: the Y coordinate of the center point of the arc

radiusX: the overall width (horizontal radius) of the full ellipse of which this arc is a partial section

radiusY: the overall height (vertical radius) of the full ellipse of which this arc is a partial section

startAngle: the starting angle of the arc in degrees

length: the angular extent of the arc in degrees

 

Example

Arc arc1 = new Arc();

Arc arc2 = new Arc(150, 150, 150, 55, 45, 240);

 

ArcType

ArcType is used to specify closure type for Arc objects. There are three possible Arc types. Following table summarizes the arc types.

 

Arc Type

Description

OPEN

The closure type for an open arc with no path segments connecting the two ends of the arc segment.

CHORD

The closure type for an arc closed by drawing a straight line segment from the start of the arc segment to the end of the arc segment.

ROUND

The closure type for an arc closed by drawing straight line segments from the start of the arc segment to the center of the full ellipse and from that point to the end of the arc segment.

 

arc2.setType(ArcType.CHORD);

 

FXML Examples

Example 1: Arc with round arc type.

<Arc fx:id="arc1"  fill="lightsalmon" stroke="black" strokeWidth="5"
        centerX="160"
        centerY="150"
        radiusX="150"
        radiusY="55"
        startAngle="45"
        length="240"
        type="round"/>

  Example 2: Arc with chord arc type.

 

<Arc fx:id="arc2"  fill="lightsalmon" stroke="black" strokeWidth="5"
        centerX="500"
        centerY="150"
        radiusX="150"
        radiusY="55"
        startAngle="45"
        length="240"
        type="chord"/>

 

Example 3: Arc with open arc type.

<Arc fx:id="arc3"  fill="lightsalmon" stroke="black" strokeWidth="5"
        centerX="850"
        centerY="150"
        radiusX="150"
        radiusY="55"
        startAngle="45"
        length="240"
        type="open"/>

 

Find the below working application.

 

arcDemo.fxml

<?import javafx.scene.*?>
<?import javafx.scene.shape.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>

<Group xmlns:fx="http://javafx.com/fxml" fx:controller="com.sample.app.controller.ArcController">

    <Arc fx:id="arc1"  fill="lightsalmon" stroke="black" strokeWidth="5"
        centerX="160"
        centerY="150"
        radiusX="150"
        radiusY="55"
        startAngle="45"
        length="240"
        type="round"/>
        
    <Arc fx:id="arc2"  fill="lightsalmon" stroke="black" strokeWidth="5"
        centerX="500"
        centerY="150"
        radiusX="150"
        radiusY="55"
        startAngle="45"
        length="240"
        type="chord"/>
        
    <Arc fx:id="arc3"  fill="lightsalmon" stroke="black" strokeWidth="5"
        centerX="850"
        centerY="150"
        radiusX="150"
        radiusY="55"
        startAngle="45"
        length="240"
        type="open"/>
        
</Group>

ArcController.java

package com.sample.app.controller;

import java.net.URL;
import java.util.ResourceBundle;

import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.shape.Arc;

public class ArcController implements Initializable {

    @FXML
    private Arc arc1;

    @FXML
    private Arc arc2;

    @FXML
    private Arc arc3;

    @Override
    public void initialize(URL location, ResourceBundle resources) {

    }

}


ArcDemo.java

package com.sample.app;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class ArcDemo  extends Application {

    private static final String FXML_FILE = "/arcDemo.fxml";
    private static final String STAGE_TITLE = "Arc Demo";

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

    }

    @Override
    public void start(Stage primaryStage) throws Exception {

        Parent root = (Parent) FXMLLoader.load(this.getClass().getResource(FXML_FILE));

        Scene scene = new Scene(root, 1000, 400, Color.WHITE);

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

}


Output

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment