JavaFX如何将形状重叠到另一个 [英] JavaFX how to overlap a shape onto another

查看:316
本文介绍了JavaFX如何将形状重叠到另一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在圆顶上创建一条线.

I am trying to create a line on top of a circle.

我尝试过的事情:

  • 我将圆圈添加到StackPane,然后将StackPane和行添加到发送给场景构造函数的组中得到了一些不希望的结果,例如stackpane无法在舞台上完全散布

在圆后添加线会自动将其置于圆线上方,但我需要对其进行更多控制.

adding the line after the circle puts it automatically above that but i need more control over it.

例如,我有10个形状,我想声明所有形状,然后决定要处理的内容,而我不想担心要按顺序添加它们

e.g., i have 10 shapes and i want to declare them all and then decide what goes over what and i don't want to worry about adding them in order

推荐答案

JavaFX的绘制方式

JavaFX使用画家的算法,绘制孩子 .oracle.com/javase/8/javafx/api/javafx/scene/Parent.html"rel =" noreferrer> Parent 节点从头到尾依次排列,以便最后一个节点被绘制在第一节点.

JavaFX uses a Painter's algorithm, painting the children of a Parent node in order from first to last, so that the last nodes will be painted over the first nodes.

这是在每个保留模式中完成的. docs.oracle.com/javase/8/javafx/get-started-tutorial/jfx-architecture.htm#A1107438"rel =" noreferrer>脉冲而不是

This is done in a retained mode on every pulse rather than an immediate mode. So items are not painted until you relinquish control of logic to the JavaFX graphics system. That means that you can add the items to the scene graph and then reorder or move them around in the scene graph as you wish, before they are painted.

JavaFX中的3D绘画也可以使用 Z缓冲,但是此答案仅与具有相同z坐标值的元素的2D绘制,而无需Z缓冲.

3D painting in JavaFX can also use Z-buffering, but this answer relates just to 2D painting of elements with the same z co-ordinate value, without Z-buffering.

如何设置节点的绘制顺序

通过将节点添加到父级的子级列表中,可以将节点放置在

By adding nodes to the children list of a parent, you can place your nodes in a Group parent or a Pane parent. The nodes will be painted in the order in which you added them to the children list.

如果您想使用CSS,则窗格是个好选择,否则通常可以使用Group.窗格也具有可调整大小的范围,而组则没有.由于这些原因,我通常更喜欢使用窗格而不是组.

A Pane is good if you wish to use CSS, otherwise a Group will usually do. Also a Pane has a resizable range, which a group does not. For these reasons, I usually prefer to use a Pane over a Group.

要更改子代的绘制顺序,您可以在子代列表中的适当位置移动节点.

To change the order in which the children are painted, you can move nodes around in position within the children list.

例如,以下代码将更改父窗格子级列表中第一和第五项的绘制顺序:

For example, the following code will change the paint order of the first and fifth items in the parent pane children list:

Collections.swap(parentPane.getChildren(), 0, 4)

此外,节点具有 toFront toBack 便捷方法将节点移至节点的父级子级列表的后方或前部.

In addition, Node has toFront and toBack convenience methods to move a node to the back or front of the node's parent's child list.

关于布局窗格使用的建议

不要使用诸如StackPane之类的布局窗格来保存要明确定位的节点.根据布局窗格的内部算法,托管布局窗格(例如StackPane)旨在自动为您处理节点的布局.例如,默认情况下,添加到StackPane的每个节点都将位于StackPane的中间.

Don't use layout panes such as StackPane for holding nodes that you wish to explicitly position. Managed layout panes such as StackPane are designed to handle the layout of nodes automatically for you, according to an internal algorithm for the layout pane. For example, by default, every node you add to a StackPane will be centered in the middle of the StackPane.

相关

这篇关于JavaFX如何将形状重叠到另一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