-->

Drawing transform independent layout bounds in Jav

2019-01-20 01:21发布

问题:

For a simple vector drawing app I am looking to implement a "Selection Box", that is a graphic representation of the layoutBounds of a Node.

Example:

example http://i61.tinypic.com/ou4p3t.png

Thanks to jewelsea and his BoundsExample, I have a good understanding now on how to get the data for the box. The part I am struggling with is actually drawing the box on the scene, in a way that correctly respects the transformations on the nodes.

Correctly in this case means the bounds logical size get scaled with a node, but the stroke of the selection box stays constant. Meaning a selection box scales with its corresponding node, but the stroke stays unscaled.

I can think of two general strategies to implement such a selection box.

  1. As a property of my custom node The selection box could be an internal details of my custom node with its visibility bound to the nodes selected state. In that case I would need to find a way to have nodes IGNORE the parents transformations, if that is possible.

  2. Drawing selection boxes on top of the scaled nodes on a transparent pane In that case I would bind the selection boxes to the layout bounds of the scaled nodes, after applying the transformations of a node to its bounds. This does not seem to happen in JFX (even for 'boundsInParent') as you can quickly test in the example by applying some scaling to 'group' in line ~122. Link to modified example with scaling.

Since I am fairly new to JFX I wanted to ask for advice. Hope you find this problem interesting :)

Best regards, Oliver.

回答1:

As it turns out, the SceneBuilder itself is the largest freely available JavaFX project that I know of, that has that excact problem already solved.

By studying the com.oracle.javafx.scenebuilder.kit.editor package of the SceneBuilder source code I was reassured, that my second proposed startegy is the way to go.