java - 将封底附加到卡片图像上

标签 java javafx javafx-2 javafx-8

我找到了this top answer (第一 block 代码)帮助我旋转图像(在我的例子中是一张卡片)。但是如何将后盖 ( example ) 安装到该旋转卡上?

最佳答案

假设您没有更换相机,并且根本看不到图像的角度仍然是 90° 和 270°,则只需交换 中的 Image 即可ImageView 在这些角度上也将这些角度的 scaleX 属性分别更改为 -11 ,这样您就不需要镜像原始图像。

以下代码是jewelsea's的修改版本answer to "Flip a card animation" ,因此必须将部分代码的功劳授予该用户。

public class QuickFlip extends Application {
    @Override
    public void start(Stage stage) throws Exception {
        BooleanProperty showFront = new SimpleBooleanProperty(true);

        Node card = createCard(showFront,
                new Image("http://www.ohmz.net/wp-content/uploads/2012/05/Game-of-Throne-Magic-trading-cards-2.jpg"),
                new Image("https://upload.wikimedia.org/wikipedia/en/a/aa/Magic_the_gathering-card_back.jpg")
                );

        stage.setScene(createScene(card));
        stage.show();

        // first 90° -> show front
        RotateTransition rotator1 = createRotator(card, 0, 90);

        // from 90° to 270° show backside
        rotator1.setOnFinished(evt -> showFront.set(false));
        RotateTransition rotator2 = createRotator(card, 90, 270);

        // from 270° to 360° show front again
        rotator2.setOnFinished(evt -> showFront.set(true));
        RotateTransition rotator3 = createRotator(card, 270, 360);

        SequentialTransition rotator = new SequentialTransition(card, rotator1, rotator2, rotator3);
        rotator.setCycleCount(10);
        rotator.play();
    }

    private Scene createScene(Node card) {
        StackPane root = new StackPane();
        root.getChildren().addAll(card);

        Scene scene = new Scene(root, 600, 700, true, SceneAntialiasing.BALANCED);
        scene.setCamera(new PerspectiveCamera());

        return scene;
    }

    private Node createCard(BooleanProperty showFront, Image front, Image back) {
        ImageView imageView = new ImageView();

        imageView.setFitHeight(front.getHeight());
        imageView.setFitWidth(front.getWidth());

        // show front/back depending on value of the showFront property
        imageView.imageProperty().bind(Bindings.when(showFront).then(front).otherwise(back));

        // mirror image, when backside is shown to prevent wrong orientation
        imageView.scaleXProperty().bind(Bindings.when(showFront).then(1d).otherwise(-1d));
        return imageView;
    }

    private RotateTransition createRotator(Node card, double fromAngle, double toAngle) {
        // animation length proportional to the rotation angle
        RotateTransition rotator = new RotateTransition(Duration.millis(Math.abs(10000 * (fromAngle - toAngle) / 360)), card);
        rotator.setAxis(Rotate.Y_AXIS);
        rotator.setFromAngle(fromAngle);
        rotator.setToAngle(toAngle);
        rotator.setInterpolator(Interpolator.LINEAR);

        return rotator;
    }

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

关于java - 将封底附加到卡片图像上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37117475/

相关文章:

JavaFX 2.0 : Addition of Text Shifts Other Controls

java - Java Rest api 中的资源实例

java - 使 JavaFX 应用程序窗口外部元素在所有操作系统中保持相似

javafx-2 - Javafx 剪贴板双换行符

java - 如何在没有CMD窗口的情况下运行jlink生成的Java运行时镜像?

java - 如何在 NetBeans 10/JDK11 上启用 JavaFX

java - 在 Swing JFrame 上添加/删除 JFXPanel

java.lang.NoClassDefFoundError : HttpServletRequest 错误

java - 线程 "main"java.lang.NoClassDefFoundError : HelloWorld 中的异常

java - @Autowired 不适用于从非 spring jpos 库实现的类