Dưới đây là các hình học học bạn có thể vẽ bằng JavaFX:
- Line - Đường thẳng nối hai điểm. Lớp javafx.scene.shape.Line đại diện cho đường thẳng trong mặt phẳng XY.
- Rectangle - Hình chữ nhật bốn cạnh, hai cặp cạnh song song và đối diện, các góc nội tiếp là góc vuông. Lớp Rectangle đại diện cho hình chữ nhật.
- Circle - Đường tròn khép kín, mọi điểm cách đều tâm. Lớp Circle đại diện cho đường tròn.
- Ellipse - Ellipse được xác định bởi hai điểm gọi là tiêu điểm. Tổng khoảng cách từ bất kỳ điểm nào trên ellipse đến hai tiêu điểm là hằng số. Lớp Ellipse đại diện cho ellipse.
- Polygon - Hình khép kín tạo bởi các đoạn thẳng đồng phẳng nối đuôi nhau. Lớp Polygon đại diện cho polygon.
- Polyline - Giống polygon nhưng không khép kín ở cuối. Hoặc đường liên tục gồm một hoặc nhiều đoạn thẳng. Lớp Polyline đại diện cho polyline.
- Cubic Curve - Đường cong Bezier tham số bậc 3 trong mặt phẳng XY. Lớp CubicCurve đại diện cho cubic curve.
- QuadCurve - Đường cong Bezier tham số bậc 2. Lớp QuadCurve đại diện cho quad curve.
- Arc - Một phần của đường cong. Lớp Arc đại diện cho arc.
Để tạo hình dạng mong muốn, bạn cần:
- Khởi tạo lớp tương ứng.
- Thiết lập các thuộc tính của nó.
- Thêm đối tượng đã tạo vào Group.
Ví dụ
Ví dụ JavaFX sau minh họa việc tạo tất cả các hình 2D có sẵn:
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.shape.Arc;
import javafx.scene.shape.ArcType;
import javafx.scene.shape.Circle;
import javafx.scene.shape.CubicCurve;
import javafx.scene.shape.Ellipse;
import javafx.scene.shape.Line;
import javafx.scene.shape.Polygon;
import javafx.scene.shape.Polyline;
import javafx.scene.shape.QuadCurve;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
public class JavaFXShapes extends Application {
public void start(Stage stage) {
Font font = Font.font("verdana", FontWeight.BOLD, FontPosture.REGULAR, 12);
Text cirText = new Text("Circle"); cirText.setFont(font); cirText.setX(50); cirText.setY(130);
Text rectText = new Text("Rectangle"); rectText.setFont(font); rectText.setX(170); rectText.setY(130);
Text ellipseText = new Text("Ellipse"); ellipseText.setFont(font); ellipseText.setX(310); ellipseText.setY(130);
Text polyText = new Text("Polygon"); polyText.setFont(font); polyText.setX(425); polyText.setY(130);
Text lineText = new Text("Line"); lineText.setFont(font); lineText.setX(530); lineText.setY(130);
Text polyLineText = new Text("Poly Line"); polyLineText.setFont(font); polyLineText.setX(40); polyLineText.setY(260);
Text cubicCurveText = new Text("Cubic Curve"); cubicCurveText.setFont(font); cubicCurveText.setX(140); cubicCurveText.setY(260);
Text quadCurveText = new Text("Quad Curve"); quadCurveText.setFont(font); quadCurveText.setX(340); quadCurveText.setY(260);
Text arcText = new Text("Arc"); arcText.setFont(font); arcText.setX(490); arcText.setY(260);
// Vẽ Circle
Circle circle = new Circle(75.0f, 65.0f, 40.0f );
// Vẽ Rectangle
Rectangle rect = new Rectangle(150, 30, 100, 65);
// Vẽ Ellipse
Ellipse ellipse = new Ellipse(330, 60, 60, 35);
// Vẽ Polygon
Polygon poly = new Polygon(410, 60, 430, 30, 470, 30, 490, 60, 470, 100, 430, 100 );
// Vẽ Line
Line line = new Line(540, 30, 540, 90); line.setStrokeWidth(5.0);
// Vẽ Polyline
Polyline polyLine = new Polyline(25, 210, 100, 210, 50, 180, 50, 230); polyLine.setStrokeWidth(5.0);
// Vẽ CubicCurve
CubicCurve cubicCurve = new CubicCurve(150.0, 210.0, 200.0, 70.0, 200.0, 290.0, 270.0, 210.0);
// Vẽ QuadCurve
QuadCurve quadCurve = new QuadCurve(400.0, 200.0, 440.0, 250.0, 330.0, 170.0);
// Vẽ Arc
Arc arc = new Arc(490, 240, 50, 80, 30, 70); arc.setType(ArcType.ROUND);
// Thiết lập Stage
Group root = new Group(
circle, ellipse, rect, poly, line,
polyLine, cubicCurve, quadCurve, arc,
cirText, rectText, ellipseText, polyText, lineText,
polyLineText, cubicCurveText, quadCurveText, arcText);
Scene scene = new Scene(root, 600, 300);
stage.setTitle("2D shapes Example");
stage.setScene(scene);
stage.show();
}
public static void main(String args[]){
launch(args);
}
}
Kết quả
