I'm trying to rotate a Cube on a click, but when i do it one time, the cube reset his options and i can`t back to the first situation. How can i do that?. Thanks.
TEST
-------------------------------
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.PerspectiveCamera;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import javafx.scene.paint.ImagePattern;
import javafx.scene.shape.RectangleBuilder;
import javafx.scene.transform.Rotate;
import javafx.scene.transform.Translate;
import javafx.stage.Stage;
import javafx.util.Duration;
/**
* A sample that demonstrates an animated rotation of 3D cubes. When the
* application runs in standalone mode, the scene must be constructed with
* the depthBuffer argument set to true, and the root node must have depthTest
* set to true.
*
* @see javafx.scene.transform.Rotate
* @see javafx.scene.paint.Color
* @see javafx.scene.shape.RectangleBuilder
*/
public class CubeSample extends Application {
private Timeline animation;
private Timeline animation2;
private Boolean state = true;
private void init(Stage primaryStage) {
Group root = new Group();
primaryStage.setResizable(false);
primaryStage.setScene(new Scene(root, 400,150,true));
primaryStage.getScene().setCamera(new PerspectiveCamera());
root.getTransforms().addAll(
new Translate(400 / 2, 150 / 2),
new Rotate(180, Rotate.X_AXIS)
);
root.getChildren().add(create3dContent());
}
public Node create3dContent() {
final Cube c = new Cube(100,Color.BLACK,1);
animation = new Timeline();
animation.getKeyFrames().addAll(
new KeyFrame(Duration.ZERO,
new KeyValue(c.ry.angleProperty(), 0d)
),
new KeyFrame(Duration.seconds(1),
new KeyValue(c.ry.angleProperty(), 180d)
));
animation.setCycleCount(1);
c.setOnMousePressed(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
play();
}
});
animation2 = new Timeline();
animation2.getKeyFrames().addAll(
new KeyFrame(Duration.ZERO,
new KeyValue(c.ry.angleProperty(), 0d)
),
new KeyFrame(Duration.seconds(1),
new KeyValue(c.ry.angleProperty(), -180d)
));
animation2.setCycleCount(1);
c.setOnMousePressed(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
play();
}
});
return new Group(c);
}
public void play() {
if(state){
animation.play();
state=false;
}else{
animation2.play();
state=true;
}
}
@Override public void stop() {
animation.pause();
}
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 color, double shade) {
getTransforms().addAll(rz, ry, rx);
getChildren().addAll(
RectangleBuilder.create() // back face
.width(size).height(size)
.fill(new ImagePattern(
new Image("2.jpg"), 0, 0, 1, 1, true )
)
.translateX(-0.5*size)
.translateY(-0.5*size)
.translateZ(0.5*size)
.build(),
RectangleBuilder.create() // bottom face
.width(size).height(size)
.fill(color.deriveColor(0.0, 1.0, (1 - 0.4*shade), 1.0))
.translateX(-0.5*size)
.translateY(0)
.rotationAxis(Rotate.X_AXIS)
.rotate(90)
.build(),
RectangleBuilder.create() // right face
.width(size).height(size)
.fill(color.deriveColor(0.0, 1.0, (1 - 0.3*shade), 1.0))
.translateX(-1*size)
.translateY(-0.5*size)
.rotationAxis(Rotate.Y_AXIS)
.rotate(90)
.build(),
RectangleBuilder.create() // left face
.width(size).height(size)
.fill(color.deriveColor(0.0, 1.0, (1 - 0.2*shade), 1.0))
.translateX(0)
.translateY(-0.5*size)
.rotationAxis(Rotate.Y_AXIS)
.rotate(90)
.build(),
RectangleBuilder.create() // false top face
.width(size).height(size)
.fill(color.deriveColor(0.0, 1.0, (1 - 0.1*shade), 1.0))
.translateX(-0.5*size)
.translateY(-1*size)
.rotationAxis(Rotate.X_AXIS)
.rotate(90)
.build(),
RectangleBuilder.create() // top face
.width(size).height(size)
.fill(new ImagePattern(
new Image("1.jpg"), 0, 0, 1, 1, true
)
)
.translateX(-0.5*size)
.translateY(-0.5*size)
.translateZ(-0.5*size)
.build()
);
}
}
public double getSampleWidth() { return 400; }
public double getSampleHeight() { return 150; }
@Override
public void start(Stage primaryStage) throws Exception {
init(primaryStage);
primaryStage.show();
}
public static void main(String[] args) { launch(args); }
}
TEST
-------------------------------
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.PerspectiveCamera;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import javafx.scene.paint.ImagePattern;
import javafx.scene.shape.RectangleBuilder;
import javafx.scene.transform.Rotate;
import javafx.scene.transform.Translate;
import javafx.stage.Stage;
import javafx.util.Duration;
/**
* A sample that demonstrates an animated rotation of 3D cubes. When the
* application runs in standalone mode, the scene must be constructed with
* the depthBuffer argument set to true, and the root node must have depthTest
* set to true.
*
* @see javafx.scene.transform.Rotate
* @see javafx.scene.paint.Color
* @see javafx.scene.shape.RectangleBuilder
*/
public class CubeSample extends Application {
private Timeline animation;
private Timeline animation2;
private Boolean state = true;
private void init(Stage primaryStage) {
Group root = new Group();
primaryStage.setResizable(false);
primaryStage.setScene(new Scene(root, 400,150,true));
primaryStage.getScene().setCamera(new PerspectiveCamera());
root.getTransforms().addAll(
new Translate(400 / 2, 150 / 2),
new Rotate(180, Rotate.X_AXIS)
);
root.getChildren().add(create3dContent());
}
public Node create3dContent() {
final Cube c = new Cube(100,Color.BLACK,1);
animation = new Timeline();
animation.getKeyFrames().addAll(
new KeyFrame(Duration.ZERO,
new KeyValue(c.ry.angleProperty(), 0d)
),
new KeyFrame(Duration.seconds(1),
new KeyValue(c.ry.angleProperty(), 180d)
));
animation.setCycleCount(1);
c.setOnMousePressed(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
play();
}
});
animation2 = new Timeline();
animation2.getKeyFrames().addAll(
new KeyFrame(Duration.ZERO,
new KeyValue(c.ry.angleProperty(), 0d)
),
new KeyFrame(Duration.seconds(1),
new KeyValue(c.ry.angleProperty(), -180d)
));
animation2.setCycleCount(1);
c.setOnMousePressed(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
play();
}
});
return new Group(c);
}
public void play() {
if(state){
animation.play();
state=false;
}else{
animation2.play();
state=true;
}
}
@Override public void stop() {
animation.pause();
}
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 color, double shade) {
getTransforms().addAll(rz, ry, rx);
getChildren().addAll(
RectangleBuilder.create() // back face
.width(size).height(size)
.fill(new ImagePattern(
new Image("2.jpg"), 0, 0, 1, 1, true )
)
.translateX(-0.5*size)
.translateY(-0.5*size)
.translateZ(0.5*size)
.build(),
RectangleBuilder.create() // bottom face
.width(size).height(size)
.fill(color.deriveColor(0.0, 1.0, (1 - 0.4*shade), 1.0))
.translateX(-0.5*size)
.translateY(0)
.rotationAxis(Rotate.X_AXIS)
.rotate(90)
.build(),
RectangleBuilder.create() // right face
.width(size).height(size)
.fill(color.deriveColor(0.0, 1.0, (1 - 0.3*shade), 1.0))
.translateX(-1*size)
.translateY(-0.5*size)
.rotationAxis(Rotate.Y_AXIS)
.rotate(90)
.build(),
RectangleBuilder.create() // left face
.width(size).height(size)
.fill(color.deriveColor(0.0, 1.0, (1 - 0.2*shade), 1.0))
.translateX(0)
.translateY(-0.5*size)
.rotationAxis(Rotate.Y_AXIS)
.rotate(90)
.build(),
RectangleBuilder.create() // false top face
.width(size).height(size)
.fill(color.deriveColor(0.0, 1.0, (1 - 0.1*shade), 1.0))
.translateX(-0.5*size)
.translateY(-1*size)
.rotationAxis(Rotate.X_AXIS)
.rotate(90)
.build(),
RectangleBuilder.create() // top face
.width(size).height(size)
.fill(new ImagePattern(
new Image("1.jpg"), 0, 0, 1, 1, true
)
)
.translateX(-0.5*size)
.translateY(-0.5*size)
.translateZ(-0.5*size)
.build()
);
}
}
public double getSampleWidth() { return 400; }
public double getSampleHeight() { return 150; }
@Override
public void start(Stage primaryStage) throws Exception {
init(primaryStage);
primaryStage.show();
}
public static void main(String[] args) { launch(args); }
}