Java Slick2D-检测鼠标悬停在图像上
我试图检测鼠标何时悬停在图像上(播放图像),以及何时单击鼠标时发生了什么 我在屏幕中央绘制了一幅图像,我不知道如何在我自己的代码中实现这一点:Java Slick2D-检测鼠标悬停在图像上,java,lwjgl,slick2d,Java,Lwjgl,Slick2d,我试图检测鼠标何时悬停在图像上(播放图像),以及何时单击鼠标时发生了什么 我在屏幕中央绘制了一幅图像,我不知道如何在我自己的代码中实现这一点: @Override public void init(GameContainer arg0, StateBasedGame arg1) throws SlickException { f = (float) (0.1 * Main_Activity.apg.getHeight() / 180);
@Override
public void init(GameContainer arg0, StateBasedGame arg1) throws SlickException {
f = (float) (0.1 * Main_Activity.apg.getHeight() / 180);
play = new Image("res/play_image.png");
play_Original_Width = play.getWidth();
play_Original_Height = play.getHeight();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
width = screenSize.getWidth();
height = screenSize.getHeight();
}
@Override
public void render(GameContainer arg0, StateBasedGame arg1, Graphics g) throws SlickException {
play.draw(Main_Activity.apg.getWidth() / 2 - ((play_Original_Width * f) / 2),
Main_Activity.apg.getHeight() / 2 - ((play_Original_Height * f) / 2), f);
}
@Override
public void update(GameContainer gc, StateBasedGame sbg, int arg2) throws SlickException {
int x = Mouse.getX();
int y = Mouse.getY();
}
apg:
如何检测鼠标何时悬停在图像上(播放图像)
非常感谢。您只需测试您的x值是否在图像x和图像x+图像宽度之间,y值是否相同 在更新方法中:
int x = Mouse.getX();
int y = Mouse.getY();
int playX = Main_Activity.apg.getWidth() / 2 - ((play_Original_Width * f) / 2);
int playY = Main_Activity.apg.getHeight() / 2 - ((play_Original_Height * f) / 2);
if (x > playX && x < playX+play_Original_Width && y > playY && y < playY + play_Original_Height) {
// then your mouse coordinates are over your image.
}
Rectangle mousePosition = new Rectangle(x,y,1,1);
if(mousePosition.intersects(playImageRectangle)) {//do whatever you need}
在您的方法中:
int x = Mouse.getX();
int y = Mouse.getY();
int playX = Main_Activity.apg.getWidth() / 2 - ((play_Original_Width * f) / 2);
int playY = Main_Activity.apg.getHeight() / 2 - ((play_Original_Height * f) / 2);
if (x > playX && x < playX+play_Original_Width && y > playY && y < playY + play_Original_Height) {
// then your mouse coordinates are over your image.
}
Rectangle mousePosition = new Rectangle(x,y,1,1);
if(mousePosition.intersects(playImageRectangle)) {//do whatever you need}