Java ArrayList如何检查其中一个矩形是否包含鼠标?
我有一个应用程序,你把瓷砖。你可以把瓷砖铺在瓷砖上,我不想这样。我知道我需要一些东西,比如如果平铺矩形包含鼠标,那么不要在上面放置平铺。但这不起作用。请看以下代码:Java ArrayList如何检查其中一个矩形是否包含鼠标?,java,Java,我有一个应用程序,你把瓷砖。你可以把瓷砖铺在瓷砖上,我不想这样。我知道我需要一些东西,比如如果平铺矩形包含鼠标,那么不要在上面放置平铺。但这不起作用。请看以下代码: for (int i = 0; i < b.toArray().length; i++) { b.get(i).tick(); if (b.get(i).r.contains(Comp.mx, Comp.my)) { canPlaceATile = false; // Sys
for (int i = 0; i < b.toArray().length; i++) {
b.get(i).tick();
if (b.get(i).r.contains(Comp.mx, Comp.my)) {
canPlaceATile = false;
// System.out.println("yes");
}
else {
canPlaceATile = true;
//System.out.println("no");
}
if (b.get(i).remove) {
b.remove(i);
i--;
}
}
瓷砖的一个示例:
public class wall extends block {
public wall(int x, int y, int rot) {
this.x = x;
this.y = y;
this.rotate = rot;
r = new Rectangle(x - (int) play.camx, y - (int) play.camy, 20, 20);
id = 0;
}
public void tick() {
createCollisionRect();
if (Comp.mr && r.contains(new Point((Comp.mx), (Comp.my)))) {
remove = true;
}
}
public void render(Graphics g) {
ImageIcon i62 = new ImageIcon("res/tiles/wall.png");
img = i62.getImage();
g.drawImage(img, x - (int) play.camx, y - (int) play.camy, null);
g.setColor(Color.red);
// g.drawRect(x -(int)play.camx, y - play.camy, 20,20);
}
}
我想检查是否有任何矩形包含鼠标,如果鼠标位于其中一个矩形内,则将canPlaceATile设置为false。上面的代码不起作用,因为当我打印控制台并将鼠标放在其中一块瓷砖上时,会显示:
yes
no
yes
no
yes
no
yes
no
yes
no
yes
no
yes
no
yes
no
yes
no
yes
no
yes
no
yes
这是当我的鼠标在矩形上时,根本不移动它。如何修复此问题,使我无法将瓷砖放置在瓷砖顶部。她是我放置瓷砖的地方:
public void mousePressed(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON1) {
Comp.ml = true;
if (manager.isplay && play.dragging == false) {
if (play.canPlaceATile) {
if (play.selectedID == 0) {
play.b.add(new wall((Comp.mx / 20) * 20, (Comp.my / 20) * 20, play.selectedRot));
}
}
}
}
}
您的代码缺少示例中使用的一些核心部分。什么是游戏?什么是薪酬?冲突代码是否在游戏循环中执行?不管怎样:我想从你的代码中可以提取到,你正在创建一个简化为墙坐标的碰撞矩形,但是你要检查鼠标坐标,也就是屏幕坐标。但这只是一个很难理解的问题,因为您的变量名是如此的默默无闻,以至于很难理解示例(至少对我来说)。
public void mousePressed(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON1) {
Comp.ml = true;
if (manager.isplay && play.dragging == false) {
if (play.canPlaceATile) {
if (play.selectedID == 0) {
play.b.add(new wall((Comp.mx / 20) * 20, (Comp.my / 20) * 20, play.selectedRot));
}
}
}
}
}