일반적으로 2D 모양은 XY 평면에 그릴 수 있는 기하학적 도형이며 여기에는 선, 직사각형, 원 등이 포함됩니다.
javafx.scene.shape 패키지는 다양한 클래스를 제공하며, 각 클래스는 2차원 기하학적 객체 또는 이에 대한 작업을 나타내거나 정의합니다. Shape라는 클래스는 JavaFX의 모든 2차원 모양의 기본 클래스입니다.
2D 모양 만들기
JavaFX를 사용하여 2D 기하학적 모양을 그리려면 다음을 수행해야 합니다.
-
클래스 인스턴스화 − 해당 클래스를 인스턴스화합니다. 예를 들어, 원을 그리려면 아래와 같이 Circle 클래스를 인스턴스화해야 합니다. -
//Drawing a Circle Circle circle = new Circle();
-
속성 설정 − 해당 클래스의 메서드를 사용하여 모양의 속성을 설정합니다. 예를 들어, 원을 그리려면 중심과 반경이 필요하며 각각 setCenterX(), setCenterY() 및 setRadius() 메서드를 사용하여 설정할 수 있습니다.
//Setting the properties of the circle circle.setCenterX(300.0f); circle.setCenterY(135.0f); circle.setRadius(100.0f);속성 설정
-
그룹에 도형 개체 추가 − 마지막으로 매개변수로 생성된 모양을 그룹 생성자에 −
로 전달합니다.
Group root = new Group(circle);
예
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.shape.Circle;
public class CircleExample extends Application {
public void start(Stage stage) {
//Drawing a Circle
Circle circle = new Circle();
//Setting the properties of the circle
circle.setCenterX(300.0f);
circle.setCenterY(135.0f);
circle.setRadius(100.0f);
//Creating a Group object
Group root = new Group(circle);
//Creating a scene object
Scene scene = new Scene(root, 600, 300);
//Setting title to the Stage
stage.setTitle("Drawing a Circle");
//Adding scene to the stage
stage.setScene(scene);
//Displaying the contents of the stage
stage.show();
}
public static void main(String args[]){
launch(args);
}
} 출력
