Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Java JavaFX tetris: Unable to add rectangular block

NewCoder123

New Coder
I'm trying to create a tetris app using JavaFX. The problem I'm having is that I'm unable to add a rectangle block to my scene object dynamically.

Layout.xml:
XML:
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.text.*?>
<?import javafx.scene.shape.*?>
<?import javafx.geometry.*?>
<?import java.lang.*?>
<?import javafx.scene.*?>
<?import javafx.scene.layout.*?>

<Group fx:id="root" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.LayoutController">
   <children>
      <HBox fx:id="overview" prefHeight="625.0" prefWidth="725.0">
         <children>
            <Pane fx:id="tetrisGrid" prefHeight="200.0" prefWidth="500.0" HBox.hgrow="SOMETIMES" />
            <Pane fx:id="scoreboard" prefHeight="200.0" prefWidth="250.0" HBox.hgrow="SOMETIMES">
               <children>
                  <VBox fx:id="labelBox" layoutX="21.0" layoutY="218.0" prefHeight="314.0" prefWidth="220.0" spacing="100.0">
                     <children>
                        <Text fx:id="score" strokeType="OUTSIDE" strokeWidth="0.0" text="Score:" wrappingWidth="124.205078125">
                           <font>
                              <Font name="Bell MT" size="30.0" />
                           </font>
                        </Text>
                        <Text fx:id="level" strokeType="OUTSIDE" strokeWidth="0.0" text="Level:" wrappingWidth="124.205078125">
                           <font>
                              <Font name="Bell MT" size="30.0" />
                           </font>
                        </Text>
                     </children>
                  </VBox>
               </children>
            </Pane>
         </children>
      </HBox>
   </children>
</Group>


Main class:

Java:
package application;
   
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Group;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;

public class Main extends Application {
    protected static final int size = 25;
    protected static final int move = 25;
    protected static final int rows = 25;
    protected static final int col = 25;
    protected static int[][] grid = new int[rows][col];
    protected static final int extra = 100;
   
    @Override
    public void start(Stage stage) {
            try {
                LayoutController.addRect();
                Parent root = LayoutController.load();
                Scene scene = new Scene(root, (size * col) + extra, size * col);
                stage.setScene(scene);
                stage.show();
            }
            catch(Exception e) {
                e.printStackTrace();
            }
    }
   
    public static void main(String[] args) {
        launch(args);
    }
}

LayoutController Class:
Code:
package application;
import java.io.IOException;

import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Group;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Text;
 
public class LayoutController {
    @FXML private static Text score;
    @FXML private static Text level;
    @FXML private static Pane tetrisGrid;
    @FXML private static Pane scoreboard;
    @FXML private static Group root;
    @FXML private static VBox labelBox;
    @FXML private static HBox overview;

    public static void addRect() {
        Rectangle rect = new Rectangle(Main.size, Main.size, Main.size, Main.size);
        tetrisGrid.getChildren().add(rect);
    }
}
 
I'm trying to create a tetris app using JavaFX. The problem I'm having is that I'm unable to add a rectangle block to my scene object dynamically.

Layout.xml:
XML:
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.text.*?>
<?import javafx.scene.shape.*?>
<?import javafx.geometry.*?>
<?import java.lang.*?>
<?import javafx.scene.*?>
<?import javafx.scene.layout.*?>

<Group fx:id="root" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.LayoutController">
   <children>
      <HBox fx:id="overview" prefHeight="625.0" prefWidth="725.0">
         <children>
            <Pane fx:id="tetrisGrid" prefHeight="200.0" prefWidth="500.0" HBox.hgrow="SOMETIMES" />
            <Pane fx:id="scoreboard" prefHeight="200.0" prefWidth="250.0" HBox.hgrow="SOMETIMES">
               <children>
                  <VBox fx:id="labelBox" layoutX="21.0" layoutY="218.0" prefHeight="314.0" prefWidth="220.0" spacing="100.0">
                     <children>
                        <Text fx:id="score" strokeType="OUTSIDE" strokeWidth="0.0" text="Score:" wrappingWidth="124.205078125">
                           <font>
                              <Font name="Bell MT" size="30.0" />
                           </font>
                        </Text>
                        <Text fx:id="level" strokeType="OUTSIDE" strokeWidth="0.0" text="Level:" wrappingWidth="124.205078125">
                           <font>
                              <Font name="Bell MT" size="30.0" />
                           </font>
                        </Text>
                     </children>
                  </VBox>
               </children>
            </Pane>
         </children>
      </HBox>
   </children>
</Group>


Main class:

Java:
package application;
  
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Group;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;

public class Main extends Application {
    protected static final int size = 25;
    protected static final int move = 25;
    protected static final int rows = 25;
    protected static final int col = 25;
    protected static int[][] grid = new int[rows][col];
    protected static final int extra = 100;
  
    @Override
    public void start(Stage stage) {
            try {
                LayoutController.addRect();
                Parent root = LayoutController.load();
                Scene scene = new Scene(root, (size * col) + extra, size * col);
                stage.setScene(scene);
                stage.show();
            }
            catch(Exception e) {
                e.printStackTrace();
            }
    }
  
    public static void main(String[] args) {
        launch(args);
    }
}

LayoutController Class:
Code:
package application;
import java.io.IOException;

import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Group;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Text;
 
public class LayoutController {
    @FXML private static Text score;
    @FXML private static Text level;
    @FXML private static Pane tetrisGrid;
    @FXML private static Pane scoreboard;
    @FXML private static Group root;
    @FXML private static VBox labelBox;
    @FXML private static HBox overview;

    public static void addRect() {
        Rectangle rect = new Rectangle(Main.size, Main.size, Main.size, Main.size);
        tetrisGrid.getChildren().add(rect);
    }
}
Never mind I solved it. I got rid of the static attributes for the injected elements in my LayoutController class.
 
Back
Top Bottom