Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

java mouseListener movement bug

Posted on 2012-04-11
2
269 Views
Last Modified: 2012-04-17
Hi,

I created an application where you can set several rectangles with Strings in on a panel.
Each of these rectangles can be moved by click-dragging.

Code:
public void mousePressed(MouseEvent e) {
		preX = this.getPosition().x - e.getX();
	    preY = this.getPosition().y - e.getY();
	    
	    if (getBounds().contains(e.getX(), e.getY())){
		    this.pressed = true;
	    }
	}

	public void mouseReleased(MouseEvent e) {
		this.pressed = false;
	}

	public void mouseDragged(MouseEvent e) {
		if(this.pressed){
			System.out.println(e.getX() + " - " + e.getY());
			setPosition(new Point(e.getPoint().x + preX, e.getPoint().y + preY));
		}
	}

Open in new window


Now I found a bug which after some thinking seems logic but I can't seem to figure out a simple solution for it to fix.

Bug is the following:
When two rectangles are intersecting and you click (on the intersection) you move both of the rectangles instead of 1.

This is logic, but what could I do to fix this?
0
Comment
Question by:Mutsop
2 Comments
 
LVL 16

Accepted Solution

by:
imladris earned 500 total points
ID: 37833231
Simplest solution I can think of is to maintain a list of rectangle objects. Then, when you find that a rectangle contains the mouse event call a method to set pressed. This method should have access to the list of the rectangles. The method should ensure that only *one* rectangle ever has the pressed flag set to true.

So, whenever it is called to set it to true, it should first reset the pressed flag for all rectangles. This will ensure that there is only ever one rectangle that will be moved.
0
 
LVL 3

Author Closing Comment

by:Mutsop
ID: 37855560
Thanks
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
ForLoop Example 3 52
difference of if loops 23 62
hibernate jars 4 45
jboss 7.1 start up error 1 31
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This video teaches viewers about errors in exception handling.

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question