เนื่องจากคลาส javafx.scene.text.Text ใน JavaFX สืบทอดมาจาก Shape class จึงสืบทอดสมาชิกทั้งหมด สามารถปรับ stroke และสีของ text node ได้โดยตั้งค่าให้ stroke, stroke width และ fill properties ที่ Text class สืบทอดมา
- Stroke Width - ระบุความกว้างของเส้นขอบของรูปทรง ตั้งค่าด้วยเมธอด setStrokeWidth() ของ Shape class
- Fill - ระบุสีที่ใช้เติมพื้นที่ภายในรูปทรง ตั้งค่าด้วยเมธอด setFill() ของ Shape class
- Stroke - ระบุสีของขอบรูปทรง ตั้งค่าด้วยเมธอด setStroke() ของ javafx.scene.shape.Shape class
ตัวอย่าง
import java.io.FileNotFoundException;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
public class SettingStroke_Color extends Application {
public void start(Stage stage) throws FileNotFoundException {
// สร้าง Text object
String str = "Welcome to Tutorialspoint";
Text text = new Text(30.0, 80.0, str);
// ตั้งค่าฟอนต์
Font font = Font.font("Brush Script MT", FontWeight.BOLD, FontPosture.REGULAR, 65);
text.setFont(font);
// ตั้งค่าสีข้อความ
text.setFill(Color.BROWN);
// ตั้งค่าความกว้าง stroke
text.setStrokeWidth(2);
// ตั้งค่าสี stroke
text.setStroke(Color.BLUE);
// ตั้งค่า Stage
Group root = new Group(text);
Scene scene = new Scene(root, 595, 150, Color.BEIGE);
stage.setTitle("Stroke And Color");
stage.setScene(scene);
stage.show();
}
public static void main(String args[]){
launch(args);
}
}
ผลลัพธ์
