Java 鼠标适配器
Java MouseAdapter
所以我有我的自定义鼠标适配器。如果出现以下情况,我想调用一个方法:
x 和 y 坐标在一个矩形中,例如坐标 x = 40 y = 40 和宽度 = 10 长度 = 10。如何检查(鼠标的)x 和 y 坐标是否在矩形中?
我试过这个:
@Override
public void mouseClicked(MouseEvent e) {
if(isXinBounds(e.getX()) || isYinBounds(e.getY())) {
//This happens if the mouse click is in the rectangle
}
}
//Check if the x coordinate of the mouse click is in the rectangle
private boolean isXinBounds(int x) {
if(x <= ob.getBounds().getX() + ob.getBounds().getWidth() / 2 && x >= ob.getBounds().getX() - ob.getBounds().getWidth() / 2) return true;
return false;
}
//Check if the y coordinate of the mouse click is in the rectangle
private boolean isYinBounds(int y) {
if(y <= ob.getBounds().getY() + ob.getBounds().getHeight() / 2 && y >= ob.getBounds().getY() - ob.getBounds().getHeight() / 2) return true;
return false;
}
但这没有用
您正在创建一个非常奇怪的公式来检查您的点击是否在矩形的边界内。
如果我们设置矩形坐标:
x = 10
y = 10
width = 50
height = 50
并使用这些值创建一个新的矩形:
Rectangle rect = new Rectangle(x, y, widht, height)
然后我们可以对 X 和 Y 都这样做
private boolean xIsInBounds(int x) {
if (x >= rect.x && x <= rect.width + x) {
return true;
}
return false;
}
因为我们检查 X 和 Y 值,因为它们将始终相同,但我们必须将 X 和 Y 值分别添加到宽度和高度,因为它由于 X 和Y 值不在 0,0。
您的代码中的另一个问题是您要问:
if (xIsInBounds(...) || yIsInBounds(...)) { ... }
当它应该是 && 而不是 ||.
这是以 Minimal Reproducible Example
形式产生以下输出的代码
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class ClickOnBounds extends MouseAdapter {
private JFrame frame;
private JPanel pane;
private static final int X_COORD = 10;
private static final int Y_COORD = 10;
private static final int RECT_WIDTH = 50;
private static final int RECT_HEIGHT = 50;
private Rectangle rect = new Rectangle(X_COORD, Y_COORD, RECT_WIDTH, RECT_HEIGHT);
@SuppressWarnings("serial")
private void createAndShowGUI() {
frame = new JFrame(this.getClass().getSimpleName());
pane = new JPanel() {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.draw(rect);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(70, 70);
}
};
pane.addMouseListener(this);
frame.add(pane);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
@Override
public void mouseClicked(MouseEvent e) {
super.mouseClicked(e);
if (xIsInBound(e.getX()) && yIsInBound(e.getY())) {
System.out.println("Yes! " + e.getX() + " " + e.getY());
} else {
System.out.println("No! " + e.getX() + " " + e.getY());
}
}
private boolean xIsInBound(int x) {
if (x >= rect.x && x <= rect.width + X_COORD) {
return true;
}
return false;
}
private boolean yIsInBound(int y) {
if (y >= rect.y && y <= rect.height + Y_COORD) {
return true;
}
return false;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new ClickOnBounds()::createAndShowGUI);
}
}
而且,正如您在上图中所见,它运行完美。
The x and y coordinates are in a Rectangle
Rectangle
class 已经支持 contains(…)
方法。
您可以只使用:
if (yourRectangle.contains(e.getPoint())
// do something
所以我有我的自定义鼠标适配器。如果出现以下情况,我想调用一个方法: x 和 y 坐标在一个矩形中,例如坐标 x = 40 y = 40 和宽度 = 10 长度 = 10。如何检查(鼠标的)x 和 y 坐标是否在矩形中? 我试过这个:
@Override
public void mouseClicked(MouseEvent e) {
if(isXinBounds(e.getX()) || isYinBounds(e.getY())) {
//This happens if the mouse click is in the rectangle
}
}
//Check if the x coordinate of the mouse click is in the rectangle
private boolean isXinBounds(int x) {
if(x <= ob.getBounds().getX() + ob.getBounds().getWidth() / 2 && x >= ob.getBounds().getX() - ob.getBounds().getWidth() / 2) return true;
return false;
}
//Check if the y coordinate of the mouse click is in the rectangle
private boolean isYinBounds(int y) {
if(y <= ob.getBounds().getY() + ob.getBounds().getHeight() / 2 && y >= ob.getBounds().getY() - ob.getBounds().getHeight() / 2) return true;
return false;
}
但这没有用
您正在创建一个非常奇怪的公式来检查您的点击是否在矩形的边界内。
如果我们设置矩形坐标:
x = 10
y = 10
width = 50
height = 50
并使用这些值创建一个新的矩形:
Rectangle rect = new Rectangle(x, y, widht, height)
然后我们可以对 X 和 Y 都这样做
private boolean xIsInBounds(int x) {
if (x >= rect.x && x <= rect.width + x) {
return true;
}
return false;
}
因为我们检查 X 和 Y 值,因为它们将始终相同,但我们必须将 X 和 Y 值分别添加到宽度和高度,因为它由于 X 和Y 值不在 0,0。
您的代码中的另一个问题是您要问:
if (xIsInBounds(...) || yIsInBounds(...)) { ... }
当它应该是 && 而不是 ||.
这是以 Minimal Reproducible Example
形式产生以下输出的代码import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class ClickOnBounds extends MouseAdapter {
private JFrame frame;
private JPanel pane;
private static final int X_COORD = 10;
private static final int Y_COORD = 10;
private static final int RECT_WIDTH = 50;
private static final int RECT_HEIGHT = 50;
private Rectangle rect = new Rectangle(X_COORD, Y_COORD, RECT_WIDTH, RECT_HEIGHT);
@SuppressWarnings("serial")
private void createAndShowGUI() {
frame = new JFrame(this.getClass().getSimpleName());
pane = new JPanel() {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.draw(rect);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(70, 70);
}
};
pane.addMouseListener(this);
frame.add(pane);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
@Override
public void mouseClicked(MouseEvent e) {
super.mouseClicked(e);
if (xIsInBound(e.getX()) && yIsInBound(e.getY())) {
System.out.println("Yes! " + e.getX() + " " + e.getY());
} else {
System.out.println("No! " + e.getX() + " " + e.getY());
}
}
private boolean xIsInBound(int x) {
if (x >= rect.x && x <= rect.width + X_COORD) {
return true;
}
return false;
}
private boolean yIsInBound(int y) {
if (y >= rect.y && y <= rect.height + Y_COORD) {
return true;
}
return false;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new ClickOnBounds()::createAndShowGUI);
}
}
而且,正如您在上图中所见,它运行完美。
The x and y coordinates are in a Rectangle
Rectangle
class 已经支持 contains(…)
方法。
您可以只使用:
if (yourRectangle.contains(e.getPoint())
// do something