Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/400.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 翻转卡片动画_Java_Javafx 2_Javafx_Java 3d_Javafx 8 - Fatal编程技术网

Java 翻转卡片动画

Java 翻转卡片动画,java,javafx-2,javafx,java-3d,javafx-8,Java,Javafx 2,Javafx,Java 3d,Javafx 8,我正试图翻转一个彩色矩形。是否可以使用RotateTransfion来执行此操作 我尝试了以下代码: public void rotateField(){ RotateTransition rt = new RotateTransition(Duration.millis(3000), field[4][4]); rt.setByAngle(360); rt.setCycleCount(1); rt.play(); } 但是,这不会翻转矩形,它只是旋转矩形。

我正试图翻转一个彩色矩形。是否可以使用RotateTransfion来执行此操作

我尝试了以下代码:

 public void rotateField(){
    RotateTransition rt = new RotateTransition(Duration.millis(3000), field[4][4]);
    rt.setByAngle(360);
    rt.setCycleCount(1);
    rt.play();
}
但是,这不会翻转矩形,它只是旋转矩形。 我想翻转矩形,就像你翻转扑克牌一样


是否可以为此使用rotateTransition类?

不适用于2D世界。但您可以使用两个
ScaleTransition
s来模拟卡片翻转:

Rectangle front = new Rectangle(50, 50);

ScaleTransition stHideFront = new ScaleTransition(Duration.millis(1500), front);
stHideFront.setFromX(1);
stHideFront.setToX(0);

Rectangle back = new Rectangle(50, 50, Color.RED);
back.setScaleX(0);

ScaleTransition stShowBack = new ScaleTransition(Duration.millis(1500), back);
stShowBack.setFromX(0);
stShowBack.setToX(1);

stHideFront.setOnFinished(new EventHandler<ActionEvent>() {
    @Override
    public void handle(ActionEvent t) {
        stShowBack.play();
    }
});

StackPane root = new StackPane();
root.getChildren().addAll(front, back);
Scene scene = new Scene(root, 300, 250);
primaryStage.setScene(scene);

primaryStage.show();
stHideFront.play();
Rectangle front=新矩形(50,50);
ScaleTransition stHideFront=新的ScaleTransition(持续时间.毫秒(1500),前);
stHideFront.setFromX(1);
stHideFront.setToX(0);
矩形背面=新矩形(50,50,颜色为红色);
背面。设置刻度(0);
ScaleTransition stShowBack=新的ScaleTransition(持续时间.毫秒(1500),返回);
stShowBack.setFromX(0);
stShowBack.setToX(1);
setOnFinished(新的EventHandler(){
@凌驾
公共无效句柄(ActionEvent t){
stShowBack.play();
}
});
StackPane root=新的StackPane();
root.getChildren().addAll(前、后);
场景=新场景(根,300,250);
初级阶段。场景(场景);
primaryStage.show();
stHideFront.play();

我喜欢Sergey的解决方案,它非常巧妙地使用了ScaleTransition,在2D中工作意味着你不需要处理3D的一些复杂问题


这里有两个3D旋转示例

绕Y轴旋转2D节点(图像视图)(需要JavaFX2.2+3D支持)


围绕Y轴旋转3D节点(三角形网格)(需要Java 8+3D支持)


你的例子太棒了!很好的例子,但是我如何在一次旋转后停止它呢?有一个循环计数,将行
rotator.setCycleCount(10)
更改为
rotator.setCycleCount(1)
仅旋转一次。Jewelsea我如何实际使用这个例子作为动画animate=new Animation();设置.launch()的动画;?我试着把它整合起来,但没用。其目的是集成到jframe@KayzelMoo创建一个新问题,在其中引用这个问题,并在问题中包含一个(有人可以用来复制您的问题的最小、完整的代码)。
import javafx.animation.*;
import javafx.application.Application;
import javafx.scene.*;
import javafx.scene.image.*;
import javafx.scene.layout.StackPane;
import javafx.scene.transform.Rotate;
import javafx.stage.Stage;
import javafx.util.Duration;

public class QuickFlip extends Application {
    @Override
    public void start(Stage stage) throws Exception {
        Node card = createCard();

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

        RotateTransition rotator = createRotator(card);
        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() {
        return new ImageView(
            new Image(
                "http://www.ohmz.net/wp-content/uploads/2012/05/Game-of-Throne-Magic-trading-cards-2.jpg"
            )
        );
    }

    private RotateTransition createRotator(Node card) {
        RotateTransition rotator = new RotateTransition(Duration.millis(10000), card);
        rotator.setAxis(Rotate.Y_AXIS);
        rotator.setFromAngle(0);
        rotator.setToAngle(360);
        rotator.setInterpolator(Interpolator.LINEAR);
        rotator.setCycleCount(10);

        return rotator;
    }

    public static void main(String[] args) {
        launch();
    }
}
import javafx.animation.*;
import javafx.application.Application;
import javafx.scene.*;
import javafx.scene.image.Image;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.*;
import javafx.scene.shape.*;
import javafx.scene.transform.Rotate;
import javafx.stage.Stage;
import javafx.util.Duration;

public class CardFlip extends Application {
    final Image CARD_IMAGE = new Image(
        "http://fc05.deviantart.net/fs70/i/2010/345/7/7/vitam_et_mortem_by_obviouschild-d34oni2.png"
        // sourced from: http://obviouschild.deviantart.com/art/Vitam-et-Mortem-189267194
    );
    final int W = (int) (CARD_IMAGE.getWidth()  / 2);
    final int H = (int) CARD_IMAGE.getHeight();

    @Override
    public void start(Stage stage) throws Exception {
        Node card = createCard();

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

        RotateTransition rotator = createRotator(card);
        rotator.play();
    }

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

        Scene scene = new Scene(root, W + 200, H + 200, true, SceneAntialiasing.BALANCED);
        scene.setFill(Color.MIDNIGHTBLUE.darker().darker().darker().darker());
        scene.setCamera(new PerspectiveCamera());

        return scene;
    }

    private RotateTransition createRotator(Node card) {
        RotateTransition rotator = new RotateTransition(Duration.millis(10000), card);
        rotator.setAxis(Rotate.Y_AXIS);
        rotator.setFromAngle(0);
        rotator.setToAngle(360);
        rotator.setInterpolator(Interpolator.LINEAR);
        rotator.setCycleCount(10);

        return rotator;
    }

    private Node createCard() {
        MeshView card = new MeshView(createCardMesh());

        PhongMaterial material = new PhongMaterial();
        material.setDiffuseMap(CARD_IMAGE);
        card.setMaterial(material);

        return card;
    }

    private TriangleMesh createCardMesh() {
        TriangleMesh mesh = new TriangleMesh();

        mesh.getPoints().addAll(-1 * W/2, -1 * H/2 , 0, 1 * W/2, -1 * H/2, 0, -1 * W/2, 1 * H/2, 0, 1 * W/2, 1 * H/2, 0);
        mesh.getFaces().addAll(0, 0, 2, 2, 3, 3, 3, 3, 1, 1, 0, 0);
        mesh.getFaces().addAll(0, 4, 3, 7, 2, 6, 3, 7, 0, 4, 1, 5);
        mesh.getTexCoords().addAll(0, 0, 0.5f, 0, 0, 1, 0.5f, 1, 0.5f, 0, 1, 0, 0.5f, 1, 1, 1);

        return mesh;
    }

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