How to check if the mouse cursor has entered a string in Java?

-1

So I am making a menu, and rather then using Java's buttons, I decided to use text that acted as a button. My idea was that if the mouse cursor was on the text it would work. But it didn't. My code is:

public void MouseClick(int mouseX, int mouseY, int button) {
        String[] names = {"Singleplayer", "Mutliplayer", "Language", "Options", "Exit"};
        int count = 0;
        for(String name : names) {
            float x = (width/names.length) * count + (width/names.length) / 2f - mc.fontRendererObj.getStringWidth(name)/2f;
            float y = height;
            if(mouseX >= x && mouseY >= y && mouseX < x + mc.fontRendererObj.getStringWidth(name) && mouseY < y + mc.fontRendererObj.FONT_HEIGHT){
                System.out.println("Well this works");
                switch(name) {
                    case "Singleplayer":
                        mc.displayGuiScreen(new GuiWorldSelection(this));
                        System.out.println("This Works");
                    break;
                    case "Multiplayer":
                        mc.displayGuiScreen(new GuiMultiplayer(this));
                        System.out.println("Testing if this even works");
                    break;
                }
            }
        }
    }

I did a println test to see if it was maybe the menu that wasn't loading. But the text didn't print so it left me to conclude it was the actual button that wasn't working. Thanks, byeenter image description here

Code I use for the background:

        
        this.drawModalRectWithCustomSizedTexture(0, 0, 0, 0, this.width, this.height, this.width, this.height);
        this.drawGradientRect(0, height - 100, width, height, 0x00000000, 0xff000000);
        String [] names = {"Singleplayer", "Mutliplayer", "Language", "Options", "Exit"};
        int count = 0;
        for(String name : names) {
            
            this.drawString(mc.fontRendererObj, name, (width/names.length) * count + (width/names.length) / 2f, height - 20, -1);
            count++;
        }
        GlStateManager.translate( width/2f, height/2f - mc.fontRendererObj.FONT_HEIGHT,0);
        GlStateManager.scale(2, 2, 1);
        GlStateManager.translate(-(width/2f),-(height/2f),0);
        this.drawCenteredString(mc.fontRendererObj, "WeBeTrees Minecraft Mod", width/2f + 7, height/2f - mc.fontRendererObj.FONT_HEIGHT, -1);
        GlStateManager.translate(4,4,0);
        GlStateManager.scale(0.5, 0.5, 1);
        GlStateManager.translate(-4,-4,0);
java
minecraft
asked on Stack Overflow Jul 4, 2020 by we be trees org • edited Jul 4, 2020 by we be trees org

1 Answer

0

You don't update count within the loop, so for every menu item you're testing if the click happened in the position of the first item. Add count++ to update it:

    for(String name : names) {
        float x = (width/names.length) * count + (width/names.length) / 2f - mc.fontRendererObj.getStringWidth(name)/2f;
        // ...rest of loop...
        count++;
    }

Also, check if the computation for the string bounding box matches with where you're drawing the strings. y = height to me suggests you're testing if the click happened outside the window (below height)

answered on Stack Overflow Jul 4, 2020 by Joni

User contributions licensed under CC BY-SA 3.0