Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Java

การดำเนินการต่างๆ ของออบเจ็กต์ 2D ใน JavaFX

JavaFX รองรับ 3 การดำเนินการบนออบเจ็กต์ 2D ได้แก่ Union, Subtraction และ Intersection

  • Union Operation - รับรูปทรง 2 รูปขึ้นไป คืนพื้นที่ที่ครอบครองโดยรวม
  • Intersection Operation - รับรูปทรง 2 รูปขึ้นไป คืนพื้นที่ทับซ้อนกัน
  • Subtraction Operation - รับรูปทรง 2 รูปขึ้นไป คืนพื้นที่ของรูปแรก ไม่รวมพื้นที่ทับซ้อนกับรูปที่สอง

ตัวอย่าง

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Shape;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;

public class JavaFXOperations extends Application {
    public void start(Stage stage) {
        Font font = Font.font("verdana", FontWeight.BOLD, FontPosture.REGULAR, 12);

        // วาดวงกลมสำหรับ Union
        Circle shape1 = new Circle(65.0f, 135.0f, 50.0f);
        Circle shape2 = new Circle(130.0f, 135.0f, 50.0f);
        // Union operation
        Shape union = Shape.union(shape1, shape2);
        union.setFill(Color.RED);
        Text label1 = new Text("Union Operation");
        label1.setFont(font);
        label1.setX(40);
        label1.setY(230);

        // วาดวงกลมสำหรับ Intersect
        Circle shape3 = new Circle(250.0f, 135.0f, 50.0f);
        Circle shape4 = new Circle(325.0f, 135.0f, 50.0f);
        // Intersect operation
        Shape intersect = Shape.intersect(shape3, shape4);
        intersect.setFill(Color.RED);
        Text label2 = new Text("Intersect Operation");
        label2.setFont(font);
        label2.setX(225);
        label2.setY(230);

        // วาดวงกลมสำหรับ Subtract
        Circle shape5 = new Circle(445.0f, 135.0f, 50.0f);
        Circle shape6 = new Circle(510.0f, 135.0f, 50.0f);
        // Subtract operation
        Shape subtract = Shape.subtract(shape5, shape6);
        subtract.setFill(Color.RED);
        Text label3 = new Text("Subtract Operation");
        label3.setFont(font);
        label3.setX(420);
        label3.setY(230);

        // ตั้งค่า Stage
        Group root = new Group(
            shape1, shape2, shape3, shape4, shape5, shape6,
            union, intersect, subtract, label1, label2, label3
        );
        Scene scene = new Scene(root, 600, 300);
        stage.setTitle("2D Operations Example");
        stage.setScene(scene);
        stage.show();
    }
    public static void main(String args[]){
        launch(args);
    }
}

ผลลัพธ์

การดำเนินการต่างๆ ของออบเจ็กต์ 2D ใน JavaFX