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

CodeJava_to

New Member
28/7/12
10
1
3
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.

PHP:
// 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){
        Application.launch(args);
    }

// Ghi đè phương thức start của lớp Application
    @Override
    public void start(Stage primaryStage){
        primaryStage.setTitle("Drawing Text");
        Group root = new Group();
        Scene scene = new Scene(root, 300, 250, Color.WHITE);// Thêm root vào Scene
        Random rand = new Random(System.currentTimeMillis());
        for(int i = 0; i < 1000; i++){
            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(x, y, "Cong dong Java Viet Nam");

            int rot = rand.nextInt(360);
            text.setFill(Color.rgb(red, green, blue, .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
        primaryStage.show();
    }
}
Bất cứ ai có góp ý gì xin để lại bên dưới! Cảm ơn đã đọc!
 
  • Like
Reactions: JackV

Joe

Thành viên VIP
21/1/13
2,701
1,246
113
Code_Java_to,

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
PHP:
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
PHP:
...
Threading
WebEngine objects must be created and accessed solely from the JavaFX Application thread. This 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.
 
Sửa lần cuối:
  • Like
Reactions: CodeJava_to

CodeJava_to

New Member
28/7/12
10
1
3
Joe,

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