Solved

java mouseListener movement bug

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
topping3 challenge 14 70
java. non-English characters encoding problem. intellij idea 3 74
groovy example issue 10 90
Impossible to extract MSI from new JAVA releases 2 49
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

895 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now