Solved

java mouseListener movement bug

Posted on 2012-04-11
2
261 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
Comment Utility
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
Comment Utility
Thanks
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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 will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
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…

771 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

11 Experts available now in Live!

Get 1:1 Help Now