Question

How would you put the main texture in, and then paste another texture in on top of it? Or, should I just create the main texture with the button texture already on it, and check for mouse clicks in that area? I am using lwjgl and slick2d for it.

Was it helpful?

Solution

You can draw a image over an image just by putting the code for the image you want on top under the code you want for the image code you want for the image on the bottom. Like:

graphic.drawImage(Image, 25, 50); //Under
graphic.drawImage(Image2, 25, 50); //Over

It's like this because of the load order, code loads from top to bottom, although using a string variable where the image location is suppose to be seemed more efficient to me so that when the image is hovered over or clicked it can change the string to a different image.

int mousex = Mouse.getX();
int mousey = Mouse.getY();
String buttonimgloc  = "unhoveredimagelocation.png"; 

public void render(GameContainer gc, StateBasedGame sbg, Graphics graphic) throws SlickException{
    Image button = new Image(buttonimgloc);
    graphic.drawImage(button, 35, 140);
}

public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException{
    Input input = gc.getInput();
    mousex = Mouse.getX();
    mousey = Mouse.getY();

    if((mousex>53 && mousex<114) && (mousey>300 && mousey<320)){
        buttonimgloc = "hoveredimagelocation.png";
        } else {
            buttonimgloc = "unhoveredimagelocation";
    }
}

The coordinates for mousex and mousey are from a game I've been working on you'll want to change them to where your image is like

((mousex>"leftedgeofimage" && mousex<"rightedgeofimage") && (mousey>"bottomedgeofimage" && mousey<"topedgeofimage"))

Don't add the "s and change the "edgeofimage" with the actual coordinate of that edge of the image

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top