Relocating a node JavaFX - java

I'm creating a maze game where there will be multiple 'chasers' following a player round a maze, to do this I need to relocate the chasers however i've ran into an issue using .relocate() it only moves it once ,I understand it does this because it shifts the whole coordinates axis itself rather than just the nodes coordinates.
I cannot use chaser.setCentreX() unfortunately to move it as I will be dynamically creating the nodes and will all have the same name 'chaser' hence why I am using getChildren() to get the coordinates. To differentiate between the multiple chasers I will use .setId() .getId() so that I can access the LayoutBounds of a specific node. For example if there were three chasers the first one would be called 'chaser' with and Id of 1 then the next with and Id of 2 etc..
Is there an alternative way of relocating a node without having this issue? , the code below demonstrates the problem.
package sample;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
import javafx.util.Duration;
public class Main extends Application {
#Override
public void start(Stage primaryStage) throws Exception{
Pane pane=new Pane();
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(pane, 600, 600));
primaryStage.show();
Circle chaser=new Circle();
chaser.setRadius(12);
chaser.setCenterX(100);
chaser.setCenterY(50);
chaser.setFill(Color.YELLOW);
pane.getChildren().add(chaser);
Timeline timeline = new Timeline(new KeyFrame(Duration.millis(30), new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
System.out.println("running");
pane.getChildren().get(0).relocate(chaser.getCenterX()+400,chaser.getCenterY());
}
}));
timeline.setCycleCount(Timeline.INDEFINITE);
timeline.play();
}
public static void main(String[] args) {
launch(args);
}
}

What relocate does is assign the layoutX and layoutY properties. Those are completely independent from centerX and centerY. Since the latter properties are never updated, the target location remains constant regardless of how often you execute relocate(chaser.getCenterX()+400,chaser.getCenterY()); on chaser.
To see updates, you need to refer to an value that is updated when updating the location, e.g.
chaser.relocate(chaser.getLayoutX() + 400, chaser.getLayoutY());
or
chaser.setCenterX(chaser.getCenterX() + 400);
// chaser.setCenterY(chaser.getCenterY());
or
chaser.setTranslateX(chaser.getTranslateX() + 400);
// chaser.setTranslateY(chaser.getTranslateY());
As for using ids: That seems completely unnecessary. You could keep track of the chasers without using the CSS id by simply storing them in a suitable data structure. If those are the only children of the parent, you don't even need an extra datastructure, since every chaser would be stored in the children list in that case...

Related

How to achive the effect of UNDECORATED + UTILITY in javafx? [duplicate]

I need remove my javafx app from the taskbar. I tried StageStyle.UTILITY. This is works but I need both UNDECORATED and UTILITY stage styles or another solvings.
Thank you for your replies.
Sorry you've been waiting so long for some sort of an answer on this, the following is mainly for people who come to this in the future hoping to discover a way of achieving this.
Let me start of by saying I wouldn't consider the following a solution but more of a workaround.
Assigning more than one initStyle to a stage is not possible however hiding the application from the task-bar and assigning an initStyle other than utility to the stage that is shown is.
To achieve this one must create two stages, the stage they want the user to see, and an another stage that will be considered the parent of the main stage and will be of initStyle.UTILITY this will prevent the icon from showing in the task-bar.
Below you can see the hello world example from oracles documentation modified to allow for an undecorated window with no icon (Note if one wanted to achieve a transparent/decorated window they could do so by changing the style of mainStage).
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
public class MultipleStageStyles extends Application {
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) {
primaryStage.initStyle(StageStyle.UTILITY);
primaryStage.setOpacity(0);
primaryStage.setHeight(0);
primaryStage.setWidth(0);
primaryStage.show();
Stage mainStage = new Stage();
mainStage.initOwner(primaryStage);
mainStage.initStyle(StageStyle.UNDECORATED);
Button btn = new Button();
btn.setText("Say 'Hello World'");
btn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
System.out.println("Hello World!");
}
});
StackPane root = new StackPane();
root.getChildren().add(btn);
mainStage.setScene(new Scene(root, 300, 250));
mainStage.show();
}
}

javafx - how to create a loop of graphic components

In my javafx application I want to create a scene where I can show the usernames of all the users in my database.
Precisely, I want to show a list of labels where every label get a username.
(the number of labels depend on the number of users).
Note: I can do this in java with a list and a foreach loop, but this is the first time that I work with javafx and I want to know how to create a loop of graphic component.
Thanks.
Here are a couple of alternatives, one sample is just looping and adding new labels to the children of a layout pane, the other is using the in-built ListView component. There are other alternatives of course. Which you choose to use will depend upon the functionality you need to achieve.
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class UserDisplay extends Application {
#Override public void start(Stage stage) {
String[] users = { "Huey", "Dewey", "Louie" };
VBox layout = new VBox(10);
// ALTERNATIVE 1: add labels in a loop.
for (String user: users) {
Label userLabel = new Label(user);
layout.getChildren().add(userLabel);
}
// ALTERNATIVE 2: use the built-in ListView component.
ListView<String> listView = new ListView<>(
FXCollections.observableArrayList(users)
);
layout.getChildren().add(listView);
layout.setPadding(new Insets(10));
layout.setPrefSize(100,200);
stage.setScene(new Scene(layout));
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}

