1
\$\begingroup\$

I built a 2x2 Rubik's cube with javafx and my next goal is to apply animations to it. For that I need to keep track of the individual cubes. But I could not find one. Then I simply set up four Group objects namely front,back,right and left and assigned the respective cubes to them. I added buttons for the rotations. Here's my code,

import javafx.animation.RotateTransition;
import javafx.application.Application;
import javafx.application.ConditionalFeature;
import javafx.application.Platform;
import javafx.scene.AmbientLight;
import javafx.scene.DepthTest;
import javafx.scene.Group;
import javafx.scene.PerspectiveCamera;
import javafx.scene.Scene;
import javafx.scene.SceneAntialiasing;
import javafx.scene.SubScene;
import javafx.scene.control.Button;
import javafx.scene.paint.Color;
import javafx.scene.paint.PhongMaterial;
import javafx.scene.shape.CullFace;
import javafx.scene.shape.MeshView;
import javafx.scene.shape.TriangleMesh;
import javafx.scene.transform.Rotate;
import javafx.scene.transform.Translate;
import javafx.stage.Stage;
import javafx.util.Duration;

/**
 *
 * @author deb-l-ana
 */
public class NewFXMain extends Application {

    @Override
    public void start(Stage primaryStage) {
        System.out.println(Platform.isSupported(ConditionalFeature.SCENE3D));
        primaryStage.setTitle("Coordinate axex");
        Group root=new Group();
        SubScene scene=new SubScene(root,300,300,true,SceneAntialiasing.BALANCED);
        PerspectiveCamera camera = new PerspectiveCamera(true);
        root.setDepthTest(DepthTest.ENABLE);
        camera.setNearClip(0.1);
camera.setFarClip(10000.0);

        camera.getTransforms().addAll (new Translate(0, 0, -1000));


        Cube c1 = new Cube(50,Color.BLACK.brighter(),Color.BLACK.brighter(),Color.ORANGE.brighter(),Color.BLACK.brighter(),Color.BLUE.brighter(),Color.YELLOW.brighter());
         c1.setTranslateX(105);

        Cube c2 = new Cube(50,Color.BLACK.brighter(),Color.BLACK.brighter(),Color.BLACK.brighter(),Color.GREEN.brighter(),Color.BLUE.brighter(),Color.YELLOW.brighter());
        c2.setTranslateX(50);

        Cube c3 = new Cube(50,Color.WHITE.darker(),Color.BLACK.brighter(),Color.BLACK.brighter(),Color.GREEN.brighter(),Color.BLUE.brighter(),Color.BLACK.brighter());
        c3.setTranslateX(50);
        c3.setTranslateZ(55);

        Cube c4 = new Cube(50,Color.WHITE.darker(),Color.BLACK.brighter(),Color.ORANGE.brighter(),Color.BLACK.brighter(),Color.BLUE.brighter(),Color.BLACK.brighter());
        c4.setTranslateX(105);
        c4.setTranslateZ(55);

        Cube c5 = new Cube(50,Color.BLACK.brighter(),Color.RED.brighter(),Color.ORANGE.brighter(),Color.BLACK.brighter(),Color.BLACK.brighter(),Color.YELLOW.brighter());
         c5.setTranslateX(105);
         c5.setTranslateY(55);

        Cube c6 = new Cube(50,Color.BLACK.brighter(),Color.RED.brighter(),Color.BLACK.brighter(),Color.GREEN.brighter(),Color.BLACK.brighter(),Color.YELLOW.brighter());
        c6.setTranslateX(50);
        c6.setTranslateY(55);

        Cube c7 = new Cube(50,Color.WHITE.darker(),Color.RED.brighter(),Color.BLACK.brighter(),Color.GREEN.brighter(),Color.BLACK.brighter(),Color.BLACK.brighter());
        c7.setTranslateX(50);
        c7.setTranslateZ(55);
        c7.setTranslateY(55);

        Cube c8 = new Cube(50,Color.WHITE.darker(),Color.RED.brighter(),Color.ORANGE.brighter(),Color.BLACK.brighter(),Color.BLACK.brighter(),Color.BLACK.brighter());
        c8.setTranslateX(105);
        c8.setTranslateZ(55);
        c8.setTranslateY(55);
        Rotate rotateX = new Rotate(30, 0, 0, 0, Rotate.X_AXIS);
        Rotate rotateY = new Rotate(20, 0, 0, 0, Rotate.Y_AXIS);
        scene.setOnMouseDragged(me -> {
            mousePosX = me.getSceneX();
            mousePosY = me.getSceneY();
            rotateX.setAngle(rotateX.getAngle()-(mousePosY - mouseOldY));
            rotateY.setAngle(rotateY.getAngle()+(mousePosX - mouseOldX));
            mouseOldX = mousePosX;
            mouseOldY = mousePosY;
        });


        root.getTransforms().addAll(rotateX, rotateY);
        Group k=new Group(c1,c2,c3,c4,c5,c6,c7,c8);
        k.setRotationAxis(Rotate.Y_AXIS);
        k.setRotate(180);
        root.getChildren().addAll(k,new AmbientLight());
        scene.setCamera(camera);
        \\Animation section
        Button F = new Button("F");
        Button F_ = new Button("F_");
        Button B = new Button("B");
        Button B_ = new Button("B_");
        Button L = new Button("L");
        Button L_ = new Button("L_");
        Button R = new Button("R");
        Button R_ = new Button("R_");
        Button X = new Button("X");
        Button X_ = new Button("X_");
        Button Y = new Button("Y");
        Button Y_ = new Button("Y_");
        Button Z = new Button("Z");
        Button Z_ = new Button("Z_");

        Group front=new Group(c1,c2,c5,c6);
        Group back=new Group(c3,c4,c7,c8);
        Group right=new Group(c1,c4,c5,c8);
        Group left=new Group(c2,c3,c6,c7);


        PerspectiveCamera camera2 = new PerspectiveCamera(true);


        F.setOnAction(e -> {
            front.setRotationAxis(Rotate.Z_AXIS);
            RotateTransition rt = new RotateTransition(Duration.millis(100), front);
            rt.setByAngle(90);
            rt.play();
                });

        F_.setOnAction(e -> {
            front.setRotationAxis(Rotate.Z_AXIS);
            RotateTransition rt = new RotateTransition(Duration.millis(100), front);
            rt.setByAngle(-90);
            rt.play();
                });
        //moves- normal letters represent clockwise and underscored represents anticlockwise rotations.

        F.setTranslateY(200);
        F_.setTranslateY(170);
        B.setTranslateY(140);
        B_.setTranslateY(110);
        L.setTranslateY(80);
        L_.setTranslateY(50);
        R.setTranslateY(20);
        R_.setTranslateY(-10);
        //The rest are not being added right now
        /*X.setTranslateY(-40);
        X_.setTranslateY(-70);
        Y.setTranslateY(-100);
        Y_.setTranslateY(-130);
        Z.setTranslateY(-160);
        Z_.setTranslateY(-190);*/

        Group r=new Group(F,F_,B,B_,L,L_,R,R_);
        Scene s=new Scene(r,600,600,true);
        camera2.setNearClip(0.1);
        camera2.setFarClip(10000.0);
        root.getChildren().addAll(front);

        B.setOnAction(e -> {
            back.setRotationAxis(Rotate.Z_AXIS);
            RotateTransition rt = new RotateTransition(Duration.millis(100), back);
            rt.setByAngle(90);
            rt.play();
                });

        B_.setOnAction(e -> {
            back.setRotationAxis(Rotate.Z_AXIS);
            RotateTransition rt = new RotateTransition(Duration.millis(100), back);
            System.out.println("hhhh");
            rt.setByAngle(-90);
            rt.play();
                });
        root.getChildren().addAll(back);


        L.setOnAction(e -> {
            left.setRotationAxis(Rotate.X_AXIS);
            RotateTransition rt = new RotateTransition(Duration.millis(1000), left);
            rt.setByAngle(90);
            rt.play();
                });

        L_.setOnAction(e -> {
            left.setRotationAxis(Rotate.X_AXIS);
            RotateTransition rt = new RotateTransition(Duration.millis(1000), left);
            rt.setByAngle(-90);
            rt.play();
                });
        root.getChildren().addAll(left);


        R.setOnAction(e -> {
            right.setRotationAxis(Rotate.X_AXIS);
            RotateTransition rt = new RotateTransition(Duration.millis(1000), right);
            rt.setByAngle(90);
            rt.play();
                });

        R_.setOnAction(e -> {
            right.setRotationAxis(Rotate.X_AXIS);
            RotateTransition rt = new RotateTransition(Duration.millis(1000), right);
            rt.setByAngle(-90);
            rt.play();
                });
        root.getChildren().addAll(right);
        camera2.getTransforms().addAll (new Translate(0, 0, -1000));
        s.setCamera(camera2);
        r.getChildren().addAll(scene);
        primaryStage.setScene(s);
        primaryStage.show();

    }
    double mousePosX,mousePosY,mouseOldX,mouseOldY;
     /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

