javafx8特性及體系一覽

一、特性一覽

javafx8發佈有一段時間了,今天閒來看看。javafx主要致力於富客戶端開發,以彌補swing的缺陷,主要提供圖形庫與media庫,支持audio,vedio,graphics,animation,3D等,同時採用現代化的css方式支持界面設計。同時又採用XUI方式以XML方式設計UI界面,達到顯示與邏輯的分離。與android這方面確實有點相似性。

javafx體系結構:

javafx特性

  • 1.javafx8內嵌爲java API,與JDK一起打包,javafx運行環境被加入了jre中。
  • 2.FXML與SceneBuilder: FXML是以XML爲基礎的UI界面描述文件,用於構造XUI形式的界面,與android頗爲相似。 SceneBuilder是一種可視化拖拽的FXML生成器,以可視化方式設計UI界面,同時fxml中還可以使用javascript來進行事件的處理。(另一種方式是通過註解在單獨的controller類中進行事件處理。)
  • 3.WebView: WebView是一個webkit瀏覽器組件支持HTML,HTML5, JS,CSS等,javafx8強化了對HTML5的支持,可以支持HTML5的一些新特性如:webWork,WebSocket等。 同時允許java API與JS 相互調用。這與android也頗爲相似。
  • 4.Swing集成:javafx可以與swing組件相互集成,javafx8新增了SwingNode類,可以使swing組件集成到javafx中。
  • 5.內鍵的UI組件Controls與CSS樣式:UI組件可以自由地通過CSS來設計皮膚。 DatePicker and TreeTableView 在javafx8中被添加了進來。同時 CSS Styleable*類已經成爲了開放的API,允許我們自由地通過css定製樣式。
  • 6.Modena theme: javafx8採用了新的主題。如果你希望使用舊主題,可以通過 setUserAgentStylesheet(STYLESHEET_CASPIAN)
  •  7.3D繪圖能力:javafx8新增了3D類庫,如: Shape3D (Box, Cylinder, MeshView, and Sphere subclasses), SubScene, Material, PickResult, LightBase (AmbientLight and PointLight subclasses), and SceneAntialiasing。
  • 8.Canvas API:
  • 9.Printing API:javafx新增了javafx.print包
  • 10.RichText Support富文本支持:javafx強化了富文本的支持。
  • 11.MultiTouch Support:強化了touch支持
  • 12.Hi-DPI支持:強化了顯示效果。
  • 13硬件圖形加速:Hardware-accelerated graphics pipeline:
  • 14.高性能媒體media引擎
  • 15.Self-contained application deployment model自包含部署模式:可以自動提取必須的運行環境和相關依賴,可以使javafx就像本地應用程序一樣運行,且無需帶有臃腫的jre,因爲它會自動提取最小化的運行環境。同時提供安裝嚮導和運行啓動文件,就像本地程序一樣。 

二、體系結構

javafx Sene Graph

javafx採用SceneGraph作爲application的入口及用戶接口,用戶向用戶展示界面,處理用戶交互如input和render渲染。SceneGraph採取tree node的形式組織界面。
每一個node擁有id,style,bound.每個節點還擁有
Effects, such as blurs and shadows
Opacity Transforms Event handlers (such as mouse, key and input method)An application-specific state
scene graph包含
  • Nodes: Shapes (2-D and 3-D), images, media, embedded web browser, text, UI controls, charts, groups, and containers 
  • State: Transforms (positioning and orientation of nodes), visual effects, and other visual state of the content
  •  Effects: Simple objects that change the appearance of scene graph nodes, such as blurs, shadows, and color adjustment
可見scene graph是將layout與ui控件及容器都作爲node節點。

Glass Windowing Toolkit

Glass Windowing Toolkit是一個平臺相關的工具庫,是比較底層的,它負責管理 windows, timers, and surfaces。同時它還負責event queue,這個Even queue不同與Swing總的EDT,javafx中event queue是在javafx application thread中而非在單獨的線程中。
關於線程需要注意幾點:
  • javafx application thread:javafx的主線程。它不同於Swing中的EDT要注意着一點。同時在scene graph中只要子元素子node沒有被掛載到root根節點我們就可以在單獨的線程中繪製node,然後再在javafx application thread中將這些繪製好的子node掛載到root下,這樣就可以提高繪製效率。
  • Prism render thread:渲染線程是一個底層對於程序員透明的一個線程負責渲染,同時優化了再多核處理器下的工作能力,可以執行併發渲染。
  • Media Thread:多媒體線程是一個單獨的後臺線程,通過application thread可以與scene graph同步。
Puls
Puls是一個與Glass Window Toolkit相關的事件調度器,用於同步scene graph中元素的狀態與prism的渲染。
而Glass Window Toolkit負責管理Puls

Media and Imaging

Media:javafx支持的格式MP3,FLV,WAV等。
三個主要組件Media,MediaPlayer, MediaView

Web Component:

javafx WebEngine基於webkit,提供了完整的瀏覽器功能。同時支持:CSS,HTML5.JavaScript,SVG,DOM等
主要能力:
  • 1.衝本地或遠程url加載渲染html頁面
  • 2.歷史查看與前進後退
  • 3.刷新,reload
  • 4.web組件效果
  • 5.編輯HTML內容,執行js腳本,處理events,操作dom
  • 主要類:WebEngine, WebView(WebView是一個node,同時它封裝了WebEngine。)

CSS

javafx中CSS可以應用於任意node節點。運行程序動態異步地改變組件的外觀 javafx內置的樣式以"-fx-"開頭

Control

Control即javafx中的UI控件

Layout

javafx中layout也是作爲scene graph中的Node而存在的,主要有如下幾類: BorderPane HBox,VBox StackPane GridPane FlowPane TilePane(對,你沒看錯,是TilePane而非TitlePane):TilePane使子node以一致性地大小排列 AnchorPane:The AnchorPane class enables developers to create anchor nodes to the top, bottom, left side, or center of the layout.

 2D and 3D

2D包:javafx.scene.tranform,主要基於x-y座標 Translate,Scale,shear,rotate,以及affine – Perform a linear mapping from 2-D/3-D coordinates to other 2-D/3-D coordinates while preserving the 'straight' and 'parallel' properties of the lines. This class should be used with Translate, Scale, Rotate, or Shear transform classes instead of being used directly.

Visual Effect

Visual Effect基於image-pixed。主要類:DropShadowReflectionLighting  


 三、HelloWord解析

 

package helloworld;
 
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
 
public class HelloWorld extends Application {
    
    @Override
    public void start(Stage primaryStage) {
        Button btn = new Button();
        btn.setText("Say 'Hello World'");
        btn.setOnAction(new EventHandler<ActionEvent>() {
 
            @Override
            public void handle(ActionEvent event) {
                System.out.println("Hello World!");
            }
        });
        
        StackPane root = new StackPane();
        root.getChildren().add(btn);

 Scene scene new Scene(root, 300, 250);

        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }
 public static void main(String[] args) {
        launch(args);
    }
}
  •  javafx應用需要集成javafx.application.Application類。並覆蓋其start方法作爲應用入口點。
  • javafx以Stage和Scene組織顯示界面。Stage爲頂級容器,Scene包含所有組件內容的容器
  • Scene包含層次的Nodes節點。並以root節點作爲樹的根
  • main()函數並不是必須的,但是推薦你這麼做。main函數在使用javafx Packging Tool打包成jar文件時不是必須的,但是假如你使用的IDE並不是完全支持javafx打包,或者你與Swing進行集成,那麼main函數則必須了。launch(args)方法用於啓動application

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章