Kickstart Kotlin Open JavaFX 14 Minecraft Game like UI

Submitted by Dickens A S on Sat, 09/19/2020 - 10:53

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.fxml"))
            var scene:Scene = Scene(root)

            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) {
    companion object {
        fun main(args: Array<String>) {


<?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="" xmlns:fx=""
		<GridPane AnchorPane.bottomAnchor="50.0" AnchorPane.leftAnchor="0.0"
			AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" fx:id="mygrid">
		<BorderPane maxHeight="50.0" minHeight="50.0"
			AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0"
				<Button fx:id="mybutton" maxHeight="50"
					mnemonicParsing="false" prefHeight="50.0"
					prefWidth="200.0" text="Button" />

run the gradle command

gradlew run

That's All!!


Minecraft Game UI

Source Code

End of Article !

Add new comment