所以我在这个状态上有两个按钮点击这里查看截图
实际发生的是我的第二个按钮“开始”不适用于以下值:
backButton.renderImage(g, CONTAINER_CENTER_X - backButton.getWidth() - 10, CONTAINER_MAX_Y - backButton.getHeight() - 30);
startButton.renderImage(g, CONTAINER_CENTER_X + 10, CONTAINER_MAX_Y - startButton.getHeight() - 30);
这是我为 x 和 y 设置的按钮坐标和值的输出:
返回按钮:[x=245, y= 535, width= 145, height=35] / 当前 X: 245 / 当前 Y: 535 开始按钮: [x=410, y= 535, width= 0, height=35] / 当前 X:410 / 当前 Y:535
然后我发现,通过加减 button.getWidth(),我可以让按钮工作
backButton.renderImage(g, CONTAINER_CENTER_X - backButton.getWidth() - 10, CONTAINER_MAX_Y - backButton.getHeight() - 30);
startButton.renderImage(g, CONTAINER_CENTER_X + startButton.getWidth() - startButton.getWidth() + 10, CONTAINER_MAX_Y - startButton.getHeight() - 30);
这很奇怪,而且非常丑陋......:D
这是我为 x 和 y 设置的按钮坐标和值的输出:
返回按钮:[x=245, y= 535, width= 145, height=35] / 当前 X: 245 / 当前 Y: 535 开始按钮: [x=410, y= 535, width= 145, height=35] / 当前 X:410 / 当前 Y:535
那么,检查一下这个状态码和按钮类,告诉我你怎么看?
这个状态
package poker;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;
import org.newdawn.slick.state.transition.FadeInTransition;
import org.newdawn.slick.state.transition.FadeOutTransition;
import poker.gui.PButton;
import poker.gui.UIConstants;
import static poker.gui.UIConstants.CONTAINER_CENTER_X;
import static poker.gui.UIConstants.CONTAINER_MAX_Y;
import poker.util.RandomUtility;
public class SetupScreenState extends BasicGameState implements UIConstants, StatePanelInterface {
public static final int ID = 2;
private Image titleImage;
private PButton backButton;
private PButton startButton;
public SetupScreenState() {
}
@Override
public int getID() {
return ID;
}
@Override
public void init(GameContainer container, StateBasedGame game) throws SlickException {
titleImage = new Image("res/gui/title_setup.png");
backButton = new PButton("res/gui/button_back.png", "res/gui/button_back_hover.png");
startButton = new PButton("res/gui/button_start.png", "res/gui/button_start_hover.png");
}
@Override
public void render(GameContainer container, StateBasedGame game, Graphics g) throws SlickException {
RandomUtility.bgTheme(container, g);
renderTopPanel(container, game, g);
renderCenterPanel(container, game, g);
renderBottomPanel(container, game, g);
}
@Override
public void update(GameContainer container, StateBasedGame game, int delta) throws SlickException {
Input input = container.getInput();
int mx = input.getMouseX();
int my = input.getMouseY();
if (backButton.contains(mx, my)) {
if (backButton.isButtonPressed(input)) {
game.enterState(StartScreenState.ID, new FadeOutTransition(), new FadeInTransition());
}
} else if (startButton.contains(mx, my)) {
if (startButton.isButtonPressed(input)) {
game.enterState(PlayScreenState.ID, new FadeOutTransition(), new FadeInTransition());
}
}
}
@Override
public void renderTopPanel(GameContainer container, StateBasedGame game, Graphics g) throws SlickException {
g.drawImage(titleImage, 50, 50);
}
@Override
public void renderCenterPanel(GameContainer container, StateBasedGame game, Graphics g) throws SlickException {
}
@Override
public void renderBottomPanel(GameContainer container, StateBasedGame game, Graphics g) throws SlickException {
backButton.renderImage(g, CONTAINER_CENTER_X - backButton.getWidth() - 10, CONTAINER_MAX_Y - backButton.getHeight() - 30);
startButton.renderImage(g, CONTAINER_CENTER_X + startButton.getWidth() - startButton.getWidth() + 10, CONTAINER_MAX_Y - startButton.getHeight() - 30);
}
}
按钮类
package poker.gui;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
public class PButton {
private int x;
private int y;
private int width;
private int height;
private Image image;
private String classicImage;
private String hoverImage;
public PButton() {
}
public PButton(String classicImage, String hoverImage) throws SlickException {
this.classicImage = classicImage;
this.hoverImage = hoverImage;
image = new Image(classicImage);
}
public PButton(String classicImage, String hoverImage, int x, int y) throws SlickException {
this.classicImage = classicImage;
this.hoverImage = hoverImage;
this.x = x;
this.y = y;
image = new Image(classicImage);
}
public void renderImage(Graphics g) {
g.drawImage(image, x, y);
}
public void renderImage(Graphics g, int x, int y) throws SlickException {
this.x = x;
this.y = y;
g.drawImage(image, x, y);
}
public boolean contains(int x, int y) throws SlickException {
int minX = this.x;
int minY = this.y;
int maxX = this.x + this.width;
int maxY = this.y + this.height;
if ((x > minX && x < maxX) && (y > minY && y < maxY)) {
if (hoverImage != null) {
image = new Image(hoverImage);
}
return true;
}
image = new Image(classicImage);
return false;
}
public boolean isButtonPressed(Input input) throws SlickException {
return input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON);
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public void setWidth(int width) {
this.width = width;
}
public int getWidth() {
return width = image.getWidth();
}
public void setHeight(int height) {
this.height = height;
}
public int getHeight() {
return height = image.getHeight();
}
public Image getImage() {
return image;
}
public void setImage(Image image) {
this.image = image;
}
@Override
public String toString() {
return "[x=" + x + ", y= " + y + ", width= " + width + ", height=" + height + "]";
}
}