JavaFX: Setting absolute position for circle

In our previous example, we have put the circle in a stack pane.  If you look at the constructors for Circle object, we should be able to set the position of the circle in the scene.

Circle(double centerX, double centerY, double radius, Paint fill)

As was mentioned in an earlier post, we can put the circle in a Pane object and this will allow the absolute position of the circle to take effect.

package javafxapplication12;

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

public class CirclePosition extends Application {

    @Override
    public void start(Stage stage) {

        stage.setTitle("Codecrawl.com");

        Pane pane = new Pane();

        Scene scene = new Scene(pane, 500, 400);
        stage.setScene(scene);

        pane.setStyle("-fx-background-color: lightgray;");

        Circle circle = new Circle();
        circle.setCenterX(120);
        circle.setCenterY(250);
        circle.setRadius(50);
        circle.setFill(Color.CORNFLOWERBLUE);

        pane.getChildren().add(circle);
        stage.show();
    }

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

circleposition

 

Series Navigation<< JavaFX: Creating A Circle
JavaFX: Hyperlink >>