Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

java mouseListener movement bug

Posted on 2012-04-11
2
Medium Priority
?
276 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
[X]
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
2 Comments
 
LVL 16

Accepted Solution

by:
imladris earned 2000 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

704 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