javafx官方文档学习之二Scene体系学习一

个人博文小站:http://www.xby1993.net,文章更新以博文小站为主,通常与oschina同步发布

原创文章,转载请注明出处,做者。
css

Package javafx.scene

Provides the core set of base classes for the JavaFX Scene Graph API.html

See: Descriptionjava

主要有Scene顶级容器,Node节点基类,Parent分支节点基类,Group分支节点类,.节点渲染类Camera,鼠标光标类:Cursor,SnapShot类及其参数结果类。同时因为采用建造者模式,故而有对应的Builder类。

Package javafx.scene Description

Provides the core set of base classes for the JavaFX Scene Graph API. A scene graph is a tree-like data structure, where each item in the tree has zero or one parent and zero or more children.

The two primary classes in this package are:

  • Scene – Defines the scene to be rendered. It contains a fill variable(即fill属性,可用setFill()方法进行设置) that specifies the background of the scene, width andheight variables that specify the size of the scene, and a content sequence that contains a list of "root" Nodes to be rendered onto the scene. This sequence of Nodes is the scene graph for this Scene. A Scene is rendered onto a Stage, which is the top-level container for JavaFX content.

  • Node – Abstract base class for all nodes in the scene graph. Each node is either a "leaf" node with no child nodes or a "branch" node with zero or more child nodes. Each node in the tree has zero or one parent. Only a single node within each tree in the scene graph will have no parent, which is often referred to as the "root" node. There may be several trees in the scene graph. Some trees may be part of a Scene, in which case they are eligible to be displayed. Other trees might not be part of anyScene.

Branch nodes are of type Parent or subclasses thereof.

Leaf nodes are classes such as RectangleTextImageViewMediaView, or other such leaf classes which cannot have children.

A node may occur at most once anywhere in the scene graph. Specifically, a node must appear no more than once in the children list of a Parent or as the clip of a Node. See the Node class for more details on these restrictions.


public class Example extends Application {

    @Override public void start(Stage stage) {
        
        Group root = new Group();
        Scene scene = new Scene(root, 200, 150);
        scene.setFill(Color.LIGHTGRAY);

        Circle circle = new Circle(60, 40, 30, Color.GREEN);
        
        Text text = new Text(10, 90, "JavaFX Scene");
        text.setFill(Color.DARKRED);
        
        Font font = new Font(20);
        text.setFont(font);
        
        root.getChildren().add(circle);
        root.getChildren().add(text);
        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) {
        Application.launch(args);
    }
}


1. Node基类

Base class for scene graph nodes. A scene graph is a set of tree data structures 

javafx.scene.Node是这个树体系的基类。

继承体系:

  • java.lang.Object

  • javafx.scene.Node

Each item in the scene graph is called a Node. Branch nodes are of type Parent, whose concrete subclasses are GroupRegion, and Control, or subclasses thereof.

Leaf nodes are classes such as RectangleTextImageViewMediaViewor other such leaf classes which cannot have children. 

如下规则是为了防止树体系无限循环交错的规则:

A node may occur at most once anywhere in the scene graph. Specifically, a node must appear no more than once in all of the following: as the root node of a Scene, the children ObservableList of a Parent, or as the clip of a Node.

The scene graph must not have cycles. A cycle would exist if a node is an ancestor of itself in the tree, considering the Group content ObservableList, Parent children ObservableList, and Node clip relationships mentioned above.

If a program adds a child node to a Parent (including Group, Region, etc) and that node is already a child of a different Parent or the root of a Scene, the node is automatically (and silently) removed from its former parent. If a program attempts to modify the scene graph in any other way that violates the above rules, an exception is thrown, the modification attempt is ignored and the scene graph is restored to its previous state.

总之就是一个节点只能在树体系中最多出现一次。不能交错添加节点


关于线程注意事项:Node objects may be constructed and modified on any thread as long they are not yet attached to a Scene. An application must attach nodes to a Scene, and modify nodes that are already attached to a Scene, on the JavaFX Application Thread.


关于节点标识ID:

String ID

Each node in the scene graph can be given a unique id. This id is much like the "id" attribute of an HTML tag in that it is up to the designer and developer to ensure that the id is unique within the scene graph. A convenience function calledlookup(String) can be used to find a node with a unique id within the scene graph, or within a subtree of the scene graph. The id can also be used identify nodes for applying styles; see the CSS section below.


节点变换:

Transformations

Any Node can have transformations applied to it. These include translation, rotation, scaling, or shearing.

translation rotation scaling shearing


边界矩形

Bounding Rectangles

 设置变量boundsInLocal  boundsInParent   layoutBounds 


节点样式:

CSS

The Node class contains idstyleClass, and style variables that are used in styling this node from CSS. The id and styleClass variables are used in CSS style sheets to identify nodes to which styles should be applied. The style variable contains style properties and values that are applied directly to this node.

