Can't add multiple of same rectangle - java
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.
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();
How to pass stackPane reference to another Class method
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
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.
Javafx: how to fill a TilePane with circles and add an event handler to each one
I tryed with this code but it is not working: public class Scacchiera extends Application { TilePane root; #Override public void start(Stage primaryStage) { root = new TilePane(); root.setPrefColumns(4); root.setPrefRows(4); for (int i = 0; i < 16; i++) { Circle c = new Circle(i); c.addEventHandler(ActionEvent.ACTION, new EventHandler<ActionEvent>() { #Override public void handle(ActionEvent event) { System.out.println("Circle clicked"); } }); root.getChildren().add(c); } Scene scene = new Scene(root, 300, 250); primaryStage.setTitle("Hello World!"); primaryStage.setScene(scene); primaryStage.show(); } public static void main(String[] args) { launch(args); } } Why? Can you help me? I am not sure what is the problem but may be my events handler dies with the for cicle..
Circles are just shapes, they don't have actions like controls. What you want to do is is handle an onMouseClicked event to your circle: // java 8 circle.setOnMouseClicked(mouseEvent -> System.out.println("Mouse Clicked!"));
SceneBuilder 2.0 Dynamic shape generation
I have an FXML file that does some certain animations with some right now static(so to speak) shapes that are hard-coded into the fxml. What I am trying to do is dynamically create shapes from Java Objects that have certain properties such as color which these objects will be pulling from a database and populate the fxml with these object based shapes, I am not sure how to go about doing this. Below is the code for the main class, I know why the error is happening but not sure how to do it any other way. public class TestConveyorView extends GuiceApplication { #Inject private GuiceFXMLLoader fxmlLoader; public Injector createInjector() { return Guice.createInjector(new AbstractModule() { #Override protected void configure() { } }); } public static void main(String[] args) { Application.launch(args); } #Override public void init(List<Module> modules) throws Exception { } #Override public void start(Stage stage) throws Exception { //GridPane root = new GridPane(); Parent root = fxmlLoader.load(getClass().getClassLoader().getResource("fxml/TestConveyorView.fxml")).getRoot(); Box box = new Box(1, red); Rectangle rectangle = new Rectangle(50,50,box.getColor()); // Can't seem to add it to the scene, problem occurs here. root.getChildren().add(rectangle); Scene scene = new Scene(root); // BackgroundImage background = new BackgroundImage(null, BackgroundRepeat.REPEAT, BackgroundRepeat.REPEAT, BackgroundPosition.DEFAULT, BackgroundSize.DEFAULT); stage.setScene(scene); stage.show(); } }
Ok I fixed the problem by changing Parent root = ... To AnchorPane root = ... Simple fix that I overlooked I guess.