Java 抽象组件类是如何在slick 2d中使用的?

Java 抽象组件类是如何在slick 2d中使用的?,java,lwjgl,slick2d,Java,Lwjgl,Slick2d,我对slick 2d有些陌生,在我的项目中,我一直在玩按钮之类的东西。我会画一个图像,然后在坐标上听一听,结果它有50行长,这似乎是不必要的 但后来我看到了这条线;可悲的是,它死了,所以没有帮助。这家伙所做的似乎真的很有效率,但到目前为止,我还没有找到任何关于如何使用它的信息。如果有人能向我解释或告诉我如何使用它,那就太棒了。我在Slick 2D中实现了这样一个按钮 protected Shape hitbox; private boolean inside = false;

我对slick 2d有些陌生,在我的项目中,我一直在玩按钮之类的东西。我会画一个图像,然后在坐标上听一听,结果它有50行长,这似乎是不必要的


但后来我看到了这条线;可悲的是,它死了,所以没有帮助。这家伙所做的似乎真的很有效率,但到目前为止,我还没有找到任何关于如何使用它的信息。如果有人能向我解释或告诉我如何使用它,那就太棒了。

我在Slick 2D中实现了这样一个按钮

protected Shape hitbox;    
private boolean inside = false;    

public void render(GameContainer container, Graphics g) {
     .
     .//drawing the picture of the button 
     .
    if(inside && container.getInput().isMousePressed(Input.MOUSE_LEFT_BUTTON)){
         //someone clicked the button
    }
}
public void init(GameContainer container) {
    //setting a hitbox up to recognize when the mouse is over the picture/button
    hitbox = new Rectangle(/*dimensions of your picture*/);
}


public void update(GameContainer container, int delta) {
    //this code finds out if the mouse is over the button
    int mouseX = container.getInput().getMouseX();
    int mouseY = container.getInput().getMouseY();
    inside = hitbox.contains(mouseX, mouseY))
}

经过一些测试,我发现了如何做到这一点,在button类中,我写道:

import org.newdawn.slick.Color;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.geom.Rectangle;
import org.newdawn.slick.geom.Shape;
import org.newdawn.slick.gui.AbstractComponent;
import org.newdawn.slick.gui.GUIContext;

public class Button extends AbstractComponent{

protected int width;
protected int height;
protected int x;
protected int y;
protected int mouseX;
protected int mouseY;
protected boolean pressed;
protected Shape hitbox;
protected boolean over;
protected String text;

public Button(GUIContext container, int x, int y, int width, int height, String text) {
    super(container);
    setLocation(x, y);
    this.width = width;
    this.height = height;
    this.text = text;
    hitbox = new Rectangle(x, y, width, height);
}

@Override
public int getHeight() {
    return height;
}

@Override
public int getWidth() {
    return width;
}

@Override
public int getX() {
    return x;
}

@Override
public int getY() {
    return y;
}

public boolean isPressed() {
    return pressed;
}
public void update(GUIContext container) {
    mouseX = container.getInput().getMouseX();
    mouseY = container.getInput().getMouseY();
    over = hitbox.contains(mouseX, mouseY);

    if (over && container.getInput().isMousePressed(Input.MOUSE_LEFT_BUTTON)) {
        pressed = true;
    }else{
        pressed = false;
    }
}

@Override
public void render(GUIContext container, Graphics g) throws SlickException {
    g.setColor(Color.blue);
    g.fillRect(x, y, width, height);
    g.setColor(Color.black);
    g.drawString(text, x + width/2, y + height/2);
}
@Override
public void setLocation(int x, int y) {
    this.x = x;
    this.y = y;
}
要在我的框架中使用它,我只需写下:

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;

public class Menu extends BasicGameState{
    public static final int ID = 1;
    private StateBasedGame game;
    private Button button;

    public int getID() {
        return ID;
    }

    @Override
    public void init(GameContainer container, StateBasedGame game) throws SlickException {
    this.game = game;
    button = new Button(container, 1280/2, 720/2, 200, 200, "Works");
    }

    @Override
    public void render(GameContainer container, StateBasedGame game, Graphics g) throws SlickException {
    button.render(container, g);
    }

    @Override
    public void update(GameContainer container, StateBasedGame game, int delta) {
    button.update(container);
    if (button.isPressed()) {
        System.out.println("Works");
    }
}

}

是的,这会起作用,但我正在尝试创建一个新组件,这样我就可以在init方法中使用一些参数初始化它,然后在render方法中渲染它。不过我几乎可以让它工作了。你可以创建一个私有数组。初始化时,创建组件并将其添加到阵列中。在render方法中,迭代数组中的每个组件,并调用这些组件的render函数。