Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

How to render a BufferedImage in a GUI 1.16.5


Recommended Posts

Hello, I'm new at modding and recently I was trying to put a image, which is outside .jar, into a gui. But the far I got is load this image in a byte[] the transforme this in a BufferedImage. I search some foruns and thigs about how I could solve my problem, a part of them seems to transform the ImageBuffer into a intByte them use GL11 to render the image, but i cant make the image appear in my GUI. The other part seems to use DynamicTexture, but the questions is outdate (like 2014 or 2015) and the code seems to cheged.

I think the most promising way to solve my problem is through GL11, but I am open to new ways and possibilties. I even tried to render this image with the JNA using GDI32, but ( because I'm a completely noob ) It doesn't work.

The reason why I want a BufferedImage is because the image will uptade according to another API thata generates random images each time. This is why I can't put the image at resources.

If anyone want any code for the gui.

Thanks if you readed this far.



import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.api.distmarker.Dist;

import net.minecraft.world.World;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.ResourceLocation;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.client.gui.screen.inventory.ContainerScreen;
import net.minecraft.client.renderer.texture.DynamicTexture;
import net.minecraft.client.Minecraft;

import com.mojang.blaze3d.systems.RenderSystem;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Map;

import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL12;

import static org.lwjgl.opengl.GL11.*;

import com.google.common.collect.Maps;
import com.mojang.blaze3d.matrix.MatrixStack;
import com.mojang.blaze3d.platform.GlStateManager;

public class MyWindowGUI extends ContainerScreen<MyWindowGUIContainer.GuiContainerMod> {
    private World world;
    private int x, y, z;
    private PlayerEntity entity;
    public MyWindowGUI(MyWindowGUIContainer.GuiContainerMod container, PlayerInventory inventory, ITextComponent text) {
        super(container, inventory, text);
        this.world = container.world;
        this.x = container.x;
        this.y = container.y;
        this.z = container.z;
        this.entity = container.entity;
        this.xSize = 500;
        this.ySize = 400;
    private static final ResourceLocation texture = new ResourceLocation("myMod:textures/textuteTest.png");

    public void render(MatrixStack ms, int mouseX, int mouseY, float partialTicks) {
        super.render(ms, mouseX, mouseY, partialTicks);
        this.renderHoveredTooltip(ms, mouseX, mouseY);


    protected void drawGuiContainerBackgroundLayer(MatrixStack ms, float partialTicks, int gx, int gy) {
        RenderSystem.color4f(1, 1, 1, 1);
        int k = (this.width - this.xSize) / 2;
        int l = (this.height - this.ySize) / 2;
        this.blit(ms, k, l, 0, 0, this.xSize, this.ySize, this.xSize, this.ySize);
            //Minecraft.getInstance().getTextureManager().bindTexture(new ResourceLocation("modId:textures/screen1.jpeg"));
            }catch(Exception e){  }
        this.blit(ms, this.guiLeft + 42, this.guiTop + 84, 0, 0, 96, 128, 96, 128);

    public boolean keyPressed(int key, int b, int c) {
        if (key == 256) {
            return true;
        return super.keyPressed(key, b, c);

    public void tick() {

    protected void drawGuiContainerForegroundLayer(MatrixStack ms, int mouseX, int mouseY) {
        //my previos attempts
//        glPushMatrix();
//        BufferedImage image = new BufferedImage(128, 128, BufferedImage.TYPE_INT_ARGB);
//        Graphics2D g2d = image.createGraphics();
//        g2d.setColor(new Color(1.0f, 1.0f, 1.0f, 0.5f));
//        g2d.fillRect(0, 0, 128, 128); //A transparent white background
//        g2d.setColor(Color.red);
//        g2d.drawRect(0, 0, 127, 127); //A red frame around the image
//        g2d.fillRect(10, 10, 10, 10); //A red box 
//        g2d.setColor(Color.blue);
//        g2d.drawString("Test image", 10, 64); //Some blue text
//        g2d.dispose();
//        int[] pixels = new int[image.getWidth() * image.getHeight()];
//        image.getRGB(0, 0, image.getWidth(), image.getHeight(), pixels, 0, image.getWidth());
//        ByteBuffer buffer = BufferUtils.createByteBuffer(image.getWidth() * image.getHeight() * 4); //4 for RGBA, 3 for RGB
//        for(int y = 0; y < image.getHeight() ; y++){
//            for(int x = 0; x < image.getWidth(); x++){
//                int pixel = pixels[y * image.getWidth() + x];
//                buffer.put((byte) ((pixel >> 16) & 0xFF));     // Red component
//                buffer.put((byte) ((pixel >> 8) & 0xFF));      // Green component
//                buffer.put((byte) (pixel & 0xFF));                // Blue component
//                buffer.put((byte) ((pixel >> 24) & 0xFF));    // Alpha component. Only for RGBA
//            }
//        }
//        buffer.flip(); //FOR THE LOVE OF GOD DO NOT FORGET THIS
//       int textureID = glGenTextures(); //Generate texture ID
//       GlStateManager.bindBuffer(mouseX, textureID);
//        glBindTexture(GL_TEXTURE_2D, textureID); //Bind texture ID
//        //Setup wrap mode
//        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL12.GL_CLAMP_TO_EDGE);
//        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL12.GL_CLAMP_TO_EDGE);
//        //Setup texture scaling filtering
//        //Send texel data to OpenGL
//        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, image.getWidth(), image.getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
//        glBindTexture(GL_TEXTURE_2D, textureID);
//        glEnd();
        //images.put("minhaTextura", new DynamicTexture(GuiIsOpenProcedure.buffImage));
        //int k = (this.width - this.xSize) / 2;
        //int l = (this.height - this.ySize) / 2;
        //this.blit(ms, k, l, 0, 0, this.xSize, this.ySize, this.xSize, this.ySize);
        //Minecraft.getInstance().getTextureManager().getDynamicTextureLocation("texture2", GuiIsOpenProcedure.buffImage)

    public void onClose() {
        GuiIsOpenProcedure.guiIsOpen = false;

    public void init(Minecraft minecraft, int width, int height) {
        super.init(minecraft, width, height);


Edited by Marelo
Link to comment
Share on other sites

Posted (edited)

I read in stackOverflow that i can't save a image inside a .jar. But i don't know if is possible. Because i generate the image outside the .jar. What I tring to do looks like rendering the icon image of the world inside the game, it is just an example, isn't the case

I'm modding at eclipse, if I understand the question.

Edited by Marelo
Link to comment
Share on other sites

8 minutes ago, Marelo said:

I read in stackOverflow that i can't save a image inside a .jar. But i don't know if is possible. Because i generate the image outside the .jar. What I tring to do looks like rendering the icon image of the world inside the game, it is just an example, isn't the case

why do you want to do this?

8 minutes ago, Marelo said:

I'm modding at eclipse, if I understand the question.

using the mdk?

Link to comment
Share on other sites

Posted (edited)
2 minutes ago, Luis_ST said:

why do you want to do this?

using the mdk?


2 minutes ago, Luis_ST said:

why do you want to do this?

Because my API only generates this outside the .jar

Edited by Marelo
Link to comment
Share on other sites

Posted (edited)
4 minutes ago, Luis_ST said:

why would you want to save a picture in a .jar?

ResourceLocation only load images inside the mod .jar, i thought it could be a way to solve my problem

Edited by Marelo
Link to comment
Share on other sites

  • 4 weeks later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing

    No registered users viewing this page.

  • Posts

    • Hi i'm currently working on an entity. I just i finished it but it won't spawn and i don't know why. I got this error : java.lang.NullPointerException: Cannot invoke "net.minecraft.world.entity.ai.attributes.AttributeSupplier.getValue(net.minecraft.world.entity.ai.attributes.Attribute)" because "this.supplier" is null at net.minecraft.world.entity.ai.attributes.AttributeMap.getValue(AttributeMap.java:65) ~[forge-1.17.1-37.0.103_mapped_official_1.17.1-recomp.jar%2375!:?] at net.minecraft.world.entity.LivingEntity.getAttributeValue(LivingEntity.java:1806) ~[forge-1.17.1-37.0.103_mapped_official_1.17.1-recomp.jar%2375!:?] at net.minecraft.world.entity.LivingEntity.getMaxHealth(LivingEntity.java:1571) ~[forge-1.17.1-37.0.103_mapped_official_1.17.1-recomp.jar%2375!:?] at net.minecraft.world.entity.LivingEntity.<init>(LivingEntity.java:230) ~[forge-1.17.1-37.0.103_mapped_official_1.17.1-recomp.jar%2375!:?] at net.minecraft.world.entity.Mob.<init>(Mob.java:112) ~[forge-1.17.1-37.0.103_mapped_official_1.17.1-recomp.jar%2375!:?] at com.armanse.dreamy.entity.SleepingPlayerEntity.<init>(SleepingPlayerEntity.java:16) ~[%2379!:?] at net.minecraft.world.entity.EntityType.create(EntityType.java:460) ~[forge-1.17.1-37.0.103_mapped_official_1.17.1-recomp.jar%2375!:?] at net.minecraft.world.entity.EntityType.lambda$create$1(EntityType.java:470) ~[forge-1.17.1-37.0.103_mapped_official_1.17.1-recomp.jar%2375!:?] at java.util.Optional.map(Optional.java:260) ~[?:?] at net.minecraft.world.entity.EntityType.create(EntityType.java:469) ~[forge-1.17.1-37.0.103_mapped_official_1.17.1-recomp.jar%2375!:?] at net.minecraft.world.entity.EntityType.loadStaticEntity(EntityType.java:552) ~[forge-1.17.1-37.0.103_mapped_official_1.17.1-recomp.jar%2375!:?] at net.minecraft.world.entity.EntityType.loadEntityRecursive(EntityType.java:508) ~[forge-1.17.1-37.0.103_mapped_official_1.17.1-recomp.jar%2375!:?] at net.minecraft.server.commands.SummonCommand.spawnEntity(SummonCommand.java:50) ~[forge-1.17.1-37.0.103_mapped_official_1.17.1-recomp.jar%2375!:?] at net.minecraft.server.commands.SummonCommand.lambda$register$1(SummonCommand.java:34) ~[forge-1.17.1-37.0.103_mapped_official_1.17.1-recomp.jar%2375!:?] at com.mojang.brigadier.CommandDispatcher.execute(CommandDispatcher.java:263) ~[brigadier-1.0.18.jar%2341!:?] at net.minecraft.commands.Commands.performCommand(Commands.java:224) ~[forge-1.17.1-37.0.103_mapped_official_1.17.1-recomp.jar%2375!:?] at net.minecraft.server.network.ServerGamePacketListenerImpl.handleCommand(ServerGamePacketListenerImpl.java:1130) ~[forge-1.17.1-37.0.103_mapped_official_1.17.1-recomp.jar%2375!:?] at net.minecraft.server.network.ServerGamePacketListenerImpl.handleChat(ServerGamePacketListenerImpl.java:1109) ~[forge-1.17.1-37.0.103_mapped_official_1.17.1-recomp.jar%2375!:?] at net.minecraft.server.network.ServerGamePacketListenerImpl.handleChat(ServerGamePacketListenerImpl.java:1095) ~[forge-1.17.1-37.0.103_mapped_official_1.17.1-recomp.jar%2375!:?] at net.minecraft.network.protocol.game.ServerboundChatPacket.handle(ServerboundChatPacket.java:27) ~[forge-1.17.1-37.0.103_mapped_official_1.17.1-recomp.jar%2375!:?] at net.minecraft.network.protocol.game.ServerboundChatPacket.handle(ServerboundChatPacket.java:6) ~[forge-1.17.1-37.0.103_mapped_official_1.17.1-recomp.jar%2375!:?] at net.minecraft.network.protocol.PacketUtils.lambda$ensureRunningOnSameThread$0(PacketUtils.java:21) ~[forge-1.17.1-37.0.103_mapped_official_1.17.1-recomp.jar%2375!:?] at net.minecraft.server.TickTask.run(TickTask.java:17) ~[forge-1.17.1-37.0.103_mapped_official_1.17.1-recomp.jar%2375!:?] at net.minecraft.util.thread.BlockableEventLoop.doRunTask(BlockableEventLoop.java:139) ~[forge-1.17.1-37.0.103_mapped_official_1.17.1-recomp.jar%2375!:?] at net.minecraft.util.thread.ReentrantBlockableEventLoop.doRunTask(ReentrantBlockableEventLoop.java:22) ~[forge-1.17.1-37.0.103_mapped_official_1.17.1-recomp.jar%2375!:?] at net.minecraft.server.MinecraftServer.doRunTask(MinecraftServer.java:775) ~[forge-1.17.1-37.0.103_mapped_official_1.17.1-recomp.jar%2375!:?] at net.minecraft.server.MinecraftServer.doRunTask(MinecraftServer.java:162) ~[forge-1.17.1-37.0.103_mapped_official_1.17.1-recomp.jar%2375!:?] at net.minecraft.util.thread.BlockableEventLoop.pollTask(BlockableEventLoop.java:112) ~[forge-1.17.1-37.0.103_mapped_official_1.17.1-recomp.jar%2375!:?] at net.minecraft.server.MinecraftServer.pollTaskInternal(MinecraftServer.java:758) ~[forge-1.17.1-37.0.103_mapped_official_1.17.1-recomp.jar%2375!:?] at net.minecraft.server.MinecraftServer.pollTask(MinecraftServer.java:752) ~[forge-1.17.1-37.0.103_mapped_official_1.17.1-recomp.jar%2375!:?] at net.minecraft.util.thread.BlockableEventLoop.managedBlock(BlockableEventLoop.java:122) ~[forge-1.17.1-37.0.103_mapped_official_1.17.1-recomp.jar%2375!:?] at net.minecraft.server.MinecraftServer.waitUntilNextTick(MinecraftServer.java:738) ~[forge-1.17.1-37.0.103_mapped_official_1.17.1-recomp.jar%2375!:?] at net.minecraft.server.MinecraftServer.runServer(MinecraftServer.java:687) ~[forge-1.17.1-37.0.103_mapped_official_1.17.1-recomp.jar%2375!:?] at net.minecraft.server.MinecraftServer.lambda$spin$2(MinecraftServer.java:258) ~[forge-1.17.1-37.0.103_mapped_official_1.17.1-recomp.jar%2375!:?] at java.lang.Thread.run(Thread.java:831) [?:?] I was wondering if the problem was with the attributes of the entity since it is mention at the top but i couldn't find the error (i'm a noob so that's kinda normal). Here is the code related to the attributes: @EventBusSubscriber(modid = DreamyMain.MOD_ID, bus = Bus.FORGE) public class ClientInitEvent { @SubscribeEvent public static final void entityAttributCreation(EntityAttributeCreationEvent event) { event.put(EntityInit.Entity.get(), Entity.createAttributes().build()); } @SubscribeEvent public static final void entityClientRederer(EntityRenderersEvent.RegisterRenderers event) { event.registerEntityRenderer(EntityInit.Entity.get(), Entity::new); } }   //IN The entity class public static AttributeSupplier.Builder createAttributes() { return Mob.createMobAttributes().add(Attributes.MAX_HEALTH, 20D).add(Attributes.MOVEMENT_SPEED, 0D); }  
    • I removed a bunch of mods that said other version than 1.16.5 seems to be working. Still its kinda weird because it clearly said that the "game version" was 1.16.5. Still not happy bout it  
    • From the looks of it LoomContainer still doesn't call onCraftMatrixChanged... What am i missing seriously?
  • Topics

  • Who's Online (See full list)

  • Create New...

Important Information

By using this site, you agree to our Privacy Policy.