Home

Awesome

<div align="center">

ImGui Java

JNI based binding for Dear ImGui

Github All Releases CI<br> Maven Central binding javadoc app javadoc

</div>

Features

To understand how to use ImGui Java - read official documentation and wiki. Binding adopts C++ API for Java, but almost everything can be used in the same manner.

ImGui Java has a ready to use implementation for GLFW and OpenGL API using LWJGL3 library. See imgui-lwjgl3 module.<br> Implementation is optional to use. Advantage of Dear ImGui is total portability, so feel free to copy-paste classes or write your own implementations.

Additionally, there is an imgui-app module, which provides a high abstraction layer.<br> It hides all low-level code under one class to extend. With it, you can build your GUI application instantly.

Support

ko-fi

You can support the project to motivate its further development.

How to Try

Make sure you have installed <u>JDK 8</u> or higher.

<p align="center"><img src="https://i.imgur.com/JjhR9in.png"/></p>

Demo

You can try binding by yourself in three simple steps:

git clone git@github.com:SpaiR/imgui-java.git
cd imgui-java
./gradlew :example:run

See example module to try other widgets in action.

How to Use

ImGui in LWJGL YouTube video by GamesWithGabe.<br> You can use this video as a basic step-by-step tutorial. It shows how to integrate binding with the usage of jar files.<br> Gradle and Maven dependencies could be used for this purpose as well.

Take a note, that integration itself is a very flexible process. It could be done in one way or another. If you just need a framework for your GUI - use Application module. Otherwise, if you need more control, the best way is not just to repeat steps, but to understand what each step does.

For macOS M-chip users

The macOS version of the binding is compiled as a universal binary. This means you can use it on both x86_64 and aarch64 platforms without any additional actions.

Application

If you don't care about OpenGL and other low-level stuff, then you can use application layer from imgui-app module. It is a one jar solution which includes: GLFW, OpenGL and Dear ImGui itself. So you only need one dependency line or one jar in classpath to make everything to work. <ins>You don't need to add separate dependencies to LWJGL or native libraries, since they are already included.</ins>

Application module is the best choice if everything you care is the GUI itself.

At the same time, Application gives options to override any life-cycle method it has. That means that if you are seeking for a bit more low-level control - you can gain it as well.

Example

A very simple application may look like this:

import imgui.ImGui;
import imgui.app.Application;
import imgui.app.Configuration;

public class Main extends Application {
    @Override
    protected void configure(Configuration config) {
        config.setTitle("Dear ImGui is Awesome!");
    }

    @Override
    public void process() {
        ImGui.text("Hello, World!");
    }

    public static void main(String[] args) {
        launch(new Main());
    }
}

Read imgui.app.Application javadoc to understand how it works under the hood.

Dependencies

Maven Central

<details> <summary><b>Gradle</b></summary>
repositories {
    mavenCentral()
}

dependencies {
    implementation "io.github.spair:imgui-java-app:${version}"
}
</details> <details> <summary><b>Maven</b></summary>
<dependencies>
    <dependency>
        <groupId>io.github.spair</groupId>
        <artifactId>imgui-java-app</artifactId>
        <version>${version}</version>
    </dependency>
</dependencies>
</details> <details> <summary><b>Raw Jar</b></summary>
  1. Go to the release page;
  2. Download java-libraries.zip;
  3. Get imgui-app-${version}-all.jar;
  4. Add the jar to your classpath.

Jar with all classifier already contains binding and lwjgl3 modules.<br> If you're using jar without the all classifier, add appropriate jars as well.

Both jars, with or without all classifier, have all required native libraries already.

</details>

Java Module System

If using Java 9 modules, you will need to require the imgui.app module.

Binding

Using binding without imgui-app module requires to "attach" it to the application manually. You can refer to imgui-app module to see how things are done there.

Dependencies

Maven Central

For simplicity, example of dependencies for Gradle / Maven only shows how to add natives for Windows. Feel free to add other platforms.

Native BinariesSystem
imgui-java-natives-windowsWindows
imgui-java-natives-linuxLinux
imgui-java-natives-macosmacOS

Take a note, that you also need to add dependencies to LWJGL library. Examples below shows how to do it as well.

<details> <summary><b>Gradle</b></summary>
repositories {
    mavenCentral()
}

ext {
    lwjglVersion = '3.3.3'
    imguiVersion = "${version}"
}

dependencies {
    implementation platform("org.lwjgl:lwjgl-bom:$lwjglVersion")

    ['', '-opengl', '-glfw'].each {
        implementation "org.lwjgl:lwjgl$it:$lwjglVersion"
        implementation "org.lwjgl:lwjgl$it::natives-windows"
    }
    
    implementation "io.github.spair:imgui-java-binding:$imguiVersion"
    implementation "io.github.spair:imgui-java-lwjgl3:$imguiVersion"
    
    implementation "io.github.spair:imgui-java-natives-windows:$imguiVersion"
}
</details> <details> <summary><b>Maven</b></summary>
<properties>
    <lwjgl.version>3.3.1</lwjgl.version>
    <imgui.java.version>${version}</imgui.java.version>