For further information about CSS and how to apply CSS styles to nodes, see the CSS Reference Guide.


2 Parent分支节点基类与分支节点Group. Region,Control

Parent:
public abstract class Parent extends Node

The base class for all nodes that have children in the scene graph.

This class handles all hierarchical scene graph operations, including adding/removing child nodes, marking branches dirty for layout and rendering, picking, bounds calculations, and executing the layout pass on each pulse.

There are three direct concrete Parent subclasses

  • Group effects and transforms to be applied to a collection of child nodes.

  • Region class for nodes that can be styled with CSS and layout children.

  • base Control class for high-level skinnable nodes designed for user interaction.(Control节点主要用户可让用户来操做的节点,如按钮,文本框,能够与用户交互,用户可操做的节点。同时因为Control实现了Skinnable接口,故而能够轻松地设置皮肤,定制外观)


Group(Group通常做为根节点root)

@DefaultProperty(value="children")
public class Groupextends Parent

Group node contains an ObservableList of children that are rendered in order whenever this node is rendered.

Group will take on the collective bounds of its children and is not directly resizable.

Any transform, effect, or state applied to a Group will be applied to all children of that group. Such transforms and effects will NOT be included in this Group's layout bounds, however if transforms and effects are set directly on children of this Group, those will be included in this Group's layout bounds.

By default, a Group will "auto-size" its managed resizable children to their preferred sizes during the layout pass to ensure that Regions and Controls are sized properly as their state changes. If an application needs to disable this auto-sizing behavior, then it should set autoSizeChildren to false and understand that if the preferred size of the children change, they will not automatically resize (so buyer beware!).

Group Example:

import javafx.scene.*;
import javafx.scene.paint.*;
import javafx.scene.shape.*;
import java.lang.Math;

Group g = new Group();
for (int i = 0; i < 5; i++) {
    Rectangle r = new Rectangle();
    r.setY(i * 20);
    r.setWidth(100);
    r.setHeight(10);
    r.setFill(Color.RED);
    g.getChildren().add(r);
}



Control:

Class Control至关于Layout布局器



(Control节点主要用户可让用户来操做的节点,如按钮,文本框,能够与用户交互,用户可操做的节点。同时因为Control实现了Skinnable接口,故而能够轻松地设置皮肤,定制外观)

关于Control节点的焦点转移focusTraversable

Most controls have their focusTraversable property set to true by default, however read-only controls such as Label andProgressIndicator, and some controls that are containers ScrollPane and ToolBar do not. 


Class Region至关于Layout布局器


  • public class Regionextends Parent

    A Region is an area of the screen that can contain other nodes and be styled using CSS.

    It can have multiple backgrounds under its contents and multiple borders around its content. By default it's a rectangle with possible rounded corners, depending on borders. It can be made into any shape by specifying the shape. It is designed to support as much of the CSS3 specification for backgrounds and borders as is relevant to JavaFX. The full specification is available at css3-background.

    By default a Region inherits the layout behavior of its superclass, Parent, which means that it will resize any resizable child nodes to their preferred size, but will not reposition them. If an application needs more specific layout behavior, then it should use one of the Region subclasses: StackPaneHBoxVBoxTilePaneFlowPaneBorderPaneGridPane, or AnchorPane.

    To implement more custom layout, a Region subclass must override computePrefWidthcomputePrefHeight, and layoutChildrenNote thatlayoutChildren is called automatically by the scene graph while executing a top-down layout pass and it should not be invoked directly by the region subclass.

    Region subclasses which layout their children will position nodes by setting layoutX/layoutY and do not altertranslateX/translateY, which are reserved for adjustments and animation.



javafx.scene.web

Class WebView



extends Parent

WebView is a Node that manages a WebEngine and displays its content. The associated WebEngine is created automatically at construction time and cannot be changed afterwards. WebView handles mouse and some keyboard events, and manages scrolling automatically, so there's no need to put it into a ScrollPane.

WebView objects must be created and accessed solely from the FX thread.


Class ImageCursor


  • public class ImageCursorextends Cursor

    A custom image representation of the mouse cursor. On platforms that don't support custom cursors, Cursor.DEFAULT will be used in place of the specified ImageCursor.

    Example:

    import javafx.scene.*;
    import javafx.scene.image.*;
    
    Image image = new Image("mycursor.png");
    
    Scene scene = new Scene(400, 300);
    scene.setCursor(new ImageCursor(image,
                                    image.getWidth() / 2,
                                    image.getHeight() /2));

Class NodeBuilder<B extends NodeBuilder<B>>

  • java.lang.Object


    • javafx.scene.NodeBuilder<B>

相关文章
相关标签/搜索