RotateTransition around a pivot? - java

I am using RotateTransiton to rotate a line, but it seems to rotate through center of the line. I would like to rotate it with pivot as one end of the line. How to do this?

The RotateTransition works by changing the rotate property, which - as you have observed - defines a rotation around the center of the Node.
If you want to rotate around a different point, define a Rotate transform, set its pivot, add it to the line's list of transforms, and use a Timeline to manipulate its angle.
Here's an example:
import javafx.animation.Animation;
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Line;
import javafx.scene.transform.Rotate;
import javafx.stage.Stage;
import javafx.util.Duration;
public class RotateLineAboutEnd extends Application {
#Override
public void start(Stage primaryStage) {
Line line = new Line(200, 200, 200, 350);
Pane pane = new Pane(line);
Rotate rotation = new Rotate();
rotation.pivotXProperty().bind(line.startXProperty());
rotation.pivotYProperty().bind(line.startYProperty());
line.getTransforms().add(rotation);
Timeline timeline = new Timeline(
new KeyFrame(Duration.ZERO, new KeyValue(rotation.angleProperty(), 0)),
new KeyFrame(Duration.seconds(1), new KeyValue(rotation.angleProperty(), 360)));
Button button = new Button("Rotate");
button.setOnAction(evt -> timeline.play());
button.disableProperty().bind(timeline.statusProperty().isEqualTo(Animation.Status.RUNNING));
HBox controls = new HBox(button);
controls.setAlignment(Pos.CENTER);
controls.setPadding(new Insets(12));
BorderPane root = new BorderPane(pane, null, null, controls, null);
Scene scene = new Scene(root, 400, 400);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}

(on JavaFX 2.2 and JavaFX 8)
The best way in my opinion is translating the layoutBounds of the node (which contains the pivot point) and translating the node itself the opposite way.
An example:
public class Main extends Application {
#Override
public void start(Stage primaryStage) throws Exception {
Group root = new Group();
primaryStage.setScene(new Scene(root, 140, 140));
Rectangle rect = new Rectangle(1, 1, 40, 40);
// comment movePivot to get the default rotation
movePivot(rect, -20, -20);
RotateTransition rt = new RotateTransition(Duration.seconds(4),rect);
rt.setToAngle(720);
rt.setCycleCount(Timeline.INDEFINITE);
rt.setAutoReverse(true);
rt.play();
primaryStage.show();
}
// this is the function you want
private void movePivot(Node node, double x, double y){
node.getTransforms().add(new Translate(-x,-y));
node.setTranslateX(x); node.setTranslateY(y);
}
public static void main(String[] args) {
launch(args);
}
}

Related

JavaFX: position text inside Group at the bottom of shape

I have simple code that draws a circle and prints Hello World! text. Currently the text is inside the circle, while I want it to be below it. Both circle and text are inside a Group.
import javafx.application.Application;
import javafx.geometry.Orientation;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.ScrollPane;
import javafx.scene.layout.FlowPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Arc;
import javafx.scene.shape.ArcType;
import javafx.scene.shape.Circle;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class CircleArc extends Application {
#Override
public void start(Stage applicationStage) {
ScrollPane scrollPane = new ScrollPane();
scrollPane.setFitToWidth(true);
FlowPane flowPane = new FlowPane();
flowPane.setOrientation(Orientation.HORIZONTAL);
Circle circle = new Circle(100);
circle.setFill(Color.GREEN);
double rate = 0.6;
Arc arc = new Arc(0, 0, 100, 100,
0, -360*rate);
arc.setType(ArcType.ROUND);
arc.setFill(Color.GRAY);
Text text = new Text("Hello World!");
text.setLayoutX(text.getLayoutX() / 2);
text.setLayoutY(text.getLayoutY());
Group group = new Group();
group.getChildren().add(circle);
group.getChildren().add(arc);
group.getChildren().add(text);
flowPane.getChildren().add(group);
scrollPane.setContent(flowPane);
Scene scene = new Scene(scrollPane);
applicationStage.setScene(scene);
applicationStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
It currently looks like this:
However I want it to look like this:
I tried using the setLayout methods but they didn't work.
EDIT:
Turned out the simple
text.setLayoutX(text.getLayoutX() - 50);
text.setLayoutY(text.getLayoutY() + 120);
worked well enough.
Use a Label. Set the circle as the graphic, position the text underneath.
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage stage) {
stage.setTitle("Centered Text Under Graphic");
Circle circle = new Circle(100);
circle.setFill(Color.GREEN);
double rate = 0.6;
Arc arc = new Arc(0, 0, 100, 100,
0, -360 * rate);
arc.setType(ArcType.ROUND);
arc.setFill(Color.GRAY);
Group circles = new Group(circle, arc);
Label label = new Label("Hello World!", circles);
label.setContentDisplay(ContentDisplay.TOP);
Pane p = new Pane(label);
Scene scene = new Scene(p);
stage.setScene(scene);
stage.show();
}
}
That will handle keeping the text centered if it changes.

How should I proceed in making my player rectangle jump?

I'm just looking to make my red rectangle jump up when you press that jump button. I can't really seem to find any thing like an animation or even a go up, wait a certain amount of time and then come back down.
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.paint.Color;
import javafx.application.Application;
import javafx.scene.*;
import javafx.scene.control.Button;
import javafx.event.EventHandler;
import javafx.event.ActionEvent;
import javafx.animation.PathTransition;
import javafx.scene.shape.*;
import javafx.util.Duration;
public class GUIPractice extends Application{
public static void main (String [] args)
{
launch(args);
}
public void start (Stage primaryStage)
{
Rectangle screen = new Rectangle(20, 20, 986, 500);
Button JumpBtn = new Button("Jump");
JumpBtn.setLayoutX(410);
JumpBtn.setLayoutY(530);
JumpBtn.setMinWidth(200);
JumpBtn.setMinHeight(100);
Rectangle player = new Rectangle(450, 420, 50, 100);
player.setFill(Color.RED);
Path path = new Path();
I believe right here below is where the jumping would go but the only thing I could figure out is how to get the rectangle to move up on the screen and not move back down.
JumpBtn.setOnAction(new EventHandler<ActionEvent>()
{
public void handle(ActionEvent e) {
player.setTranslateY(-40);
}
});
Group root = new Group(screen, JumpBtn, player);
Scene scene = new Scene(root, 1024, 768);
scene.setFill(Color.GREY);
primaryStage.setTitle("GUIPractice");
primaryStage.setScene(scene);
primaryStage.show();
}
}
Use a animation such as timeline to move the Node, e.g.
double ty = player.getTranslateY();
// quadratic interpolation to simulate gravity
Interpolator interpolator = new Interpolator() {
#Override
protected double curve​(double t) {
return t * (2 - t);
}
};
Timeline timeline = new Timeline(new KeyFrame(Duration.ZERO,
new KeyValue(player.translateYProperty(), ty, interpolator)),
new KeyFrame(Duration.seconds(1),
new KeyValue(player.translateYProperty(), ty-40, interpolator)));
// play forward once, then play backward once
timeline.setCycleCount(2);
timeline.setAutoReverse(true);
JumpBtn.setDisable(true);
timeline.setOnFinished(evt -> JumpBtn.setDisable(false));
timeline.play();

JavaFX rectangle rotation pivot from point other than top left

I have applied a JavaFx Rotation to a rectangle using the constructor that allows you to set the pivot
new Rotate(45, 15, 15)
This rotates the rectangle but it rotates around the top left corner at 15,15 on the AnchorPane in which it is placed. Is it possible to rotate a rectangle around a point that acts like the center of a circle and the rectangle rotates around the circumfrence. Like the rectangle is a piece of tread on a tire that rotates around the central pivot. Thanks heaps.
This works for me, I am not sure what you are doing though.
package helloworld;
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import javafx.scene.shape.Ellipse;
import javafx.scene.shape.Rectangle;
import javafx.scene.transform.Rotate;
import javafx.stage.Stage;
import java.time.Duration;
/**
* Created by Matt on 25/08/16.
*/
public class RotatingARectangle extends Application {
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Hello World!");
Group root = new Group();
Rectangle rect = new Rectangle(190, 395, 20, 5);
rect.setFill(Color.BLUE);
Rotate rot = new Rotate(0, 200, 200);
rect.getTransforms().add(rot);
Ellipse path = new Ellipse(200, 200, 200, 200);
path.setStroke(Color.RED);
path.setFill(null);
root.getChildren().add(rect);
root.getChildren().add(path);
Scene scene = new Scene(root, 400, 400);
primaryStage.setScene(scene);
primaryStage.show();
Timeline line = new Timeline(30);
KeyFrame key1 = new KeyFrame(
new javafx.util.Duration(0),
new KeyValue(rot.angleProperty(), 0 )
);
KeyFrame key2 = new KeyFrame(
new javafx.util.Duration(1000),
new KeyValue(rot.angleProperty(), 360 )
);
line.getKeyFrames().addAll(key1, key2);
scene.addEventHandler(MouseEvent.MOUSE_CLICKED, evt->{
line.playFromStart();
});
}
}
I set the pivot to be the center of the elipse, then use a timeline to change the angle from 0 to 360.