</properties>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.lwjgl</groupId>
            <artifactId>lwjgl-bom</artifactId>
            <version>${lwjgl.version}</version>
            <scope>import</scope>
            <type>pom</type>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>
    <dependency>
        <groupId>org.lwjgl</groupId>
        <artifactId>lwjgl</artifactId>
    </dependency>
    <dependency>
        <groupId>org.lwjgl</groupId>
        <artifactId>lwjgl-glfw</artifactId>
    </dependency>
    <dependency>
        <groupId>org.lwjgl</groupId>
        <artifactId>lwjgl-opengl</artifactId>
    </dependency>
    <dependency>
        <groupId>org.lwjgl</groupId>
        <artifactId>lwjgl</artifactId>
        <classifier>natives-windows</classifier>
    </dependency>
    <dependency>
        <groupId>org.lwjgl</groupId>
        <artifactId>lwjgl-glfw</artifactId>
        <classifier>natives-windows</classifier>
    </dependency>
    <dependency>
        <groupId>org.lwjgl</groupId>
        <artifactId>lwjgl-opengl</artifactId>
        <classifier>natives-windows</classifier>
    </dependency>
    
    <dependency>
        <groupId>io.github.spair</groupId>
        <artifactId>imgui-java-binding</artifactId>
        <version>${imgui.java.version}</version>
    </dependency>
    <dependency>
        <groupId>io.github.spair</groupId>
        <artifactId>imgui-java-lwjgl3</artifactId>
        <version>${imgui.java.version}</version>
    </dependency>
    <dependency>
        <groupId>io.github.spair</groupId>
        <artifactId>imgui-java-natives-windows</artifactId>
        <version>${imgui.java.version}</version>
    </dependency>
</dependencies>
</details> <details> <summary><b>Raw Jars</b></summary>
  1. Go to the release page;
  2. Download java-libraries.zip and native-libraries.zip;
  3. Get imgui-binding-${version}.jar and imgui-lwjgl3-${version}.jar from java-libraries, and binary libraries for required OS from native-libraries archive;
  4. Add jars to your classpath;
  5. Provide a VM option with location of files from the native-libraries archive.

VM option example:

Both imgui.library.path and java.library.path are equal with the difference, that java.library.path is standard JVM option to provide native libraries.

</details>

Java Module System

If using Java 9 modules, ImGui Java has Automatic Module Names:

PackageModule
imgui-java-appimgui.app
imgui-java-bindingimgui.binding
imgui-java-lwjgl3imgui.lwjgl3
imgui-java-natives-windowsimgui.natives.windows
imgui-java-natives-linuximgui.natives.linux
imgui-java-natives-macosimgui.natives.macos

Extensions

All extensions are already included in the binding and can be used as it is. See examples in the example module for more information about how to use them.

Freetype

By default, Dear ImGui uses stb-truetype to render fonts. However, there is an option to use the FreeType font renderer. To learn about the differences, visit the imgui_freetype page.

This binding also supports the FreeType option. FreeType is statically pre-compiled into the library, meaning it is included by default. To enable it use ImFontAtlas#setFreeTypeRenderer(true) method. This should be done before fonts atlas generation. https://github.com/SpaiR/imgui-java/blob/e822240115bb27ef0f399ccdf39613fb746f46be/example/src/main/java/Main.java#L46-L47 Therefore, you can freely use ImGuiFreeTypeBuilderFlags in your font configuration.

If you prefer not to use the FreeType font renderer, you will need to build your own binaries and use them instead.

Binding Notice

Binding was made with Java usage in mind. Some places of the original library were adapted for that.<br> For example, in places where in C++ you need to pass a reference value, in Java you pass primitive wrappers: ImInt, ImFloat etc.<br>

One important thing is how natives structs work. All of them have a public field with a pointer to the natively allocated memory.<br> By changing the pointer it's possible to use the same Java instance to work with different native structs.<br> Most of the time you can ignore this fact and just work with objects in a common way.

Read javadoc and source comments to get more info about how to do specific stuff.

How to Build Native Libraries

Ensure you've downloaded git submodules. That could be achieved:

Windows

Linux

macOS

macOS (arm64)

In envs parameter next values could be used windows, linux or macos or macosarm64.

-Dlocal is optional and means that natives will be built under the ./imgui-binding/build/ folder. Otherwise /tmp/imgui folder will be used.

Freetype

To build a version of the libraries with FreeType, you need to run the buildSrc/scripts/vendor_freetype.sh script first. This script configures the FreeType library to be statically compiled into your project.

License

See the LICENSE file for license rights and limitations (MIT).