Easily create Kotlin based JavaFX Minecraft game using boilerplate Gradle DSL
Below code explains how to write grid pane to create many small size buttons and keep then close without padding.
This is allows the developer to assume games like minecraft, terraria etc.
gridPane.add is the function allows the buttons needs to added.
Kotlin Code
import javafx.application.Application
import javafx.fxml.FXMLLoader
import javafx.scene.Parent
import javafx.scene.Scene
import javafx.stage.Stage
import javafx.scene.control.Button
import javafx.scene.layout.GridPane
import javafx.scene.control.ListView
import javafx.collections.ObservableList
import javafx.collections.FXCollections
import java.util.List
import java.util.ArrayList
import javafx.scene.Node
class App : Application() {
public override fun start(primaryStage:Stage) {
try {
var root:Parent = FXMLLoader.load(App::class.java.getResource("App.fxml"))
var scene:Scene = Scene(root)
scene.getStylesheets().add(App::class.java.getResource("App.css").toExternalForm())
primaryStage.setTitle("Example")
primaryStage.setScene(scene)
primaryStage.show()
var i=0
var j=0
var gridPane:GridPane = scene.lookup("#mygrid") as GridPane
for(i in 0..30) {
for(j in 0..12) {
var button1:Button = Button(" ")
gridPane.add(button1, i, j, 1, 1)
}
}
} catch (e:Exception) {
e.printStackTrace()
}
}
companion object {
fun main(args: Array<String>) {
}
}
}
FXML Code
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.web.*?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.control.ListView?>
<AnchorPane prefHeight="400.0" prefWidth="600.0"
xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="com.zigma.Controller">
<children>
<GridPane AnchorPane.bottomAnchor="50.0" AnchorPane.leftAnchor="0.0"
AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" fx:id="mygrid">
</GridPane>
<BorderPane maxHeight="50.0" minHeight="50.0"
AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0"
AnchorPane.rightAnchor="0.0">
<center>
<Button fx:id="mybutton" maxHeight="50"
mnemonicParsing="false" prefHeight="50.0"
prefWidth="200.0" text="Button" />
</center>
</BorderPane>
</children>
</AnchorPane>
run the gradle command
gradlew run
That's All!!
End of Article !