Horizontal Scrolling Listener

I feel like I have searched half the Web and found no solution...
I have a java application displaying a Map(different countries etc.).
currently you are able to scroll down and up using your mouse wheel...
I want it so, that you are able to scroll sideways (horizontally).
All I need is a Listener (in Swing or Javafx does not matter) triggering whenever the mousewheel gets tilted, without the need for a focus of the map (hovering with your mouse should be enough the windows should still be focused) and without any visible scrollbars.
Using the following code every time you scroll sideways a message gets printed out...
import javafx.application.Application;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.event.EventType;
import javafx.scene.Scene;
import javafx.scene.input.ScrollEvent;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class Main extends Application {
#Override
public void start(Stage primaryStage) {
try {
BorderPane root = new BorderPane();
Scene scene = new Scene(root,400,400);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
scene.setOnScroll(new EventHandler<ScrollEvent>(){
#Override
public void handle(ScrollEvent event) {
System.out.println("Scroll:" + event.getDeltaX());
}
});
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
One thing to consider:
Apparently when embedding a JFXPanel into a JFrame the sideway scrolling event is not getting passed.

Scaling down the height of a rectangle to fit in a window in javafx

I'm currently working on a project which relies on the representation of file sizes through rectangles. What I have been stuck on for quite a while however, was scaling of the height to a size that could fit inside of my Stage. This is a crucial part of the project as it allows the user to visually compare the sizes of the files they are viewing from any directory on their machine.
EDIT: I tried adding an image but I haven't got enough reputation yet :(.
http://tinypic.com/r/2e56pol/8
Why don't you just bind the height of the rectangle to the scene's using:
rectangle.heightProperty().bind(scene.heightProperty());
A complete example :
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
public class DemoTabPane extends Application {
#Override
public void start(Stage primaryStage) throws Exception {
Rectangle rectangle = new Rectangle(20,20,200,200);
rectangle.setStroke(Color.BLACK);
Scene scene = new Scene(new HBox(rectangle), 100, 100);
rectangle.heightProperty().bind(scene.heightProperty());
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}

JavaFX Beans Binding suddenly stops working

I use JavaFX NumberBindings in order to calculate certain values. Initially everything works as expected. After a rather small amount of time, however, the binding just stops working. I don't receive an Exception, either.
I've tried several bindings, as well as high- and low-level approaches. Even the calculation itself (when overridden) just stops and isn't called anymore. I've also updated to the latest JDK (1.8.0_05) and rebuilt/restarted everything.
The following Minimal Working Example illustrates the problem. It should System.out.println the current width of the main window to STDOUT. After resizing the window for about 10 seconds, the output simply stops. I've also tried to bind the resulting property to a JavaFX control, in order to ensure the Property's continued usage, but that was of no avail. I believe I'm missing some very basic behaviour of the Property/Bindings here, Google doesn't seem to know that behaviour at all.
import javafx.application.Application;
import javafx.beans.binding.NumberBinding;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class BindingsProblem extends Application {
#Override
public void start(Stage primaryStage) {
// Initialization...
StackPane root = new StackPane();
Scene scene = new Scene(root, 300, 250);
primaryStage.setScene(scene);
primaryStage.show();
// Binding - The problem occurrs here!
NumberBinding currentWidthPlusTen = primaryStage.widthProperty().add(10);
IntegerProperty boundNumberProperty = new SimpleIntegerProperty();
boundNumberProperty.bind(currentWidthPlusTen);
boundNumberProperty.addListener(new ChangeListener<Number>() {
#Override
public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
System.out.println(newValue.toString());
}
});
}
public static void main(String[] args) {
launch(args);
}
}
The binding uses a WeakListener to observe the value of currentWidthPlusTen. Since you don't keep a reference to the boundNumberProperty, it is eligible for garbage collection as soon as the start(...) method exits. When the garbage collector kicks in, the reference is lost entirely and the binding no longer works.
To see this directly, add the line
root.setOnMousePressed( event -> System.gc());
to the start(...) method. You can force the listener to "stop working" by clicking on the window.
Obviously, that's not what you want: the fix is to retain the reference to boundNumberProperty after start(...) exits. For example:
import javafx.application.Application;
import javafx.beans.binding.NumberBinding;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class BindingsProblem extends Application {
IntegerProperty boundNumberProperty;
#Override
public void start(Stage primaryStage) {
// Initialization...
StackPane root = new StackPane();
Scene scene = new Scene(root, 300, 250);
primaryStage.setScene(scene);
primaryStage.show();
// Binding - The problem occurrs here!
NumberBinding currentWidthPlusTen = primaryStage.widthProperty()
.add(10);
boundNumberProperty = new SimpleIntegerProperty();
boundNumberProperty.bind(currentWidthPlusTen);
boundNumberProperty.addListener(new ChangeListener<Number>() {
#Override
public void changed(ObservableValue<? extends Number> observable,
Number oldValue, Number newValue) {
System.out.println(newValue.toString());
}
});
}
public static void main(String[] args) {
launch(args);
}
}
Update
Anyone running into this issue might also want to look at Tomas Mikula's ReactFX, which provides a cleaner workaround for this (at the expense of using a third-party library, which you would need to spend some time learning). Tomas explains this issue and how ReactFX resolves it in this blog and the subsequent post.

Categories

Resources