    public class Cube extends Group {
        final Rotate rx = new Rotate(0,Rotate.X_AXIS);
        final Rotate ry = new Rotate(0,Rotate.Y_AXIS);
        final Rotate rz = new Rotate(0,Rotate.Z_AXIS); 
        public Cube(double size, Color back,Color bottom,Color left,Color right,Color top,Color front) {

                getTransforms().addAll(rz, ry, rx);

                //FIRST RECTANGLE BACK FACE
                TriangleMesh re1 = new TriangleMesh();
                re1.getPoints().addAll(
                        0,0,0,
                        0,(float)size,0,
                        (float)size,(float)size,0,
                        (float)size,0,0
                );
                re1.getTexCoords().addAll(
                        0,0
                );
                re1.getFaces().addAll(
                        0,0, 1,0, 3,0,
                        1,0, 2,0, 3,0
                );

                MeshView rectangle1 = new MeshView(re1);
                rectangle1.setCullFace(CullFace.NONE);
                PhongMaterial mat = new PhongMaterial(back);

                rectangle1.setMaterial(mat);
                rectangle1.setTranslateX(-0.5*size);
                rectangle1.setTranslateY(-0.5*size);
                rectangle1.setTranslateZ(0.5*size);

                //SECOND RECTANGLE
                TriangleMesh re2 = new TriangleMesh();
                re2.getPoints().addAll(
                        0,0,0,
                        0,(float)size,0,
                        (float)size,(float)size,0,
                        (float)size,0,0
                );
                re2.getTexCoords().addAll(0,0,
                        0,0
                        );
                re2.getFaces().addAll(
                        0,0, 1,0, 3,0,
                        1,0, 2,0, 3,0
                );
                MeshView rectangle2 = new MeshView(re2);
                PhongMaterial mat2=new PhongMaterial(bottom);
                rectangle2.setMaterial(mat2);
                rectangle2.setCullFace(CullFace.NONE);
                rectangle2.setTranslateX(-0.5*size);
                rectangle2.setTranslateY(0);
                rectangle2.setRotationAxis(Rotate.X_AXIS);
                rectangle2.setRotate(90);

                //CREATE THIRD RECTANGLE 
                TriangleMesh re3 = new TriangleMesh();
                re3.getPoints().addAll(
                        0,0,0,
                        0,(float)size,0,
                        (float)size,(float)size,0,
                        (float)size,0,0
                );
                re3.getTexCoords().addAll(0,0,
                        0,0
                        );
                re3.getFaces().addAll(
                        0,0, 1,0, 3,0,
                        1,0, 2,0, 3,0
                );
                MeshView rectangle3 = new MeshView(re3);
                PhongMaterial mat3=new PhongMaterial(right);
                rectangle3.setMaterial(mat3);
                rectangle3.setCullFace(CullFace.NONE);
                rectangle3.setTranslateX(-1*size);
                rectangle3.setTranslateY(-0.5*size);
                rectangle3.setRotationAxis(Rotate.Y_AXIS);
                rectangle3.setRotate(90);

                //CREATE FOURTH RECTANGLE
                TriangleMesh re4 = new TriangleMesh();
                re4.getPoints().addAll(
                        0,0,0,
                        0,(float)size,0,
                        (float)size,(float)size,0,
                        (float)size,0,0
                );
                re4.getTexCoords().addAll(0,0,
                        0,0
                        );
                re4.getFaces().addAll(
                        0,0, 1,0, 3,0,
                        1,0, 2,0, 3,0
                );
                MeshView rectangle4 = new MeshView(re4);
                PhongMaterial mat4=new PhongMaterial(left);
                rectangle4.setMaterial(mat4);
                rectangle4.setCullFace(CullFace.NONE);
                rectangle4.setTranslateX(0);
                rectangle4.setTranslateY(-0.5*size);
                rectangle4.setRotationAxis(Rotate.Y_AXIS);
                rectangle4.setRotate(90);

                //CREATE FIFTH RECTANGLE    
                TriangleMesh re5 = new TriangleMesh();
                re5.getPoints().addAll(
                        0,0,0,
                        0,(float)size,0,
                        (float)size,(float)size,0,
                        (float)size,0,0
                );
                re5.getTexCoords().addAll(0,0,
                        0,0
                        );
                re5.getFaces().addAll(
                        0,0, 1,0, 3,0,
                        1,0, 2,0, 3,0
                );
                MeshView rectangle5 = new MeshView(re5);
                PhongMaterial mat5=new PhongMaterial(top);
                rectangle5.setMaterial(mat5);
                rectangle5.setCullFace(CullFace.NONE);
                rectangle5.setTranslateX(-0.5*size);
                rectangle5.setTranslateY(-1*size);
                rectangle5.setRotationAxis(Rotate.X_AXIS);
                rectangle5.setRotate(90);

                //CREATE SIXTH RECTANGLE
                TriangleMesh re6 = new TriangleMesh();
                re6.getPoints().addAll(
                        0,0,0,
                        0,(float)size,0,
                        (float)size,(float)size,0,
                        (float)size,0,0
                );
                re6.getTexCoords().addAll(0,0,
                        0,0
                        );
                re6.getFaces().addAll(
                        0,0, 1,0, 3,0,
                        1,0, 2,0, 3,0
                );
                MeshView rectangle6 = new MeshView(re6);
                PhongMaterial mat6=new PhongMaterial(front);
                rectangle6.setMaterial(mat6);
                rectangle6.setCullFace(CullFace.NONE);
                rectangle6.setTranslateX(-0.5*size);
                rectangle6.setTranslateY(-0.5*size);
                rectangle6.setTranslateZ(-0.5*size);

                getChildren().addAll(
                        rectangle1,
                        rectangle2,
                        rectangle3,
                        rectangle4,
                        rectangle5,
                        rectangle6
                );

        }
    }
}

