Bạn có thể thêm hiệu ứng cho bất kỳ đối tượng node nào trong JavaFX bằng phương thức setEffect(). Phương thức này nhận một đối tượng của lớp Effect và áp dụng nó cho node hiện tại.
Lớp javafx.scene.effect.GaussianBlur đại diện cho hiệu ứng mờ sử dụng kernel tích chập Gaussian. Để thêm hiệu ứng mờ cho text node:
- Khởi tạo lớp Text với tọa độ x, y và chuỗi văn bản.
- Thiết lập các thuộc tính mong muốn như font, stroke, v.v.
- Tạo hiệu ứng mờ bằng cách khởi tạo lớp GaussianBlur.
- Áp dụng hiệu ứng cho text node bằng phương thức setEffect().
- Thêm text node vào đối tượng Group.
Ví dụ minh họa
import java.io.FileNotFoundException;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.effect.GaussianBlur;
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 TextBlurEffect extends Application {
@Override
public void start(Stage stage) throws FileNotFoundException {
// Tạo đối tượng text
String str = "Welcome to Tutorialspoint";
Text text = new Text(30.0, 80.0, str);
// Thiết lập font
Font font = Font.font("Brush Script MT", FontWeight.BOLD, FontPosture.REGULAR, 65);
text.setFont(font);
// Thiết lập màu chữ
text.setFill(Color.BROWN);
// Thiết lập viền
text.setStrokeWidth(2);
text.setStroke(Color.BLUE);
// Áp dụng hiệu ứng mờ
GaussianBlur blur = new GaussianBlur();
text.setEffect(blur);
// Thiết lập Stage
Group root = new Group(text);
Scene scene = new Scene(root, 595, 150, Color.BEIGE);
stage.setTitle("Hiệu ứng Blur");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Kết quả đầu ra
