1

I am trying to build a Rubiks Cube from scratch as my first real app using JavaFx. In the Moves class Im selecting Boxes from ObservableList based on position. The problem is that the Box.getTranslate dosent update so when I try to move the front and the left faces successively the Boxes selected by both are always moved resulting in.. chaos. How could I re-write this so that the Move methods correctly select the boxes to move? Here is the code, work in progress.

Main

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);
    }


}

Rubik

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;
    }


}

Moves

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);
            }
        }
    }
}
José Pereda
  • 44,311
  • 7
  • 104
  • 132
  • 1
    You might want to have a look at [this](http://jperedadnr.blogspot.com/2014/04/rubikfx-solving-rubiks-cube-with-javafx.html) take on the task. – user1803551 Nov 11 '18 at 09:56
  • Your `for` loops are not formatted properly, the `(` should be on the same line and usually the `{` is too. – user1803551 Nov 11 '18 at 10:04
  • As @user1803551 mentioned, that blog post is a good way to start. I discussed about the use of `prepend` transform instead of `add`. See also the projects [LiteRubikFX](https://github.com/jperedadnr/LiteRubikFX) and [RubikFX](https://github.com/jperedadnr/RubikFX), or even [Rubiks-cube](https://github.com/gluonhq/gluon-samples/tree/master/rubiks-cube) on mobile. See [this](https://gist.github.com/jperedadnr/28534fcdce605b75382b) for a lighter version of the cube. – José Pereda Nov 11 '18 at 13:15

0 Answers0