Finding if my mouse is inside a rectangle in Java

笑着哭i 提交于 2019-12-24 04:56:33

问题


I'm starting to develop a game and I need to be able to see if my mouse is inside a rectangle, I've tried using .contains for a rectangle but i can't seem to get it to work, i'll paste my code below, any help would be greatly appreciated! :)

public boolean isMouseOver(GameContainer gc){
    r = new Rectangle(getX(), getY(),getWidth(),getHeight());
    Input input = gc.getInput();
    xpos = input.getMouseX();
    ypos = input.getMouseY();
    return r.contains(xpos, ypos);
}

This is the method i'm trying to use, but it keeps returning false when the mouse is inside the rectangle. obviously, I initiated xpos, ypos, and the rectangle further up and I called the method in the update method of the class i'm trying to use it in.


回答1:


You have two points for your mouse, it's x and y pos.

int mouseX = gc.getInput().getMouseX();
int mouseY = gc.getInput().getMouseY();

And we have a rectangle

Rectangle rec = new Rectangle( 100, 100, 200, 200 );

So we can check

if ( mouseX >= rec.getMinX() && mouseX <= rec.getMaxX )   // check if X is within range
   && ( mouseY >= rec.getMinY() && mouseY <= rec.getMaxY) // check if y is within range

Or now that we know our X value has to be greater than the rectangles low value but less than it's high value, and the same for Y lets check the contains function

contains(float xp, float yp, float xr, float yr, float widthr, float heightr)

xp - The x coordinate of the point to check
yp - The y coordinate of the point to check
xr - The x coordinate of the rectangle
yr - The y coordinate of the rectangle
widthr - The width of the rectangle
heightr - The height of the rectangle

So I'd say

contains( mouseX, mouseY, rec.getMinX(), rect.getMinY(), rec.getWidth(), rec.getHeight() )

Perhaps something was going wrong here?




回答2:


Did you display the bounds of the Rectangle and the mouse position?

I would guess the Rectangle is relative to your component and the mouse is relative to the screen.

You can use the SwingUtilities class to do point conversions to make sure the points are relative to the same component.




回答3:


Try out somethings like this..

Just run this below program, I hope you will get the answer:

package mouse;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.HeadlessException;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import javax.swing.JFrame;

public class Mouse extends JFrame implements MouseMotionListener {

private Image dbImage;
private Graphics dbg;
int mx, my;
boolean mouseDragged;

public Mouse() throws HeadlessException {
    setSize(400, 300);
    setResizable(false);
    setVisible(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    addMouseMotionListener(this);
}

public void paint(Graphics g) {
    dbImage = createImage(getWidth(), getHeight());
    dbg = dbImage.getGraphics();
    paintComponent(dbg);
    g.drawImage(dbImage, 0, 0, this);
}

public void paintComponent(Graphics g) {


    if (mouseDragged){
        g.setColor(Color.DARK_GRAY);
        g.fillRect(0, 0, getWidth(), getHeight());
        g.setColor(Color.LIGHT_GRAY);
        g.fillRect(mx, my, 20, 20);

    }else{
        g.setColor(Color.LIGHT_GRAY);
        g.fillRect(0, 0, getWidth(), getHeight());
        g.setColor(Color.DARK_GRAY);
        g.fillRect(mx, my, 20, 20);

    }
    repaint();

}

public static void main(String[] args) {
    Mouse mouse = new Mouse();
}

@Override
public void mouseDragged(MouseEvent e) {
    mx = e.getX() - 10;
    my = e.getY() - 10;
    mouseDragged = true;
    e.consume();
}

@Override
public void mouseMoved(MouseEvent e) {
    mx = e.getX();
    my = e.getY();
    mouseDragged = false;
    e.consume();
}

}

Also there are lot of videos on you tube. I will post the link as well. Check this youtube channel : http://www.youtube.com/watch?v=PopdTUzizDA



来源:https://stackoverflow.com/questions/17238502/finding-if-my-mouse-is-inside-a-rectangle-in-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!