How to create buttons with Images (1.16.5)

Started by FishyHard on

Topic category: User side tutorials

Joined Apr 2021
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
How to create buttons with Images (1.16.5)
Sun, 10/09/2022 - 14:57 (edited)

How to make a GUI with button images for MCreator 2022.2 (Should work in older mcreator versions) in forge 1.16.5

Before:
creadoo89

After:
creadoo89

1 - Create a GUI with buttons and assign a functional procedure to the buttons.
creadoo89

2 - Edit the GUI code and select the file that ends with Window.java
creadoo89

3 - In the imports add at the end the line:

import net.minecraft.client.gui.widget.button.ImageButton;

4 - In your class add this line:

    private static final ResourceLocation MYIMAGE = new ResourceLocation("MODID:textures/(PATH WHERE IS YOUR TEXTURE)/.png");


Replace MODID with the mod id of your mod, then write your path where is your texture

 

5 - find the init() class
creadoo89

6 - comment each button with /* at the beginning and */ at the end
(optional, otherwise delete them, but note the numbers and needed text, text is shown in the 8th part)

7 - Add this code by button:

        this.addButton(new ImageButton(this.guiLeft + 51, this.guiTop + 25, 10, 20, 0, 0, 20, MYIMAGE, e -> {
            if (true) {
                MODNAMEMod.PACKET_HANDLER.sendToServer(new CLASSNAMEGui.ButtonPressedMessage(0, x, y, z));
                CLASSNAMEGui.handleButtonAction(entity, 0, x, y, z);
            }
            ((ImageButton) e).setPosition(this.guiLeft + 51, this.guiTop + 25);
        }));

8 - change ImageButton attributes for all buttons

creadoo89

9 - 8th part before and after code
Before:

After:

10 - Save and Lock (Press CTRL + W and CTRL + S, IMPORTANT you have to press CTRL + W for import to work or else you will get error)
 

11 - Texture it has to be 256x256, If you have different button textures you can put them anywhere but you will have to count where it is placed and write numbers in button code example: 12th part.


12 - Your button texture is in different place from tutorial

So check where your button texture begins (Aim at first top pixel of button) then look at mouse location in top left where you can see (px, px) currently (0, 0) and rewrite it in code. Example: 13th part.

13 - Rewrite numbers for button texture place.

14 - Example of changing button texture place

1. Check button texture placement:

2. Rewrite button texture code placement:

3. Save and Lock code and test it out:

Credits:
Main tutorial (1.17.1) - https://mcreator.net/forum/83507/added-image-buttons-guis
Help from Kleiders - https://mcreator.net/user/291988/urielito3010

Edited by FishyHard on Sun, 10/09/2022 - 14:57