How to pass stackPane reference to another Class method - java
Im working on some project of traffic simulation, and have a problem with call a method in another class,
public class Grafika extends Application {
private StackPane stackPane = new StackPane();
public void start(Stage primaryStage) {
Scene scene = new Scene(stackPane, 731, 779);
primaryStage.setScene(scene);
primaryStage.setTitle("PN");
primaryStage.show();
Image image = new Image("file:plN_v3.png");
ImageView iv = new ImageView();
iv.setImage(image);
stackPane.getChildren().add(iv);
firstPath(stackPane);}
public void setStackPane() {
this.stackPane = stackPane;
}
this is my stage with stackPane where im adding moving objects
public void firstPath(StackPane stackPane) {
Circle car = new Circle(5, 5, 5);
setCar(car);
stackPane.getChildren().add(car);
Path path = new Path();
path.getElements().add(new MoveTo(380, -105));
path.getElements().add(new CubicCurveTo(30, -140, 100, -300, -20, -140)); //first for car
path.getElements().add(new LineTo(-315, 400));
pathT(path, car);
}
public void pathT(Object path, Object car) {
PathTransition pathT = new PathTransition();
pathT.setDuration(Duration.millis(15000));
pathT.setPath((Shape) path);
pathT.setNode((Node) car);
pathT.setOrientation
(PathTransition.OrientationType.ORTHOGONAL_TO_TANGENT);
pathT.setAutoReverse(true);
pathT.play();
}
there is a example of path to move a "car".
public class App{
public static void main(String[] args)
{
Grafika.launch(Grafika.class, args);
Grafika g = new Grafika();
StackPane stackPane = new StackPane();
g.setStackPane();
g.firstPath(stackPane);
} }
And when im calling firstPath in class Grafika it's seems fine, but problem is when i try to call this method in another class
Related
JavaFx: ImageView disappears after moving through the scene
I have a code written using javafx and java 11. As I showed in the below code snippet, when I move my ImageView to right of the scene, The ImageView disappears. At first: and then after moving it using arrow keys with the help of scene event listener: then: and finally: I don't use fxml. Here is my demo which has the problem too. public class HelloApplication extends Application { public String fetchResource(String path) { return Objects.requireNonNull(getClass().getResource(path)).toString(); } #Override public void start(Stage stage) throws IOException { Rectangle r = new Rectangle(100, 100); ImageView spaceShip = new ImageView(fetchResource("spaceShip.png")); spaceShip.setFitHeight(100); spaceShip.setFitWidth(100); spaceShip.setX(0); spaceShip.setY(0); EventHandler<KeyEvent> keyListener = event -> { if (event.getCode() == KeyCode.RIGHT) { spaceShip.setX(spaceShip.getX() + 20); } }; Group game = new Group(spaceShip); Scene scene = new Scene(game, 1840, 1080); scene.setOnKeyPressed(keyListener); stage.setTitle("Hello!"); stage.setScene(scene); stage.show(); } public static void main(String[] args) { launch(); } } I should mention that I create a rectangle and check the scenario with that, And there was not any problem. I think there is some problem with ImageView.
I couldn't reproduce the issue with your code, but I could with SceneBuilder. You may get the desired result if you wrap the Group in a Pane (at least it works in SceneBuilder). Group game = new Group(spaceShip); Pane pane = new Pane(game); Scene scene = new Scene(pane, 1840, 1080); stage.setScene(scene); stage.show();
Can't add multiple of same rectangle
I want to make multiple of the same rectangle on my borderPane to make walls for the game. Each wall is going to look the same and be the same size. I know the image works and that there are no other errors. I use the following code to add the rectangles: public class Antz extends Application{ public BorderPane borderPane; public Scene scene; public Image wallImage = new Image("/recources/images/walls.png"); public Rectangle wall = new Rectangle(); public int[][] walls = {{1,0,0,0,0,1,1,1,0,1,0,0,0,0,0,1,0}, {1,1,1,1,0,1,0,0,0,1,0,1,1,1,0,1,0}, {0,1,0,1,0,0,0,1,0,1,0,0,0,1,0,0,0}, {0,1,0,1,1,1,0,1,0,1,1,1,0,1,0,1,0}, {0,0,0,0,1,0,0,1,0,0,0,1,0,1,1,1,0}, {1,0,1,1,1,1,0,1,1,1,1,1,0,1,0,0,0}, {1,0,1,0,0,1,0,0,0,0,0,1,0,0,0,1,1}, {1,0,1,1,0,1,0,1,0,1,0,0,0,1,0,0,1}, {1,0,0,0,0,0,0,1,1,1,0,0,1,1,1,0,1}, {0,0,1,0,1,1,0,1,0,1,1,0,1,0,0,0,0}, {1,1,1,0,1,0,0,0,0,0,1,0,1,1,0,1,0}, {1,0,0,0,0,0,1,1,1,0,1,0,0,1,1,1,0}, {1,0,1,0,1,1,1,0,1,1,1,1,0,0,0,1,0}, {0,0,1,0,0,0,0,0,0,0,0,1,0,1,0,0,0}, {1,0,1,1,1,1,0,1,1,1,0,1,0,1,1,0,1}, {0,0,1,0,0,0,0,0,0,1,0,0,0,1,0,0,0}}; public static void main(String[] args){ launch(args); } #Override public void start(Stage primaryStage) throws Exception { buildWindows(); buildWalls(); primaryStage.setScene(scene); primaryStage.setTitle("Formicidae"); primaryStage.show(); primaryStage.setMinHeight(550); primaryStage.setMinWidth(700); primaryStage.setFullScreenExitHint(""); primaryStage.setFullScreenExitKeyCombination(KeyCombination.NO_MATCH); } public void buildWindows() { borderPane = new BorderPane(); borderPane.setStyle("-fx-background-color: #000000;"); scene = new Scene(borderPane, 700, 550); } public void buildWalls(){ wall = new Rectangle(wallImage.getWidth(), wallImage.getHeight()); wall.setFill(new ImagePattern(wallImage)); for(int i = 0;i<walls.length;i++){ for(int j = 0;j<walls[i].length;j++){ if(walls[i][j]==1){ wall.setX(j*20); wall.setY(i*20); borderPane.getChildren().add(wall); } } } } } I get this error when it is ran: Caused by: java.lang.IllegalArgumentException: Children: duplicate children added: parent = BorderPane#4a17c25d[styleClass=root]
Trying to add the same Node twice in the Pane object is not a correct behavior. So, you have to create new one every time adding object on a pane object. public void buildWalls(){ wall.setFill(new ImagePattern(wallImage)); for(int i = 0;i<walls.length;i++){ for(int j = 0;j<walls[i].length;j++){ if(walls[i][j]==1){ wall = new Rectangle(wallImage.getWidth(), wallImage.getHeight()); wall.setX(j*20); wall.setY(i*20); borderPane.getChildren().add(wall); } } } } Well, I tested the program with some image from google. I don't know what you are supposed to do with your game but here is the output image.
changing scene in javafx
what's the matter with this code? i'm relly confused!! i wanted to change my scene in main stage. public class SignInController { #FXML TextField SignInPassword; #FXML TextField SignInUsername; #FXML CheckBox RememberMe; public void signUpScene(MouseEvent mouseEvent) throws IOException { Timeline timeline = new Timeline(); Scene SignUpScene = new Scene(FXMLLoader.load(getClass().getResource("sign up.fxml")),700,700); Main.pstage.setScene(SignUpScene); timeline.getKeyFrames().addAll( new KeyFrame(Duration.ZERO,new KeyValue(SignUpScene.getWidth(),0.0 )), new KeyFrame(Duration.millis(1000.0d),new KeyValue(SignUpScene.getWidth(),700.0 ) ) ); timeline.play(); } }
If you want to animate the width of the stage holding your new scene, you can use a Transition: public void signUpScene(MouseEvent mouseEvent) throws IOException { Scene SignUpScene = new Scene(FXMLLoader.load(getClass().getResource("sign up.fxml")),700,700); Main.pstage.setScene(SignUpScene); Rectangle clip = new Rectangle(0, 700); Transition animateStage = new Transition() { { setCycleDuration(Duration.millis(1000)); } #Override protected void interpolate(double t) { Main.pstage.setWidth(t * 700.0); } }; animateStage.play(); } } Maybe a better approach would be to gradually reveal the new scene using a clip: public void signUpScene(MouseEvent mouseEvent) throws IOException { Parent root = FXMLLoader.load(getClass().getResource("sign up.fxml")); Scene SignUpScene = new Scene(root,700,700); Main.pstage.setScene(SignUpScene); Rectangle clip = new Rectangle(0, 700); Timeline animate = new Timeline( new KeyFrame(Duration.millis(1000), new KeyValue(clip.widthProperty(), 700.0)); root.setClip(clip); // when animation finishes, remove clip: animate.setOnFinished(e -> root.setClip(null)); animate.play(); } }
JavaFX switching scenes
I've been trying to make my application to switch between scenes. Here is a copy of part of the code. The credits scene simply has a back button which should return me to the main scene. When I try to click on credits button on main scene it is becoming white a white screen. I believe that there is a better way to solve this problem could you give me some advices ? public class Application { public static void main(String[] args) { javafx.application.Application.launch(GUI.class); } } public class GUI extends Application { #Override public void start(Stage primaryStage) { Scene mainScene, creditsScene = null; mainScene = getMainScene(primaryStage, creditsScene); creditsScene = getCreditsScene(primaryStage, mainScene); primaryStage.setTitle("Test application"); primaryStage.setScene(mainScene); primaryStage.show(); } private Scene getMainScene(Stage primaryStage, Scene creditsScene) { final Button credits = new Button("Credits"); credits.setOnAction((ActionEvent e) -> { primaryStage.close(); primaryStage.setScene(creditsScene); primaryStage.show(); }); VBox x = new VBox(50); x.setAlignment(Pos.CENTER); x.getChildren().addAll( run, displayInfo, label1, displayInfo, textField, submitName, credits, exit); //scene size Scene scene = new Scene(x, 650, 900); return scene; } private Scene getCreditsScene(Stage primaryStage, Scene main) { final Button back = new Button("Back"); back.setOnAction((ActionEvent e) -> { primaryStage.setScene(main); }); VBox x = new VBox(50); x.getChildren().addAll(back); Scene credits = new Scene(x, 650, 900); return credits; }
Try to switch order of strings: mainScene = getMainScene(primaryStage, creditsScene); creditsScene = getCreditsScene(primaryStage, mainScene); here you pass to getMainScene null.
JavaFX change stage size from outside the constructor
I have something like public class MyClass extends Application { public void start(Stage stage) { MyModel model = new MyModel(); MyController controller = new MyController(model); MyView view = new MyView(model, controller); Scene scene = new Scene(view); stage.setTitle("MyTitle"); stage.setScene(scene); stage.sizeToScene(); stage.show(); view.requestFocus(); } public void changeStageSize(int width, int height) { ... } public static void main(String[] args) { launch(args); } } What do I have to write into my changeStageSize void to change my stage size?
#Override public void start(Stage primaryStage) { Button btn = new Button("Resize"); btn.setOnAction((ActionEvent event) -> { changeStageSize(primaryStage, 800, 500); primaryStage.centerOnScreen(); }); StackPane root = new StackPane(btn); Scene scene = new Scene(root); primaryStage.setScene(scene); primaryStage.show(); } public void changeStageSize(Window stage, int width, int height) { stage.setWidth(width); stage.setHeight(height); } Just set the width and height of the Window. You could use a field instead of passing the stage parameter. If you don't do this IMHO the method should be made static, since no instance members of your application class are accessed.
Since the MyView instance is the root of the scene, from within MyView you can just do Window win = getScene().getWindow(); win.setWidth(...); win.setHeight(...); There is no need to delegate this method back to the Application subclass (and you really don't want MyView to have a dependency on that anyway.