案例代碼
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import javafx.scene.shape.LineTo;
import javafx.scene.shape.MoveTo;
import javafx.scene.shape.Path;
import javafx.stage.Stage;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
public class CreatePathGUI extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
Group root = new Group();
Scene scene = new Scene(root,500,400);
Button button = new Button("開始路徑描述");
Path path = new Path();
AtomicReference<AtomicInteger> i = new AtomicReference<>(new AtomicInteger());
button.addEventFilter(MouseEvent.MOUSE_CLICKED, event->{
if((i.get().get()) < 490){
path.getElements().add(new MoveTo(i.get().get() +1,100.0f));
path.getElements().add(new LineTo(i.get().get() +20,100.0f));
path.setStroke(Color.ROSYBROWN);
i.getAndSet(new AtomicInteger(i.get().get() + 20));
}
});
root.getChildren().add(button);
root.getChildren().add(path);
primaryStage.setScene(scene);
primaryStage.setTitle("JavaFX 路徑");
primaryStage.getIcons().add(new Image(getClass().getResourceAsStream("/images/note.png")));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}