Results 1 to 6 of 6
- 10-18-2012, 07:25 PM #1
Senior Member
- Join Date
- May 2012
- Posts
- 170
- Rep Power
- 1
(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
(MenuUI.loginBt is the rectangle that repaints every 100 milliseconds)Java Code:if(MenuUI.loginBt.getBounds().contains(e.getX(), e.getY())){ MenuUI.mouseRolled = true; } else { MenuUI.mouseRolled = false; }
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...
- 10-18-2012, 09:00 PM #2
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.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 10-18-2012, 09:12 PM #3
Senior Member
- Join Date
- May 2012
- Posts
- 170
- Rep Power
- 1
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...
- 10-18-2012, 09:43 PM #4
Senior Member
- Join Date
- May 2012
- Posts
- 170
- Rep Power
- 1
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:
PaintUIJava 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(); } } } }
ListenMouseJava 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); } }
:DDDJava 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; } } }WARNING I am Russian so it's possible that I wont understand you correctly...
- 10-18-2012, 10:53 PM #5
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
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());
- 10-18-2012, 10:57 PM #6
Senior Member
- Join Date
- May 2012
- Posts
- 170
- Rep Power
- 1
Similar Threads
-
Java platformer, rect collision
By Lionlev in forum Advanced JavaReplies: 8Last Post: 10-19-2012, 01:41 PM -
Distance between a point and a rect
By branquinho in forum New To JavaReplies: 3Last Post: 01-04-2012, 10:11 AM -
How to find the edges/border of a rect? (java.awt)
By Naxix in forum New To JavaReplies: 4Last Post: 10-17-2011, 08:09 PM -
Detect collision between two objects(rect and oval)?
By Naxix in forum Java AppletsReplies: 0Last Post: 10-15-2011, 10:56 PM -
MouseEntered to rect
By g6pd in forum New To JavaReplies: 3Last Post: 03-10-2011, 01:52 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks