Vẽ text trên đồ họa Scene của JavaFX

Discussion in 'Bài viết hướng dẫn JavaFX' started by CodeJava_to, 21/7/15.

  1. CodeJava_to

    CodeJava_to New Member

    Xin chào ban quản trị và mọi thành viên congdongjava.

    Hôm nay mình xin phép viết bài hướng dẫn viết text lên Scene trong JavaFX. Mình muốn các bạn có nhu cầu học javafx có thể dùng nó để tham khảo.

    // import các gói javafx cần thiết
    import java.util.Random;
    import javafx.application.Application;
    import javafx.scene.Group;
    import javafx.scene.Scene;
    import javafx.scene.paint.Color;
    import javafx.scene.text.Text;
    import javafx.stage.Stage;

    public class 
    DrawingText extends Application{
        public static 
    void main(String[] args){

    // Ghi đè phương thức start của lớp Application
    public void start(Stage primaryStage){
    primaryStage.setTitle("Drawing Text");
    Group root = new Group();
    Scene scene = new Scene(root300250Color.WHITE);// Thêm root vào Scene
    Random rand = new Random(System.currentTimeMillis());
    int i 01000i++){
    int x rand.nextInt((int) scene.getWidth());
    int y rand.nextInt((int) scene.getHeight());
    int red rand.nextInt(255);
    int green rand.nextInt(255);
    int blue rand.nextInt(255);

    Text text = new Text(xy"Cong dong Java Viet Nam");

    int rot rand.nextInt(360);
    text.setFill(Color.rgb(redgreenblue.99));// Tô màu cho text
    text.setRotate(rot);// Xoay text đi rot độ
    root.getChildren().add(text);// Thêm text vào root

    primaryStage.setScene(scene);// Thêm scene vào Stage
    Bất cứ ai có góp ý gì xin để lại bên dưới! Cảm ơn đã đọc!
    JackV likes this.
  2. Joe

    Joe Moderator


    good to see someone "dare" to work with JFX. However, to complete your learning curve I gives you 2 essential hints:
    1) One of the JAVA basics is an Extension does not need to invoke itself
    public class DrawingText extends Application// <<< extension of Application
    public static void main(String[] args){
    // Application.launch(args); <<< redundant and superfluous. causing more works for JVM
    launch(args); // << this is the way how an INHERITANCE works!
    2) Be aware that JFX threads work independently from JAVA thread. That means a JAVA thread cannot launch an Application and its components (such as View, WebEngine, etc.). Example WebEngine
    WebEngine objects must be created 
    and accessed solely from the JavaFX Application threadThis rule also applies to any DOM and JavaScript objects obtained from the WebEngine object.
    If you want to work with JFX it's essential that you master the basics of JAVA and the NEW JFX features. And because JFX is now a part of JDK (since JDK7), it's more important to understand the basics of JAVA Concurrency, Parallelism and 55 new features of JDK8.
    Anyway, well done.
    CodeJava_to likes this.
  3. CodeJava_to

    CodeJava_to New Member


    Thank you so much for your hints, I am just studying JavaFX and want to share what I learned!

Chia sẻ trang này