Nach einigen Tests implementiert ich herausgefunden habe, wie ich dies tun könnte, in der Button-Klasse schrieb ich:
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;
}
und es zu benutzen in meinem Rahmen schreibe ich einfach:
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");
}
}
}
ja das wird funktionieren, aber was ich versuche zu tun, ist eine neue Komponente zu erstellen, so dass ich es nur innerhalb der init-Methode mit ein paar Parametern initialisieren und es bei der render-Methode rendern kann. Ich habe es fast funktioniert. – Hamish
Sie können ein privates Array erstellen. Bei der Initialisierung erstellen Sie Ihre Komponenten und fügen sie dem Array hinzu. In der Rendermethode iterieren Sie über jede Komponente Ihres Arrays und rufen die Renderfunktion dieser Komponenten auf. – Alex