Added image buttons to GUIs

Started by Doo89 on

Topic category: User side tutorials

Last seen on 07:28, 27. Apr 2024
Joined Aug 2014
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Added image buttons to GUIs
Tue, 02/06/2024 - 19:34 (edited)

How to make a GUI with button images for MCreator 2021.3 in forge 1.17.1

Before:
creadoo89

After:
creadoo89

After with changing the background image and adding the avatar:
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 Screen.java
creadoo89


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

import net.minecraft.client.gui.components.ImageButton;


creadoo89


4 - In your class add this line:

    private static final ResourceLocation MYIMAGE = new ResourceLocation("MODID:textures/mytexture.png");

replace MODID with the name of your mod and mytexture with the name of your texture which we will see below
creadoo89


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)


7 - Add this code by button:

        this.addRenderableWidget(new ImageButton(this.leftPos + 25, this.topPos + 34, 90, 20, 0, 0, 20, MYIMAGE, e -> {
         if (true) {
                CodeMod.PACKET_HANDLER.sendToServer(new GuiTestButtonMessage(0, x, y, z));
                GuiTestButtonMessage.handleButtonAction(entity, 0, x, y, z); // button action = 0
            }

         ((ImageButton)e).setPosition(this.leftPos + 25, this.topPos + 34);
      }));


8 - change ImageButton attributes for all buttons

creadoo89

Explanation of ImageButton parameters:
ImageButton(PosX, PosY, WidthImg, HeightImg, nbPixelImageX, nbPixelImageY, SautEnPixelImageSurvol , IMAGE LOCATION, Action)

PosX = position of the button starting from the left of the GUI
PosY = position of the button starting from the top of the GUI
WidthImg = image width
HeightImg = image height
nbPixelImageX = offset in x of the origin of the image
nbPixelImageY = offset in y of the origin of the image
SautEnPixelImageSurvol = hover image y offset
IMAGE LOCATION = location of the image (see point 4)
Action = action when button is clicked


9 - for all buttons change action number
creadoo89

10 - for each image remember to shift the offset in x or y

Exemple

https://i15.servimg.com/u/f15/11/82/44/56/dzocal10.png

 

11 - close the code (cross in the tab)

Clic to 'Close and save'

and
creadoo89

12 - Create a 256x256 image and save there in 'other'

exemple:
creadoo89

My image : https://mega.nz/file/Kl8zGSoZ#cAnWaQGZ-X6Y8So_OoLBcuz0LBMFZLKeQ7kPEvEIKOI

According to the name of the image, modify the code of part 4

 

Code before : https://paste.ofcode.org/a7yhRLUEuNww7k2zR8G8rX
Code after : https://paste.ofcode.org/PFcbJW6GBLkeGa4weAPLg

 

Sorry for my english! I'm french

Creadoo89

my youtube channel: https://www.youtube.com/creadoo89

 

Edited by Doo89 on Tue, 02/06/2024 - 19:34
Last seen on 11:49, 19. Jul 2023
Joined Nov 2020
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
(with a few minor…
Sun, 11/27/2022 - 00:30

(with a few minor modifications- it took maybe five minutes of messing around with the code to get it working properly.) 

@Mindthemoods do you think you can share what modifications you had to make? I'm using 1.18 and I'm not too familiar with java.

Last seen on 17:58, 18. Oct 2023
Joined Nov 2022
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Super helpful, thank you for…
Fri, 12/02/2022 - 16:24

Super helpful, thank you for this.

Last seen on 07:28, 27. Apr 2024
Joined Aug 2014
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Klemen, this tutorial is old…
Tue, 02/06/2024 - 15:19

Klemen, this tutorial is old and no longer relevant since it is now integrated into MCreator.
Can you pin this post? Thanks

Last seen on 07:28, 27. Apr 2024
Joined Aug 2014
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
*unpin this post
Tue, 02/06/2024 - 15:21

*unpin this post