如何在节点旋转时保持DropShadow的角度

时间:2015-05-16 05:05:13

标签: javafx dropshadow

在旋转的节点上应用DropShadow时,DropShadow随之旋转。是否有一种简单的方法可以将DropShadow角度保持在原位,例如: G。即使节点旋转,也在右下角?

我知道,如果我将所有节点放入一个组并在组中应用阴影,它会起作用,但遗憾的是,在我的情况下这不是一个选项。

示例图片:

  • 带阴影的左侧矩形
  • 具有相同投影的右侧矩形,但旋转180度

enter image description here

你看,阴影朝着相反的方向看错了。

代码

public class HelloEffects extends Application {

    Stage stage;
    Scene scene;

    @Override
    public void start(Stage stage) {

        Group group = new Group();

        DropShadow ds1 = new DropShadow();
        ds1.setOffsetY(4.0f);
        ds1.setOffsetX(4.0f);
        ds1.setColor(Color.BLACK);

        Rectangle rect1 = new Rectangle( 100, 200);
        rect1.relocate(100, 100);
        rect1.setEffect(ds1);
        rect1.setFill(Color.RED);

        Rectangle rect2 = new Rectangle( 100, 200);
        rect2.relocate(300, 100);
        rect2.setEffect(ds1);
        rect2.setFill(Color.RED);
        rect2.setRotate(180);

        group.getChildren().addAll(rect1, rect2);

        scene = new Scene( group, 840, 680);

        stage.setScene(scene);
        stage.show();

    }

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

1 个答案:

答案 0 :(得分:1)

您应该向rect2添加容器并将效果应用于容器,容器可以是窗格或组:

Group rect2Container = new Group(rect2);
rect2Container.setEffect(ds1);
group.getChildren().addAll(rect1, rect2Container);
相关问题