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:
Main class:
LayoutController Class:
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);
}
}