JavaFX Rubik多维数据集-多维数据集选择

时间:2018-11-11 02:47:40

标签: java javafx observablelist javafx-3d rubiks-cube

我正在尝试使用JavaFx从头开始构建Rubiks多维数据集,这是我的第一个real应用。在Moves类Im中,根据位置从ObservableList中选择Boxs。问题是Box.getTranslate剂量更新,因此当我尝试依次移动正面和左侧时,两个都选择的Box总是移动,从而导致混乱。如何重新编写此代码,以便Move方法正确选择要移动的框?这是代码,正在进行中。

主要

package ro.adrianpush;


import javafx.application.Application;
import javafx.collections.ObservableList;
import javafx.scene.*;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Box;
import javafx.stage.Stage;



public class Main extends Application {

    private static final int WIDTH = 800;
    private static final int HEIGHT = 600;
    final Group root = new Group();


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

        Camera camera = new PerspectiveCamera();
        camera.translateXProperty().setValue(-200);
        camera.translateYProperty().setValue(0);
        camera.translateZProperty().set(-500);

        AnchorPane pane = new AnchorPane();

        Rubik rubik = new Rubik();
        ObservableList<Box> boxArrayList = rubik.getBoxArrayList();

        for (Box box: boxArrayList
        ) {
                pane.getChildren().addAll(box);
        }

        primaryStage.addEventHandler(KeyEvent.KEY_PRESSED, event -> {
            switch (event.getCode()){
                case E:
                    Moves.rotateFront(boxArrayList, "clockwise");
                    break;
                case Q:
                    Moves.rotateFront(boxArrayList, "counterclockwise");
                    break;
                case A:
                    Moves.rotateLeft(boxArrayList, "clockwise");
                    break;
                case D:
                    Moves.rotateLeft(boxArrayList, "counterclockwise");
                    break;

            }
        });

        root.getChildren().add(pane);
        Scene scene = new Scene(root, WIDTH, HEIGHT, true);
        scene.setCamera(camera);
        scene.setFill(Color.ROYALBLUE);
        primaryStage.setTitle("The game nobody wants to play");
        primaryStage.setScene(scene);
        primaryStage.show();
    }


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


}

鲁比克

package ro.adrianpush;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.shape.Box;


public class Rubik {

    private ObservableList<Box> boxArrayList = FXCollections.observableArrayList();


    public Rubik(){

        for(int i = 1; i < 4; i+=1){
            for( int j = 1; j < 4; j++){
                for(int k = 1; k < 4; k++){

                    Box box = new Box(100,100,100);
                    box.setTranslateX(i*100);
                    box.setTranslateY(j*100);
                    box.setTranslateZ(k*100);
                    boxArrayList.add(box);
                }
            }
        }

    }



    public ObservableList<Box> getBoxArrayList() {
        return boxArrayList;
    }


}

移动

package ro.adrianpush;


import javafx.collections.ObservableList;
import javafx.scene.shape.Box;
import javafx.scene.transform.Rotate;

public class Moves {

    public static void rotateFront(ObservableList<Box> boxArrayList, String direction) {

        for (Box box: boxArrayList
             ) {

            if(box.getTranslateZ() == 100){
                Rotate rotate = new Rotate();
                rotate.setAxis(Rotate.Z_AXIS);
                if(direction == "clockwise"){
                    rotate.setAngle(5);
                } else if (direction == "counterclockwise"){
                    rotate.setAngle(-5);
                }

                if(box.getTranslateX() == 100){
                    rotate.setPivotX(100);
                }  if (box.getTranslateX() == 300){
                    rotate.setPivotX(-100);
                }  if(box.getTranslateY() == 100){
                    rotate.setPivotY(100);
                }  if(box.getTranslateY() == 300){
                    rotate.setPivotY(-100);
                }

                box.getTransforms().add(rotate);
            }
        }
    }

    public static void rotateBack(ObservableList<Box> boxArrayList, String direction) {

        for (Box box: boxArrayList
        ) {

            if(box.getTranslateZ() == 300){
                Rotate rotate = new Rotate();

                if(direction == "clockwise"){
                    rotate.setAngle(5);
                } else if (direction == "counterclockwise"){
                    rotate.setAngle(-5);
                }

                if(box.getTranslateX() == 100){
                    rotate.setPivotX(100);
                }  if (box.getTranslateX() == 300){
                    rotate.setPivotX(-100);
                }  if(box.getTranslateY() == 100){
                    rotate.setPivotY(100);
                }  if(box.getTranslateY() == 300){
                    rotate.setPivotY(-100);
                }

                box.getTransforms().add(rotate);
            }
        }
    }

    public static void rotateLeft(ObservableList<Box> boxArrayList, String direction) {

        for (Box box: boxArrayList
        ) {

            if(box.getTranslateX() == 100){
                Rotate rotate = new Rotate();
                rotate.setAxis(Rotate.X_AXIS);

                if(direction == "clockwise"){
                    rotate.setAngle(5);
                } else if (direction == "counterclockwise"){
                    rotate.setAngle(-5);
                }

                if(box.getTranslateY() == 100){
                    rotate.setPivotY(100);
                }  if (box.getTranslateY() == 300){
                    rotate.setPivotY(-100);
                }  if(box.getTranslateZ() == 100){
                    rotate.setPivotZ(100);
                }  if(box.getTranslateZ() == 300){
                    rotate.setPivotZ(-100);
                }

                box.getTransforms().add(rotate);
            }
        }
    }
}

0 个答案:

没有答案