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

GitHub https://github.com/dickensas/kotlin-gradle-templates/tree/master/minecraft-javafx

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!!

 

Minecraft Game UI

 

Add new comment