简体   繁体   English

应用比例缩放时不接受路径转换

[英]Path translation not honoured when scale applied

I am having some problems applying both a translation and scale to aa JavaFx Path node . 我在将转换和缩放应用于JavaFx Path节点时遇到一些问题。 I have found that whenever a scale is applied via a call to either setScaleX() or scaleY() then any translation applied by a call to relocate() is no long honoured. 我发现,每当一个规模通过调用应用要么setScaleX()的scaleY() ,然后通过调用适用于任何平移搬迁()是没有长荣幸。

For example, using this main class: 例如,使用此主类:

import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class MainApp extends Application {

    @Override
    public void start(Stage stage) throws Exception {

        StackPane myPane = new StackPane(new MyRegion());
        Scene myScene = new Scene(myPane);
        stage.setScene(myScene);
        stage.setWidth(500);
        stage.setHeight(500);
        stage.show();
    }

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

And the MyRegion class: MyRegion类:

import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.scene.layout.Region;
import javafx.scene.paint.Color;
import javafx.scene.shape.ClosePath;
import javafx.scene.shape.LineTo;
import javafx.scene.shape.MoveTo;
import javafx.scene.shape.Path;

public class MyRegion extends Region{

    private Path pointer;
    private ObjectProperty<Color> pointerColour = 
        new SimpleObjectProperty<>(Color.ORANGE);

    public MyRegion() {
        initGraphics();
    }

    private void initGraphics(){
        pointer = new Path();
        pointer.setFill(pointerColour.get());
        pointer.setStrokeWidth(0);
        pointer.getElements().add(new MoveTo(50, 50));
        pointer.getElements().add(new LineTo(50, 200));
        pointer.getElements().add(new LineTo(200, 200));
        pointer.getElements().add(new ClosePath());
        pointer.relocate(70, 70);
    }

    @Override
    protected void layoutChildren() {
        getChildren().removeAll(pointer);
        getChildren().addAll(pointer);
    }

    @Override
    protected double computePrefHeight(double width) {
        return 200;
    }

    @Override
    protected double computePrefWidth(double height) {
        return 200;
    }

}

This results in a triangle located at co-oridinates (70,70) as set by the line pointer.relocate(70, 70) in method initGraphics() : 这将导致一个三角形位于由方法initGraphics()的行pointer.relocate(70, 70) 70,70)设置的共坐标(70,70 initGraphics()

工作实例

If I now add X and Y scaling factors to the Path so that `initGraphics() now looks like: 如果我现在在路径中添加X和Y缩放因子,则`initGraphics()现在看起来像:

private void initGraphics(){
    pointer = new Path();
    pointer.setFill(pointerColour.get());
    pointer.setStrokeWidth(0);
    pointer.getElements().add(new MoveTo(50, 50));
    pointer.getElements().add(new LineTo(50, 200));
    pointer.getElements().add(new LineTo(200, 200));
    pointer.getElements().add(new ClosePath());
    pointer.relocate(70, 70);
    pointer.setScaleX(2);
    pointer.setScaleY(2);
}

The following results, which is not as I expected: 结果如下,这与我预期的不同:

在此处输入图片说明

So the size of the triangle has scaled as expected but it is drawn from the (0,0) . 因此,三角形的大小已按预期缩放,但它是从(0,0)绘制的。 This is almost as if the MoveTo() object has been ignored in the Path . 这几乎就像在Path忽略了MoveTo()对象。

It does not matter where the scale methods are called during the creation of the Path, the result is the same. 创建路径期间在何处调用scale方法都没有关系,结果是相同的。 I have also tried setting the first PathElement as a MoveTo(0,0) but the result is the same. 我也尝试将第一个PathElement设置为MoveTo(0,0)但结果是相同的。

Am I misunderstanding how these method calls work or is this a bug in JavaFx Node? 我是否误解了这些方法调用的工作方式,或者这是JavaFx Node中的错误?

When you apply scaleX / scaleY over a node, it is applied over it's center: 当在节点上应用scaleX / scaleY ,将在其中心上应用它:

Defines the factor by which coordinates are scaled about the center of the object along the X axis of this {@code Node}. 定义沿此{@code Node}的X轴围绕对象中心缩放坐标的因子。

The pivot point about which the scale occurs is the center of the untransformed {@link #layoutBoundsProperty layoutBounds}. 发生缩放的枢轴点是未变形的{@link #layoutBoundsProperty layoutBounds}的中心。

As for relocate : 至于relocate

Sets the node's layoutX and layoutY translation properties in order to relocate this node to the x,y location in the parent. 设置节点的layoutX和layoutY转换属性,以便将该节点重新定位到父节点的x,y位置。 This method does not alter translateX or translateY , which if also set will be added to layoutX and layoutY, adjusting the final location by corresponding amounts. 此方法不会更改translateX或translateY ,如果也进行了设置,则会将其添加到layoutX和layoutY,并以相应的数量调整最终位置。

(bold id mine) (我的粗体ID)

In your first case, as you say, the path will be relocated to (70, 70), but on the second case, with the scale, it is relocated to (70, 70), the center will be at (145, 145), and its size will be 300x300, meaning that the top left vertex will be at (145, 145) - (150, 150) = (-5, -5). 在你第一种情况下,如你所说,路径将被搬迁到(70,70),但在第二种情况下,具有规模,它搬迁到(70,70),该中心将在(145,145 ),其尺寸为300x300,这意味着左上角的顶点将为(145,145)-(150,150)=(-5,-5)。

This is the result of the bounding box: 这是边界框的结果:

System.out.println(pointer.getBoundsInParent());

>> BoundingBox [minX:-5.5, minY:-5.5, minZ:0.0, width:302.0, height:302.0, depth:0.0, maxX:296.5, maxY:296.5, maxZ:0.0]

(it adds 1 px for the path's stroke). (它为路径的笔划增加了1 px)。

So it is doing what it is supposed to. 因此,它正在按照预期的方式运行。

In case you want your path scaling from (70, 70), take its top left vertex as a pivot , and use a Scale transform: 如果要从(70,70)缩放路径,请以其左上角顶点为轴 ,然后使用Scale变换:

private void initGraphics(){
    pointer = new Path();
    pointer.setFill(pointerColour.get());
    pointer.setStrokeWidth(0);
    pointer.getElements().add(new MoveTo(50, 50));
    pointer.getElements().add(new LineTo(50, 200));
    pointer.getElements().add(new LineTo(200, 200));
    pointer.getElements().add(new ClosePath());
    pointer.relocate(70, 70);
    pointer.getTransforms().add(new Scale(2, 2, 50, 50));
}

缩放

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM