Monday, 26 October 2020

JavaFX: Working with Circle

 

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

 

Circle class provides following constructors to define circles.

 

public Circle()

public Circle(double radius)

public Circle(double radius, Paint fill)

public Circle(double centerX, double centerY, double radius)

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

 

Example

Circle c1 = new Circle();

Circle c2 = new Circle(3);

Circle c3 = new Circle(3, Paint.valueOf("lightgreen"));

Circle c4 = new Circle(5, 6, 3);

Circle c5 = new Circle(5, 6, 3, Paint.valueOf("lightgreen"));

 

FXML to create circle instance

<Circle fx:id="circle1" centerX="50" centerY="120" radius="30" fill="lightgreen" stroke="black" strokeWidth="5"/>

 

circleDemo.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.CircleController">

	<Circle fx:id="circle1" centerX="50" centerY="120" radius="30" fill="lightgreen" stroke="black" strokeWidth="5"/>
	
	<Circle fx:id="circle2" centerX="150" centerY="120" radius="30" fill="pink" stroke="black" strokeWidth="5"/>
	
	<Circle fx:id="circle3" centerX="250" centerY="120" radius="30" fill="red" stroke="black" strokeWidth="5"/>
	
	<Circle fx:id="circle4" centerX="350" centerY="120" radius="30" fill="lightblue" stroke="black" strokeWidth="5"/>
	
	<Circle fx:id="circle5" centerX="450" centerY="120" radius="30" fill="lightgray" stroke="black" strokeWidth="5"/>
	
	
</Group>

 

CircleController.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.Circle;

public class CircleController implements Initializable {

	@FXML
	private Circle circle1;

	@FXML
	private Circle circle2;

	@FXML
	private Circle circle3;

	@FXML
	private Circle circle4;

	@FXML
	private Circle circle5;

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

	}

}

 

CircleDemo.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 CircleDemo  extends Application {

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

	}

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

		Parent root = (Parent) FXMLLoader.load(CircleDemo.class.getResource("/circleDemo.fxml"));

		Scene scene = new Scene(root, 600, 300, Color.WHITE);

		primaryStage.setTitle("Circle Demo");
		primaryStage.setScene(scene);
		primaryStage.show();
	}

}

 

Output



  

Previous                                                    Next                                                    Home

No comments:

Post a Comment