คุณสมบัติ stroke type ของรูปทรงระบุประเภทของเส้นขอบ สามารถตั้งค่าได้ด้วยเมธอด setStrokeType() ของคลาส javafx.scene.shape.Shape
JavaFX รองรับ 3 ประเภทของ stroke แทนด้วยค่าคงที่ของ Enum StrokeType:
- StrokeType.INSIDE - วาดขอบภายในรูปทรง
- StrokeType.OUTSIDE - วาดขอบภายนอกรูปทรง
- StrokeType.CENTERED - วาดขอบโดยให้ขอบรูปทรงผ่านจุดกึ่งกลางของ stroke
เพื่อตั้งค่าขอบให้รูปทรง ต้องส่งค่าหนึ่งในสามค่านี้เป็นพารามิเตอร์ให้เมธอด setStrokeType()
ตัวอย่าง
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.shape.Polygon;
import javafx.scene.shape.StrokeType;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class StrokeTypeExample extends Application {
public void start(Stage stage) {
Font font = Font.font("verdana", FontWeight.BOLD, FontPosture.REGULAR, 12);
Text label1 = new Text("Original Image");
label1.setFont(font);
label1.setX(250.0);
label1.setY(125.0);
Polygon rhombus1 = new Polygon(300.0, 0.0, 250.0, 50.0, 300.0, 100.0, 350.0, 50.0);
Text label2 = new Text("Stroke Type: Inside");
label2.setFont(font);
label2.setX(25.0);
label2.setY(275.0);
Polygon rhombus2 = new Polygon(100.0, 150.0, 50.0, 200.0, 100.0, 250.0, 150.0, 200.0);
rhombus2.setStroke(Color.BLUE);
rhombus2.setStrokeWidth(7.0);
rhombus2.setStrokeType(StrokeType.INSIDE);
Text label3 = new Text("Stroke Type: Centered");
label3.setFont(font);
label3.setX(220.0);
label3.setY(275.0);
Polygon rhombus3 = new Polygon(300.0, 150.0, 250.0, 200.0, 300.0, 250.0, 350.0, 200.0);
rhombus3.setStroke(Color.BLUE);
rhombus3.setStrokeWidth(7.0);
rhombus3.setStrokeType(StrokeType.CENTERED);
Text label4 = new Text("Stroke Type: Outside");
label4.setFont(font);
label4.setX(430.0);
label4.setY(275.0);
Polygon rhombus4 = new Polygon(490.0, 150.0, 440, 200.0, 490.0, 250.0, 540.0, 200.0);
rhombus4.setStroke(Color.BLUE);
rhombus4.setStrokeWidth(7.0);
rhombus4.setStrokeType(StrokeType.OUTSIDE);
Group root = new Group(label1, label2, label3, label4, rhombus1, rhombus2, rhombus3, rhombus4);
Scene scene = new Scene(root, 595, 310);
stage.setTitle("Stroke Type");
stage.setScene(scene);
stage.show();
}
public static void main(String args[]){
launch(args);
}
}
ผลลัพธ์
