Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

Hi everyone, so I have created my own button class where I override the draw button method to insert my own texture. It works but for some reason I cannot pass in a different texture using the constructor. I have to hard code the texture and then it works. I was wondering if anyone could tell me why.

 

 

public class CustomGuiButton extends GuiButton
{
 public static String buttonTextureName = "";// CANNOT PASS IN A TEXTURE MUST BE HARD CODED TO ACTUALLY GET THE TEXTURE NOT SURE WHY

 public CustomGuiButton(int id, int width, int height, String displayString, String textureName) {
	super(id, width, height, displayString);
	buttonTextureName = textureName; //WAS HOPING TO PASS IN THE TEXTURE HERE AS A STRING FROM MY BUTTON CONSTRUCTOR BUT IT DOESNT WORK
}

 public static ResourceLocation buttonTexture = new ResourceLocation("custommod:textures/gui/" + buttonTextureName);
/**
     * Draws this button to the screen.
     */
@Override
public void drawButton(Minecraft minecraft, int xCoord, int yCoord)
    {
        if (this.visible)
        {
            FontRenderer fontrenderer = minecraft.fontRenderer;
            minecraft.getTextureManager().bindTexture(buttonTexture);
            GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
            this.field_146123_n = xCoord >= this.xPosition && yCoord >= this.yPosition && xCoord < this.xPosition + this.width && yCoord < this.yPosition + this.height;
            int k = this.getHoverState(this.field_146123_n);
            GL11.glEnable(GL11.GL_BLEND);
            OpenGlHelper.glBlendFunc(770, 771, 1, 0);
            GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
            this.drawTexturedModalRect(this.xPosition, this.yPosition, 0, 46 + k * 20, this.width / 2, this.height);
            this.drawTexturedModalRect(this.xPosition + this.width / 2, this.yPosition, 200 - this.width / 2, 46 + k * 20, this.width / 2, this.height);
            this.mouseDragged(minecraft, xCoord, yCoord);
            int l = 14737632;

            if (packedFGColour != 0)
            {
                l = packedFGColour;
            }
            else if (!this.enabled)
            {
                l = 10526880;
            }
            else if (this.field_146123_n)
            {
                l = 16777120;
            }

            this.drawCenteredString(fontrenderer, this.displayString, this.xPosition + this.width / 2, this.yPosition + (this.height -  / 2, l);
        }
    }
}

 

 

This is the constructor that I use inside my GUI class's initGui() method. If I try to pass in my texture name via a string as "Test.png" where my texture is located in my appropriate directory specified by custommod:textures/gui/Test.png then I get the pink/black missing texture texture.

 

   

this.testBtn = new CustomGuiButton(BUTTON_TEST_ID, this.width - 408, this.height - 157, I18n.format("Test", new Object[0]), "Test.png");
        this.testBtn.width = 37;

 

 

To actually change the texture if instead I replace this line

 

 

public static ResourceLocation buttonTexture = new ResourceLocation("custommod:textures/gui/" + buttonTextureName);

which is located inside the CustomGuiButton class, with this line

public static ResourceLocation buttonTexture = new ResourceLocation("custommod:textures/gui/Test.png");

 

 

then I get my custom texture rendering as the buttons texture. Can anyone explain why I cannot pass in my texture via a string in this way?

 

any help is appreciated

public static ResourceLocation buttonTexture = new ResourceLocation("custommod:textures/gui/" + buttonTextureName);

 

Check what static means - it is static for this class. Also, how do you even expect it to work?

 

ResourceLocation is an object like any other. When you want to change it, you have to reinitialize it.

 

public ResourceLocation buttonTexture;

public CustomGuiButton(int id, int width, int height, String displayString, String textureName)
{
super(id, width, height, displayString);
buttonTexture = new ResourceLocation("custommod:textures/gui/" + textureName);
}

If any.

 

EDIT

I accidentaly put "static" before ResourceLocation (copy/paste), don't do that, it will cause this resource to be shared between all buttons (this is what static does).

1.7.10 is no longer supported by forge, you are on your own.

  • Author

 

thanks alot Ernio your suggestion worked

 

working class

public class CustomGuiButton extends GuiButton
{
 public ResourceLocation buttonTexture;

 public CustomGuiButton(int id, int width, int height, String displayString, String textureName) {
	super(id, width, height, displayString);
	buttonTexture = new ResourceLocation("yourmodsnamehere:textures/gui/" + textureName);
}
/**
     * Draws this button to the screen.
     */
@Override
public void drawButton(Minecraft minecraft, int xCoord, int yCoord)
    {
        if (this.visible)
        {
            FontRenderer fontrenderer = minecraft.fontRenderer;
            minecraft.getTextureManager().bindTexture(buttonTexture);
            GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
            this.field_146123_n = xCoord >= this.xPosition && yCoord >= this.yPosition && xCoord < this.xPosition + this.width && yCoord < this.yPosition + this.height;
            int k = this.getHoverState(this.field_146123_n);
            GL11.glEnable(GL11.GL_BLEND);
            OpenGlHelper.glBlendFunc(770, 771, 1, 0);
            GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
            this.drawTexturedModalRect(this.xPosition, this.yPosition, 0, 46 + k * 20, this.width / 2, this.height);
            this.drawTexturedModalRect(this.xPosition + this.width / 2, this.yPosition, 200 - this.width / 2, 46 + k * 20, this.width / 2, this.height);
            this.mouseDragged(minecraft, xCoord, yCoord);
            int l = 14737632;

            if (packedFGColour != 0)
            {
                l = packedFGColour;
            }
            else if (!this.enabled)
            {
                l = 10526880;
            }
            else if (this.field_146123_n)
            {
                l = 16777120;
            }

            this.drawCenteredString(fontrenderer, this.displayString, this.xPosition + this.width / 2, this.yPosition + (this.height -  / 2, l);
        }
    }
}

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

Important Information

By using this site, you agree to our Terms of Use.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.