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

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