As I executed my program, the moves F,F_, B, B_ didn't work. Can anyone please suggest me what algorithm I should follow to make a proper animation of the cube and also why the mentioned moves were not working ?

Here's the picture

\$\endgroup\$
2
  • \$\begingroup\$ Not working isn't a very good description of what's going wrong. Your question should contain what's actually happening when you press the button compared what you would like to happen. This will make it easier to answer. \$\endgroup\$
    – Christer
    Dec 4, 2015 at 17:11
  • \$\begingroup\$ I wanted the front and back faces of the cube to rotate along with the button click events but the faces stay still but with the L , L_, R and R_ buttons the rotations are fine \$\endgroup\$ Dec 4, 2015 at 17:18

2 Answers 2

1
\$\begingroup\$

I just realize this. You can not repeat elements in a group. Once you put them in a group they stop belonging to the other group. In this case because left and right are defined last they work. If you change the code to

    Group right=new Group(c1,c4,c5,c8);
    Group left=new Group(c2,c3,c6,c7);
    Group front=new Group(c1,c2,c5,c6);
    Group back=new Group(c3,c4,c7,c8);

The opposite is true and now B and F buttons work but not L and R.

Perhaps someone can point how to achieve this.

\$\endgroup\$
0
\$\begingroup\$

A possible solution could be to create a class that simply contains four cubes.

public class CubeGroup {

    private ArrayList<Cube> cubes = new ArrayList<Cube>(4);

    public CubeGroup(Cube c1, Cube c2, Cube c3, Cube c4) {
        this.cubes.addAll(c1, c2, c3, c4);
    }

    ...

}

Being my first answer here, I am unsure if this can properly answer your question, but I believe this is a good way to solve the issue.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .