Solved

java mouseListener movement bug

Posted on 2012-04-11
2
270 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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This video teaches viewers about errors in exception handling.

679 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