How to play the TranslateTransition continuously with no stop - java

I have a Text that contains a String of 7 lines. I want to put this Text in a panel width 500 and height 70 This text should smoothly move slowly from the bottom up and play again and again without stopping.
I did the following code.
But the text only once playing and floating movement is done with small jumps rather smoothly.
How I can do smooth motion and play it again and again without ever stop?
public class TextSh extends Application {
#Override
public void start(Stage primaryStage) {
String message =" Here is the text of seven lines";
Text textRef = TextBuilder.create()
.layoutY(100)
.textOrigin(VPos.TOP)
.textAlignment(TextAlignment.CENTER)
.wrappingWidth(400)
.text(message)
/*.fill(Color.rgb(187, 195, 107))*/
.fill(Color.OLIVE)
.font(Font.font("SansSerif", FontWeight.BOLD, 18))
.build();
TranslateTransition transTransition = TranslateTransitionBuilder.create()
.duration(new Duration(75000))
.node(textRef)
.toY(-1640)
.interpolator(Interpolator.LINEAR)
.cycleCount(Timeline.INDEFINITE)
.autoReverse(true)
.build();
StackPane root = new StackPane();
root.getChildren().add(textRef);
StackPane r = new StackPane();
r.getChildren().add(root);
Scene scene = new Scene(r, 500, 70);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
transTransition.play();
}
public static void main(String[] args) {
launch(args);
}
}

Related

Mouse coordination just work in short specific range javafx [duplicate]

This question already has answers here:
How to get location of mouse in JavaFX?
(3 answers)
Closed 11 months ago.
I'm new to javafx.
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage stage) throws Exception {
Label mouseCoordination = new Label("?,?");
mouseCoordination.setOnMouseMoved(e ->
mouseCoordination.setText(e.getX()+","+ e.getY())
);
StackPane root = new StackPane();
root.setPrefHeight(100);
root.setPrefWidth(400);
root.getChildren().add(mouseCoordination);
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
Here i tried to get mouse coordination, So i wrote this but it just work in short range like (0,0) to (48,17) and it doesn't update label after that. i actually wrote that in base of a question in site
That is because the label isn't width × height and you added the mouse listener to the label; if you add the listener to root, it will work the way you wanted.
#Override
public void start(Stage stage) throws Exception {
Label mouseCoordination = new Label("?,?");
StackPane root = new StackPane();
root.setPrefWidth(400);
root.setPrefHeight(100);
root.getChildren().add(mouseCoordination);
root.setOnMouseMoved(e -> {
mouseCoordination.setText(e.getX() + "," + e.getY());
});
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
Just to be clear, you added the mouse listener to the label which only has a width of 30 and a height of 40, for example; the size depends on the text being displayed and how much space it takes up. The mouse listener is only called if your mouse is on the label; however, since you wanted it to show your current mouse position, it wouldn't work unless the label had the same width and height as the window.

Automatic (unwanted) JavaFX window resize on Debian?

I have noticed the following JavaFX behavior when implementing a JavaFX Application as follows:
public class Test extends Application {
private static Stage innerStage;
public static void main(String[] args) {
launch(args);
printSize(innerStage, "after closing the window");
}
#Override
public void start(Stage primaryStage) {
primaryStage.setWidth(300);
primaryStage.setHeight(250);
primaryStage.setTitle("Hello World!");
Button btn = new Button();
btn.setText("Say 'Hello World'");
btn.setOnAction(event -> System.out.println("Hello World!"));
StackPane root = new StackPane();
root.getChildren().add(btn);
primaryStage.setScene(new Scene(root, 250, 200));
primaryStage.show();
primaryStage.setResizable(false);
innerStage = primaryStage;
printSize(primaryStage, "at start-up");
}
private static void printSize(Stage stage, String phase) {
System.out.println(String.format("Width %s: %.1f", phase, stage.getWidth()));
System.out.println(String.format("Height %s: %.1f", phase, stage.getHeight()));
}
}
First print of the stage size gives:
Width at start-up: 300.0
Height at start-up: 250.0
while the second one gives:
Width after closing the window: 312.0
Height after closing the window: 284.0
This happens on Debian GNU/Linux 9.12 (stretch) but not on Windows 10.
Could someone explain what is causing it and let me know if there's a workaround for this such that the stage (window) does not change size by itself?
Thank you
later edit: from what I've seen, the re-size happens right after the start method of the JavaFX Application is done.

How to make the Button appear on the right side of a TitledPane JavaFX

I have managed to place a Button inside a TitledPane using setGraphic but I want it to be located on the right side; here is the code:
#Override
public void start(Stage primaryStage) {
Accordion acordion = new Accordion();
TitledPane tp1 = new TitledPane();
Button b1 = new Button();
b1.setText("X");
tp1.setGraphic(b1);
acordion.getPanes().addAll(tp1);
Scene scene = new Scene(acordion, 300, 250);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
The Button should be located on the right but I do not know how to do it
Set the alignment property of TitledPane to CENTER_RIGHT:
tp1.setAlignment(Pos.CENTER_RIGHT);

Padding button and rectangle

My button is located under the rectangle. It is currently like in the picture on the left. I want to pad the button like in the picture on the right.
How can I do that? Thanks for reading.
Here's my code.
public class Rect extends Application {
private Button btn1 = new Button("Rotate");
private Rectangle r1 = new Rectangle(50, 100);
#Override
public void start(Stage primaryStage) throws Exception {
GridPane gridPane = new GridPane();
r1.setStroke(Color.BLACK);
r1.setFill(Color.WHITE);
gridPane.add(r1, 0, 0);
gridPane.add(btn1, 0, 1);
gridPane.setAlignment(Pos.CENTER);
btn1.setAlignment(Pos.BOTTOM_CENTER);
Scene scene = new Scene(gridPane,200,200);
primaryStage.setTitle("RotateRectangleFX");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
At the moment you are doing this:
gridPane.setAlignment(Pos.CENTER);
btn1.setAlignment(Pos.BOTTOM_CENTER);
You should be setting the alignment of r1 instead like follows:
r1.setAlignment(Pos.CENTER);
btn1.setAlignment(Pos.BOTTOM_CENTER);

Is the extra top space in a stage included in the size of the scene?

If I want a scene to be 500 by 500... does that make the stage 550 by 550 or does it make the scene 450 by 450 and the stage 500 by 500? What is the width and length of the extra title bar space above the scene that include the close, restore, and minimize buttons?
No, the extra space on top of a stage is not included in the size of the Scene.
If you want to know the exact size, it is platform dependent. A very simple example to verify is to create a Scene of 500 x 500 and set the Stage at X=0, Y=0 and check the mouse co-ordinates to measure the size.
public class MyStageSize extends Application {
#Override
public void start(Stage stage) {
Label label = new Label();
VBox root = new VBox(label);
root.setAlignment(Pos.CENTER);
Scene scene = new Scene(root, 500, 500);
stage.setScene(scene);
stage.setX(0);
stage.setY(0);
stage.show();
scene.setOnMouseMoved(e -> label.setText(String.valueOf("X = " + e.getScreenX() + "\nY = " + e.getScreenY())));
}
public static void main(String[] args) {
launch(args);
}
}

Categories