Javafx 简明教程
JavaFX - Architecture
JavaFX 是一个软件平台,允许开发者构建各种内容丰富的客户端应用程序,这些应用程序可以在多个平台上保持一致的运行。这是一整套 API,包含丰富的一组用于构建图形丰富的 GUI 应用程序的类和接口。此 API 的一些重要包包含 −
JavaFX is a software platform that allows developers to build various content-rich client applications, which operate consistently across several platforms. It is a complete API with a rich set of classes and interfaces to build GUI applications with rich graphics. Some of the important packages of this API are −
-
javafx.animation − Contains classes to add transition based animations such as fill, fade, rotate, scale and translation, to the JavaFX nodes.
-
javafx.application − Contains a set of classes responsible for the JavaFX application life cycle.
-
javafx.css − Contains classes to add CSS–like styling to JavaFX GUI applications.
-
javafx.event − Contains classes and interfaces to deliver and handle JavaFX events.
-
javafx.geometry − Contains classes to define 2D objects and perform operations on them.
-
javafx.stage − This package holds the top level container classes for JavaFX application.
-
javafx.scene − This package provides classes and interfaces to support the scene graph. In addition, it also provides sub-packages such as canvas, chart, control, effect, image, input, layout, media, paint, shape, text, transform, web, etc. There are several components that support this rich API of JavaFX.
在本章中,让我们了解此 JavaFX 平台的架构设计以及它的各种组件是如何相互关联的。
In this chapter, let us learn the architectural design of this JavaFX platform and how its components are interconnected.
JavaFX Architecture
以下插图显示了 JavaFX API 的架构。在此,您可以看到支持 JavaFX API 的组件。
The following illustration shows the architecture of JavaFX API. Here you can see the components that support JavaFX API.
Scene Graph
在 JavaFX 中,GUI 应用程序使用场景图进行编码。场景图是 GUI 应用程序构建的起始点。它包含被称为节点的 (GUI) 应用程序基础元素。
In JavaFX, the GUI Applications were coded using a Scene Graph. A Scene Graph is the starting point of the construction of the GUI Application. It holds the (GUI) application primitives that are termed as nodes.
节点是视觉/图形对象,它可能包括 −
A node is a visual/graphical object and it may include −
-
*Geometrical (Graphical) objects * − (2D and 3D) such as circle, rectangle, polygon, etc.
-
UI controls − such as Button, Checkbox, Choice box, Text Area, etc.
-
Containers − (layout panes) such as Border Pane, Grid Pane, Flow Pane, etc.
-
Media elements − such as audio, video and image objects.
通常,节点集合组成一个场景图。所有这些节点按层次顺序排列,如下所示。
In general, a collection of nodes makes a scene graph. All these nodes are arranged in a hierarchical order as shown below.
场景图中的每个节点都有一个父节点,且不包含任何父节点的节点被称为 root node 。
Each node in the scene graph has a single parent, and the node which does not contain any parents is known as the root node.
同样,每个节点都必须一个或多个子级,没有子级的节点叫做 leaf node ;有子级的节点叫做 branch node 。
In the same way, every node has one or more children, and the node without children is termed as leaf node; a node with children is termed as a branch node.
一个节点实例只能添加到一个场景图一次。一个场景图的节点可以拥有效果、不透明度、变换、事件处理程序、事件处理程序、应用程序特定状态。
A node instance can be added to a scene graph only once. The nodes of a scene graph can have Effects, Opacity, Transforms, Event Handlers, Event Handlers, Application Specific States.
Prism
Prism 是一个 high performance hardware–accelerated graphical pipeline ,用于在 JavaFX 中渲染图形。它能渲染 2-D 和 3-D 图形。
Prism is a high performance hardware–accelerated graphical pipeline that is used to render the graphics in JavaFX. It can render both 2-D and 3-D graphics.
要渲染图形,Prism 会使用:
To render graphics, a Prism uses −
-
DirectX 9 on Windows XP and Vista.
-
DirectX 11 on Windows 7.
-
OpenGL on Mac and Linux, Embedded Systems.
如果系统上图形硬件支持不足,则 Prism 会使用软件渲染路径处理图形。
In case the hardware support for graphics on the system is not sufficient, then Prism uses the software render path to process the graphics.
如果与受支持的显卡或 GPU 搭配使用,它会提供更流畅的图形。但如果系统不支持显卡,则 Prism 会默认使用软件渲染堆栈(上述两个之一)。
When used with a supported Graphic Card or GPU, it offers smoother graphics. Just in case the system does not support a graphic card, then Prism defaults to the software rendering stack (either of the above two).
GWT (Glass Windowing Toolkit)
顾名思义,GWT 提供管理 Windows、计时器、表面和事件队列的服务。GWT 将 JavaFX 平台连接到本地操作系统。
As the name suggests, GWT provides services to manage Windows, Timers, Surfaces and Event Queues. GWT connects the JavaFX Platform to the Native Operating System.
Quantum Toolkit
它是对 Prism、Glass、媒体引擎和 Web 引擎的底层组件的抽象。它将 Prism 和 GWT 联系在一起,让它们可用于 JavaFX。
It is an abstraction over the low-level components of Prism, Glass, Media Engine, and Web Engine. It ties Prism and GWT together and makes them available to JavaFX.
WebView
使用 JavaFX,你还可以将 HTML 内容嵌入到场景图中。WebView 是 JavaFX 的组件,用于处理此内容。它使用一种称为 Web Kit 的技术,这是一个内部开源网络浏览器引擎。此组件支持 HTML5、CSS、JavaScript、DOM 和 SVG 等不同的 Web 技术。
Using JavaFX, you can also embed HTML content in to a scene graph. WebView is the component of JavaFX which is used to process this content. It uses a technology called Web Kit, which is an internal open-source web browser engine. This component supports different web technologies like HTML5, CSS, JavaScript, DOM and SVG.
使用 WebView,你可以:
Using WebView, you can −
-
Render HTML content from local or remote URL.
-
Support history and provide Back and Forward navigation.
-
Reload the content.
-
Apply effects to the web component.
-
Edit the HTML content.
-
Execute JavaScript commands.
-
Handle events.
通常,你可以使用 WebView 从 Java 控制 Web 内容。
In general, using WebView, you can control web content from Java.
Media Engine
JavaFX media engine 基于一个名为 Streamer 的开源引擎。此媒体引擎支持视频和音频内容的播放。
The JavaFX media engine is based on an open-source engine known as a Streamer. This media engine supports the playback of video and audio content.
JavaFX 媒体引擎为以下文件格式提供的音频支持:
The JavaFX media engine provides support for audio for the following file formats −
Audio |
MP3WAVAIFF |
Video |
FLV |
javafx.scene.media 包含 JavaFX 中提供媒体功能的类和接口。它以三个组件的形式提供,如下所示:
The package javafx.scene.media contains the classes and interfaces to provide media functionality in JavaFX. It is provided in the form of three components, which are −
-
Media Object − This represents a media file
-
Media Player − To play media content.
-
Media View − To display media.