how to translate pop up windows in javaFX?

Is there any way to translate somethings like pop up windows(that are not Node) in java FX ?
for example fade transition , translate transition or any timeline transition .... .
Thank's
Create a property and use a Timeline to "animate" the property. Register a listener with the property and update the window when its value changes.
For example:
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.util.Duration;
public class TranslateWindowExample extends Application {
#Override
public void start(Stage primaryStage) {
Button moveButton = new Button("Move");
moveButton.setOnAction(event -> {
double currentX = primaryStage.getX() ;
DoubleProperty x = new SimpleDoubleProperty(currentX);
x.addListener((obs, oldX, newX) -> primaryStage.setX(newX.doubleValue()));
KeyFrame keyFrame = new KeyFrame(Duration.seconds(1), new KeyValue(x, currentX + 100));
Timeline animation = new Timeline(keyFrame);
animation.play();
});
StackPane root = new StackPane(moveButton);
Scene scene = new Scene(root, 250, 150);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Can you give an example of what do you call popup window ?
For instance, you can create a new stage on the top of your main stage.
You can also use some Tooltip class to add some text over the top of your window.
If you want more classical windows, you should have a look to Alert class.
Anthony

JFX8 Mixed 2D and 3D, Scene Subscene incorrect drawing 3ds-file

Have some problems with using mixed 2D and 3D when i'm implementing a SubScene to my Scene.
You can see the problem by clicking the links. Im using the Interactivemesh-Modelimporter.
http://s3.postimg.org/nsef8o1f3/without_Subscene.png
http://s3.postimg.org/y16wehpgv/with_Subscene.png
import com.interactivemesh.jfx.importer.ImportException;
import com.interactivemesh.jfx.importer.tds.TdsModelImporter;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Camera;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.PerspectiveCamera;
import javafx.scene.Scene;
import javafx.scene.SubScene;
import javafx.scene.control.Button;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.scene.transform.Rotate;
import javafx.scene.transform.Translate;
import javafx.stage.Stage;
public class JavaFX7 extends Application {
HBox hBoxControls;
Group model3D;
double y_Axis;
double x_Axis;
double rotate;
public Rotate cameraXRotate;
public Rotate cameraYRotate;
public Translate cameraPosition2;
Camera camera;
public void controls() {
...
...
}
public void model () {
//
// importing 3ds Modell
//
TdsModelImporter myModel = new TdsModelImporter();
try {
String path = "C:/Users/Corvin/Downloads/DUC916_L.3DS/DUC916_L.3DS";
myModel.read(path);
}
catch (ImportException e)
{
System.out.println("Error importing 3ds model: "+e.getMessage());
return;
}
final Node[] myMesh = myModel.getImport();
myModel.close();
model3D = new Group(myMesh);
}
public static void main(String[] args) {
launch();
}
#Override
public void start(Stage stage) throws Exception {
y_Axis = 0.0;
controls();
model();
Camera camera = new PerspectiveCamera();
//-------------------
cameraXRotate = new Rotate(-5,Rotate.X_AXIS);
cameraYRotate = new Rotate(-50,Rotate.Y_AXIS);
cameraPosition2 = new Translate(0,-1900,-14000);
camera.getTransforms().add(cameraXRotate);
camera.getTransforms().add(cameraYRotate);
camera.getTransforms().add(cameraPosition2);
//-------------------
Pane sub = new Pane();
AnchorPane root = new AnchorPane();
AnchorPane.setRightAnchor(hBoxControls, 10.0);
AnchorPane.setBottomAnchor(hBoxControls, 10.0);
root.getChildren().add(hBoxControls);
// ---------------------------------------
Scene scene = new Scene(root, 800, 600, true);
SubScene subScene = new SubScene (sub, 800, 600);
subScene.setCamera(camera);
sub.getChildren().add(model3D);
root.getChildren().add(subScene);
stage.setScene(scene);
stage.show();
}
}
You are using 3D models.
That means your subscene needs a deapthBuffer not your primary scene. Thats why it works on the primary scene.
Change
SubScene subScene = new SubScene (sub, 800, 600);
to
SubScene subScene = new SubScene(sub, 800, 600,true, SceneAntialiasing.DISABLED);
(SceneAntialiasing.DISABLED or SceneAntialiasing.BALANCED)

Categories