将animation应用到2×2魔方

我用javafx构建了一个2×2魔方,我的下一个目标是将animation应用到它。 为此,我需要跟踪各个立方体。 但我找不到一个。 然后,我简单地设置了四个Group对象,分别是frontbackleftright ,并为其分配了相应的立方体。 我添加了旋转的button。 这是我的代码,

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

当我执行我的程序时,F,F_,B,B_的动作不起作用。 任何人都可以请建议我什么algorithm,我应该遵循,使立方体的正确animation,以及为什么提到的举动不工作?

这是图片

我只是意识到这一点。 你不能在一个组中重复元素。 一旦你把他们放在一个小组,他们就不再属于另一个小组了。 在这种情况下,因为左和右定义了最后他们的工作。 如果你改变代码

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

相反的是,现在B和Fbutton工作,但不是L和R.

也许有人可以指出如何实现这一点。