Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By eRaaaa

Thread: Problem with rect.contains mouse x and y

  1. #1
    Lionlev is offline Senior Member
    Join Date
    May 2012
    Posts
    172
    Rep Power
    3

    Question (SSCCE PROVIDED) Problem with rect.contains mouse x and y

    Hi, something is wrong with my code..
    Basically I want my boolean "mouseRolled" turn to true
    Java Code:
    if(MenuUI.loginBt.getBounds().contains(e.getX(), e.getY())){
    			
    			MenuUI.mouseRolled = true;
    		} else {
    			MenuUI.mouseRolled = false;
    		}
    (MenuUI.loginBt is the rectangle that repaints every 100 milliseconds)

    When I roll my mouse on to that square nothing happens HOWEVER when I move the mouse little bit upward by about 25 pixels, the mouseRolled turns true
    Which is quite weird...

    (Go down for the short source code)
    Last edited by Lionlev; 10-18-2012 at 09:44 PM.
    WARNING I am Russian so it's possible that I wont understand you correctly...

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,304
    Rep Power
    20

    Default Re: Problem with rect.contains mouse x and y

    To get better help sooner, post a [_SSCCE_|http://mindprod.com/jgloss/sscce.html] that clearly demonstrates your problem.

    This advice applies to any future questions you may have.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    Lionlev is offline Senior Member
    Join Date
    May 2012
    Posts
    172
    Rep Power
    3

    Default Re: Problem with rect.contains mouse x and y

    ok, I'll do it
    WARNING I am Russian so it's possible that I wont understand you correctly...

  4. #4
    Lionlev is offline Senior Member
    Join Date
    May 2012
    Posts
    172
    Rep Power
    3

    Default Re: Problem with rect.contains mouse x and y

    Ok here is the SSCCE, I've made it short as I could:
    3 classes: BuildUI, PaintUI and ListenMouse
    BuildUI:
    Java Code:
    import javax.swing.JFrame;
    public class BuildUI implements Runnable{
    	public static PaintUI paintIt = new PaintUI();
    	public void start(){
    		new Thread(this).start();
    	}
    	public static void main(String[] args){
    		BuildUI window = new BuildUI();
    		JFrame w = new JFrame();
    		w.setSize(600,600);
    		w.setVisible(true);
    		w.add(paintIt);
    		w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		w.setResizable(false);
    		window.start();
    		
    		w.addMouseMotionListener(new ListenMouse());
    	}
    	public void run() {
    		while(true){
    			paintIt.repaint();
    			try {
    				Thread.sleep(1000/60);
    			} catch (InterruptedException e) {
    				e.printStackTrace();
    			}
    		}
    	}
    }
    PaintUI
    Java Code:
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Rectangle;
    import java.awt.image.BufferedImage;
    import javax.swing.JPanel;
    public class PaintUI extends JPanel{
    	private static final long serialVersionUID = 1L;
    	public static boolean mouseRolled = false;
    	public static Rectangle loginBt = new Rectangle(100,100,50,50);
    	public BufferedImage loginBtn;
    	
    	public void paintComponent(Graphics g){
    		if(mouseRolled){
    			g.setColor(Color.GREEN);
    		} else {
    			g.setColor(Color.CYAN);
    		}
    		g.fillRect(loginBt.x,loginBt.y, loginBt.width, loginBt.height);
    	}
    }
    ListenMouse
    Java Code:
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseMotionListener;
    public class ListenMouse implements MouseMotionListener{
    	public void mouseDragged(MouseEvent e) {
    	}
    	public void mouseMoved(MouseEvent e) {
    		if(PaintUI.loginBt.getBounds().contains(e.getX(), e.getY())){
    			
    			PaintUI.mouseRolled = true;
    		} else {
    			PaintUI.mouseRolled = false;
    		}
    	}
    }
    :DDD
    WARNING I am Russian so it's possible that I wont understand you correctly...

  5. #5
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default Re: Problem with rect.contains mouse x and y

    You have added the MouseListener to the JFrame, so you have an offset because of the size of title bar!
    Try to add the MouseListener to the contentpane of your jframe!

    w.addMouseMotionListener(new ListenMouse());
    ->
    w.getContentPane().addMouseMotionListener(new ListenMouse());
    Lionlev likes this.

  6. #6
    Lionlev is offline Senior Member
    Join Date
    May 2012
    Posts
    172
    Rep Power
    3

    Default Re: Problem with rect.contains mouse x and y

    Quote Originally Posted by eRaaaa View Post
    You have added the MouseListener to the JFrame, so you have an offset because of the size of title bar!
    Try to add the MouseListener to the contentpane of your jframe!

    w.addMouseMotionListener(new ListenMouse());
    ->
    w.getContentPane().addMouseMotionListener(new ListenMouse());
    Oh wow, thanks! That fixes everything rep++ to you!
    WARNING I am Russian so it's possible that I wont understand you correctly...

Similar Threads

  1. Java platformer, rect collision
    By Lionlev in forum Advanced Java
    Replies: 8
    Last Post: 10-19-2012, 01:41 PM
  2. Distance between a point and a rect
    By branquinho in forum New To Java
    Replies: 3
    Last Post: 01-04-2012, 10:11 AM
  3. Replies: 4
    Last Post: 10-17-2011, 08:09 PM
  4. Replies: 0
    Last Post: 10-15-2011, 10:56 PM
  5. MouseEntered to rect
    By g6pd in forum New To Java
    Replies: 3
    Last Post: 03-10-2011, 01:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •