鼠标输入用 canvas 重叠按钮
Overlapping of Buttons with canvas for mouseinput
我目前正在 java 中仅使用标准库制作 RPG 砍杀游戏。
对于这些游戏,我在一个独立的包中制作了一个 CustomButton
class,我只是将其复制并粘贴到我几乎所有的游戏中,然后进行一些游戏特定的修改。
这是 class::
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.WritableRaster;
import objects.Collectable;
public final class CustomButton implements MouseListener {
// private BufferedImage image;
private String text = "";
private boolean pressed = false;
private int x;
private int y;
private int width;
private int height;
private Color currentColor, defaultColor, hoverColor, pressColor;
private Point mousePoint = new Point(0, 0);
private ButtonListener btnListener = null;
private Canvas canvasObject;
private BufferedImage image;
private BufferedImage darkImage;
private String actionCommand = "default";
private Collectable object;
private boolean enabled;
/*
* private CustomButton(Canvas canvasObject,JFrame frame) { this.x=100;
* this.y=100; this.width=100; this.height=100;
*
* canvasObject.addMouseListener(this); currentColor=new Color(255,255,255);
* defaultColor=new Color(255,255,255); hoverColor=new Color(255,255,255);
* pressColor=new Color(255,255,255); }
*/
public CustomButton(int x, int y, int width, int height, Canvas canvasObject) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.canvasObject = canvasObject;
canvasObject.addMouseListener(this);
currentColor = Color.GREEN;
currentColor = new Color(255, 255, 255);
defaultColor = new Color(255, 255, 255);
hoverColor = new Color(255, 255, 255);
pressColor = new Color(255, 255, 255);
enabled = true;
}
public CustomButton(int x, int y, int width, int height, Canvas canvasObject, BufferedImage image,
Collectable object) {
this.image = image;
this.darkImage = darkenImage(image);
this.x = x;
this.y = y;
this.width = width;
this.height = height;
canvasObject.addMouseListener(this);
currentColor = Color.GREEN;
currentColor = new Color(255, 255, 255);
defaultColor = new Color(255, 255, 255);
hoverColor = new Color(255, 255, 255);
pressColor = new Color(255, 255, 255);
this.canvasObject = canvasObject;
this.object = object;
enabled = true;
}
public void render(Graphics g) {
if (image == null) {
g.setColor(currentColor);
if (!pressed)
g.fillRect(this.x, this.y, width, height);
else
g.fill3DRect(this.x, this.y, width, height, true);
g.setColor(Color.BLACK);
g.drawString(text, this.x + 10, this.y + 15);
} else {
if (enabled) {
g.drawImage(image, x, y, width, height, null);
} else {
g.drawImage(darkImage, x, y, width, height, null);
}
}
}
public Rectangle getBounds() {
return new Rectangle(x, y, width, height);
}
public void tick() {
mousePoint = getMouseLocation();
changeColor();
}
private Point getMouseLocation() {
int x = 0;
int y = 0;
try {
x = (int) (canvasObject.getMousePosition().getX());
y = (int) (canvasObject.getMousePosition().getY());
} catch (NullPointerException nl) {
}
return new Point(x, y);
}
public void changeColor() {
if (!pressed) {
if (getBounds().contains(mousePoint))
currentColor = hoverColor;
else
currentColor = defaultColor;
} else {
currentColor = pressColor;
}
}
public void addButtonListener(ButtonListener btnListener) {
this.btnListener = btnListener;
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mouseClicked(MouseEvent e) {
if (enabled) {
if (btnListener != null) {
if (getBounds().contains(mousePoint)) {
ButtonID id = ButtonID.UNDETERMINABLE;
if (e.getButton() == MouseEvent.BUTTON1) id = ButtonID.LEFT;
if (e.getButton() == MouseEvent.BUTTON2) id = ButtonID.RIGHT;
btnListener.buttonClicked(new ButtonEvent(id, object, actionCommand));
}
}
}
}
public void mousePressed(MouseEvent e) {
if (getBounds().contains(mousePoint)) pressed = true;
}
public void mouseReleased(MouseEvent e) {
pressed = false;
}
public void setActionCommand(String actionCommand) {
this.actionCommand = actionCommand;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public void setDefaultColor(Color c) {
defaultColor = c;
}
public void setHoverColor(Color c) {
hoverColor = c;
}
public void setPressColor(Color c) {
pressColor = c;
}
public Collectable getObject() {
return object;
}
public void setObject(Collectable object) {
this.object = object;
}
public void destroy() {
canvasObject.removeMouseListener(this);
}
public void disable() {
enabled = false;
}
public void enable() {
enabled = true;
}
public boolean isEnabled() {
return enabled;
}
private BufferedImage darkenImage(BufferedImage image) {
int width = image.getWidth();
int height = image.getHeight();
image = deepCopy(image);
WritableRaster raster = image.getRaster();
for (int xx = 0; xx < width; xx++) {
for (int yy = 0; yy < height; yy++) {
int[] pixels = raster.getPixel(xx, yy, (int[]) null);
pixels[0] -= 50;
pixels[1] -= 50;
pixels[2] -= 50;
pixels[0] = Math.max(pixels[0], 0);
pixels[1] = Math.max(pixels[0], 0);
pixels[2] = Math.max(pixels[0], 0);
raster.setPixel(xx, yy, pixels);
}
}
return image;
}
private BufferedImage deepCopy(BufferedImage bi) {
ColorModel cm = bi.getColorModel();
boolean isAlphaPremultiplied = cm.isAlphaPremultiplied();
WritableRaster raster = bi.copyData(null);
return new BufferedImage(cm, raster, isAlphaPremultiplied, null);
}
}
从mouseClicked()
方法中大概可以看出,一个事件发送到ButtonListener
class.The ButtonListener
接口中也在包中声明了
这个按钮是在canvas itself.For例子上绘制的,在levelmap中,右下角有一个按钮,点击后会打开inventory.Let这个按钮被称为 btn_INV
.
到现在为止,我一直在获取通过 keyboard.But 移动玩家的输入我计划将 keyInput 更改为鼠标输入,玩家将移动到用户点击的方块。
为此,我必须制作一个 class ,比如实现 MouseListener
的 MouseInput
。现在的问题是,当我点击 btn_INV
时,不仅按钮会生成一个事件,而且,由于按钮实际上是在 canvas 上绘制的,因此 MouseInput
class 也会收到一个关于玩家想要 moving.Now,我认为当 MouseInput
class 得到 MouseEvent
时,它会检查按钮,因为只要鼠标点击 canvas,按钮总是 得到通知,虽然它可能不会生成 ButtonEvent
,正如您从 code.But 中看到的那样,这是一个相当糟糕的方法并且高度 inefficient.So ,我想要另一种方法。
注意: 我想创建另一个 canvas 来显示 HUD 和 btn_INV
以及其他类似的按钮,但这并没有真正解决问题尽可能绕过它。
我认为有两种方法可以解决这个问题:
首先是将您的游戏屏幕分为两部分:一部分用于按钮,另一部分用于瓷砖,因此您可以在捕获 MouseEvent 时测试点击是否位于瓷砖上或不是。请注意,磁贴可以放置在新按钮上。此解决方案易于实施,但您无法在磁贴区域放置按钮。
第二个将是创建一个"ButtonManager"。你的游戏 class 将有这个按钮管理器,它会监听鼠标事件然后发送到按钮。按钮不会直接监听此事件。如果点击在他们的边界上,他们会一个接一个地说,如果没有按钮被触发,这意味着点击发生在瓷砖上。此方法实施起来有点困难,但允许您在按钮之间创建优先级,因此按钮的边界可以相交!
希望对您有所帮助!
一般来说,按钮侦听器等概念用在应用程序中,而不是游戏中。
游戏通常采用游戏循环,其中包含更新和绘制阶段。在更新阶段,用户输入被捕获(好吧...检查多于捕获,但我稍后会谈到)并解释。
所以你只需要一个大鼠标侦听器,然后检查哪个按钮可能被按下,或者鼠标是否在游戏区域内,以便角色应该得到移动命令。
当然,从技术上讲,鼠标侦听器不应直接执行任何操作,因为它已绑定到 swing 线程。它应该只更改一些变量(例如:"left mouse button is down, position is x, y"),然后将在您的更新阶段检查这些变量,然后实际使用它。这样一来,您就不再依赖于 swing 线程(无论如何这对游戏来说都不是理想的),并且 - 几乎同样重要 - 您的游戏逻辑是完全可预测的。
但是,您仍然必须确保定期调用绘制方法并实际绘制您想要绘制的内容和时间。不过,这不是问题所在,所以我不会详细介绍它。
请记住,即使是摇摆也不仅仅是一些神奇的构造。单击鼠标时,swing 还将遍历您拥有的所有元素,并查看其中之一是否应该获得事件。如果您编写自己的按钮,那也是您必须要做的。
为每个按钮提供自己的鼠标侦听器只会让您的游戏变得越来越混乱。它还会破坏您的游戏循环,因为无论您此时正在做什么,都可以随时抛出和捕获鼠标事件。如果您的游戏逻辑发生在与 swing 不同的线程中(它应该如此),您甚至有可能在处理某些内容时收到一个事件。这会搞砸你的结果。
这也可能会导致一些非常奇怪的一次性错误,您不理解也无法重现这些错误。所以要非常小心。
我目前正在 java 中仅使用标准库制作 RPG 砍杀游戏。
对于这些游戏,我在一个独立的包中制作了一个 CustomButton
class,我只是将其复制并粘贴到我几乎所有的游戏中,然后进行一些游戏特定的修改。
这是 class::
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.WritableRaster;
import objects.Collectable;
public final class CustomButton implements MouseListener {
// private BufferedImage image;
private String text = "";
private boolean pressed = false;
private int x;
private int y;
private int width;
private int height;
private Color currentColor, defaultColor, hoverColor, pressColor;
private Point mousePoint = new Point(0, 0);
private ButtonListener btnListener = null;
private Canvas canvasObject;
private BufferedImage image;
private BufferedImage darkImage;
private String actionCommand = "default";
private Collectable object;
private boolean enabled;
/*
* private CustomButton(Canvas canvasObject,JFrame frame) { this.x=100;
* this.y=100; this.width=100; this.height=100;
*
* canvasObject.addMouseListener(this); currentColor=new Color(255,255,255);
* defaultColor=new Color(255,255,255); hoverColor=new Color(255,255,255);
* pressColor=new Color(255,255,255); }
*/
public CustomButton(int x, int y, int width, int height, Canvas canvasObject) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.canvasObject = canvasObject;
canvasObject.addMouseListener(this);
currentColor = Color.GREEN;
currentColor = new Color(255, 255, 255);
defaultColor = new Color(255, 255, 255);
hoverColor = new Color(255, 255, 255);
pressColor = new Color(255, 255, 255);
enabled = true;
}
public CustomButton(int x, int y, int width, int height, Canvas canvasObject, BufferedImage image,
Collectable object) {
this.image = image;
this.darkImage = darkenImage(image);
this.x = x;
this.y = y;
this.width = width;
this.height = height;
canvasObject.addMouseListener(this);
currentColor = Color.GREEN;
currentColor = new Color(255, 255, 255);
defaultColor = new Color(255, 255, 255);
hoverColor = new Color(255, 255, 255);
pressColor = new Color(255, 255, 255);
this.canvasObject = canvasObject;
this.object = object;
enabled = true;
}
public void render(Graphics g) {
if (image == null) {
g.setColor(currentColor);
if (!pressed)
g.fillRect(this.x, this.y, width, height);
else
g.fill3DRect(this.x, this.y, width, height, true);
g.setColor(Color.BLACK);
g.drawString(text, this.x + 10, this.y + 15);
} else {
if (enabled) {
g.drawImage(image, x, y, width, height, null);
} else {
g.drawImage(darkImage, x, y, width, height, null);
}
}
}
public Rectangle getBounds() {
return new Rectangle(x, y, width, height);
}
public void tick() {
mousePoint = getMouseLocation();
changeColor();
}
private Point getMouseLocation() {
int x = 0;
int y = 0;
try {
x = (int) (canvasObject.getMousePosition().getX());
y = (int) (canvasObject.getMousePosition().getY());
} catch (NullPointerException nl) {
}
return new Point(x, y);
}
public void changeColor() {
if (!pressed) {
if (getBounds().contains(mousePoint))
currentColor = hoverColor;
else
currentColor = defaultColor;
} else {
currentColor = pressColor;
}
}
public void addButtonListener(ButtonListener btnListener) {
this.btnListener = btnListener;
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mouseClicked(MouseEvent e) {
if (enabled) {
if (btnListener != null) {
if (getBounds().contains(mousePoint)) {
ButtonID id = ButtonID.UNDETERMINABLE;
if (e.getButton() == MouseEvent.BUTTON1) id = ButtonID.LEFT;
if (e.getButton() == MouseEvent.BUTTON2) id = ButtonID.RIGHT;
btnListener.buttonClicked(new ButtonEvent(id, object, actionCommand));
}
}
}
}
public void mousePressed(MouseEvent e) {
if (getBounds().contains(mousePoint)) pressed = true;
}
public void mouseReleased(MouseEvent e) {
pressed = false;
}
public void setActionCommand(String actionCommand) {
this.actionCommand = actionCommand;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public void setDefaultColor(Color c) {
defaultColor = c;
}
public void setHoverColor(Color c) {
hoverColor = c;
}
public void setPressColor(Color c) {
pressColor = c;
}
public Collectable getObject() {
return object;
}
public void setObject(Collectable object) {
this.object = object;
}
public void destroy() {
canvasObject.removeMouseListener(this);
}
public void disable() {
enabled = false;
}
public void enable() {
enabled = true;
}
public boolean isEnabled() {
return enabled;
}
private BufferedImage darkenImage(BufferedImage image) {
int width = image.getWidth();
int height = image.getHeight();
image = deepCopy(image);
WritableRaster raster = image.getRaster();
for (int xx = 0; xx < width; xx++) {
for (int yy = 0; yy < height; yy++) {
int[] pixels = raster.getPixel(xx, yy, (int[]) null);
pixels[0] -= 50;
pixels[1] -= 50;
pixels[2] -= 50;
pixels[0] = Math.max(pixels[0], 0);
pixels[1] = Math.max(pixels[0], 0);
pixels[2] = Math.max(pixels[0], 0);
raster.setPixel(xx, yy, pixels);
}
}
return image;
}
private BufferedImage deepCopy(BufferedImage bi) {
ColorModel cm = bi.getColorModel();
boolean isAlphaPremultiplied = cm.isAlphaPremultiplied();
WritableRaster raster = bi.copyData(null);
return new BufferedImage(cm, raster, isAlphaPremultiplied, null);
}
}
从mouseClicked()
方法中大概可以看出,一个事件发送到ButtonListener
class.The ButtonListener
接口中也在包中声明了
这个按钮是在canvas itself.For例子上绘制的,在levelmap中,右下角有一个按钮,点击后会打开inventory.Let这个按钮被称为 btn_INV
.
到现在为止,我一直在获取通过 keyboard.But 移动玩家的输入我计划将 keyInput 更改为鼠标输入,玩家将移动到用户点击的方块。
为此,我必须制作一个 class ,比如实现 MouseListener
的 MouseInput
。现在的问题是,当我点击 btn_INV
时,不仅按钮会生成一个事件,而且,由于按钮实际上是在 canvas 上绘制的,因此 MouseInput
class 也会收到一个关于玩家想要 moving.Now,我认为当 MouseInput
class 得到 MouseEvent
时,它会检查按钮,因为只要鼠标点击 canvas,按钮总是 得到通知,虽然它可能不会生成 ButtonEvent
,正如您从 code.But 中看到的那样,这是一个相当糟糕的方法并且高度 inefficient.So ,我想要另一种方法。
注意: 我想创建另一个 canvas 来显示 HUD 和 btn_INV
以及其他类似的按钮,但这并没有真正解决问题尽可能绕过它。
我认为有两种方法可以解决这个问题:
首先是将您的游戏屏幕分为两部分:一部分用于按钮,另一部分用于瓷砖,因此您可以在捕获 MouseEvent 时测试点击是否位于瓷砖上或不是。请注意,磁贴可以放置在新按钮上。此解决方案易于实施,但您无法在磁贴区域放置按钮。
第二个将是创建一个"ButtonManager"。你的游戏 class 将有这个按钮管理器,它会监听鼠标事件然后发送到按钮。按钮不会直接监听此事件。如果点击在他们的边界上,他们会一个接一个地说,如果没有按钮被触发,这意味着点击发生在瓷砖上。此方法实施起来有点困难,但允许您在按钮之间创建优先级,因此按钮的边界可以相交!
希望对您有所帮助!
一般来说,按钮侦听器等概念用在应用程序中,而不是游戏中。
游戏通常采用游戏循环,其中包含更新和绘制阶段。在更新阶段,用户输入被捕获(好吧...检查多于捕获,但我稍后会谈到)并解释。
所以你只需要一个大鼠标侦听器,然后检查哪个按钮可能被按下,或者鼠标是否在游戏区域内,以便角色应该得到移动命令。
当然,从技术上讲,鼠标侦听器不应直接执行任何操作,因为它已绑定到 swing 线程。它应该只更改一些变量(例如:"left mouse button is down, position is x, y"),然后将在您的更新阶段检查这些变量,然后实际使用它。这样一来,您就不再依赖于 swing 线程(无论如何这对游戏来说都不是理想的),并且 - 几乎同样重要 - 您的游戏逻辑是完全可预测的。
但是,您仍然必须确保定期调用绘制方法并实际绘制您想要绘制的内容和时间。不过,这不是问题所在,所以我不会详细介绍它。
请记住,即使是摇摆也不仅仅是一些神奇的构造。单击鼠标时,swing 还将遍历您拥有的所有元素,并查看其中之一是否应该获得事件。如果您编写自己的按钮,那也是您必须要做的。
为每个按钮提供自己的鼠标侦听器只会让您的游戏变得越来越混乱。它还会破坏您的游戏循环,因为无论您此时正在做什么,都可以随时抛出和捕获鼠标事件。如果您的游戏逻辑发生在与 swing 不同的线程中(它应该如此),您甚至有可能在处理某些内容时收到一个事件。这会搞砸你的结果。
这也可能会导致一些非常奇怪的一次性错误,您不理解也无法重现这些错误。所以要非常小心。