2

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.

Angus Allman
  • 511
  • 1
  • 6
  • 10
  • 1
    I'd add debug print statements to show exactly what xpos and ypos equal when your mouse is in the rectangle and when it's out of the rectangle. Perhaps the method does not work exactly as you expect. – JohnFilleau Jun 21 '13 at 15:08
  • Well, just use a if with 4 conditions and you're good. – Stephane Mathis Jun 21 '13 at 15:08
  • The problem is not the rectangle `contains`, but screen coordinates and _relative_ coordinates where (x, y) are locally (0, 0). There are component wise conversion functions, but you have to look for yourself. – Joop Eggen Jul 12 '13 at 07:30

3 Answers3

3

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?

Bryan Abrams
  • 327
  • 1
  • 8
0

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.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • I've just tried drawing a square around the mouse, and it's nowhere near it (it's down and to the right, but the distance to the mouse changes the closer to the top left corner get)so i assume its what camickr said is wrong, but could you go into a bit more depth on how to fix it please? – Angus Allman Jun 21 '13 at 20:17
0

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

dharam
  • 7,882
  • 15
  • 65
  • 93