wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes MouseDragg Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "MouseDragg" Watch "MouseDragg" New topic
Author

MouseDragg

shalini gnana
Ranch Hand

Joined: Sep 18, 2007
Posts: 189
I have drawn number of drawn a rectangles by taking x,y & width from lists..
Now i want to dragg all those rectangle..how can i do it?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41127
    
  45
You'd need to add a MouseMotionListener that can detect when the mouse is clicked and dragged. In the mouseDragged method you'd need to check whether the (x,y) coordinate of the mouse click was on the border (or the vicinity of a border) of one of the rectangles. If it was, you'd start to change the coordinates of the rectangle (and repaint the screen). You don't need to check for when the mouse button gets released - the JVM will just stop calling the mouseDragged method.


Ping & DNS - my free Android networking tools app
shalini gnana
Ranch Hand

Joined: Sep 18, 2007
Posts: 189
I tried in the same way...But its not working...Why its so..
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41127
    
  45
Shalini, I believe we already came to the conclusion that "its not working" is not an adequate description of a problem. You need to tell us what is happening.

You don't need the mousePressed method. mouseDragged will only be called if the mouse button is down, so you can perform the check for containment then.

What is the code for drawing the rectangle?
shalini gnana
Ranch Hand

Joined: Sep 18, 2007
Posts: 189
code for drawing the rectangle:



if i dragg the rectangle drawing its not getting dragged..
so you want me to use mousecliked method for the code to work?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41127
    
  45
One thing that jumps out is that the method to paint in Swing is called paintComponent, not paintComponents.

How are you changing the x/y position of the rectangle that is being moved? You must have code somewhere that iterates through the list of rectangles to determine which one should be moved.
[ February 25, 2008: Message edited by: Ulf Dittmer ]
shalini gnana
Ranch Hand

Joined: Sep 18, 2007
Posts: 189
Sorry,sir..I'm not getting what your trying to say..
I'm not using any list for rectangles...I'm using only for x
and y co-ordinates
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41127
    
  45
I'm not using any list for rectangles

Yes you are, according to the code you posted:
int x = x_cord_list.get(i);
int y = y_cord_list.get(i);
int width = width_list.get(i);

So unless you change what is stored in those lists, you will always paint at the same coordinates.
shalini gnana
Ranch Hand

Joined: Sep 18, 2007
Posts: 189
I can able to drag but the problem is,
the old drawing is remaining in the same
position...how can i solve this?whats the way to change the old
position to the new one

Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41127
    
  45
Where is "drawdig" called - from within paintComponent? Is that all it does?
[ February 25, 2008: Message edited by: Ulf Dittmer ]
shalini gnana
Ranch Hand

Joined: Sep 18, 2007
Posts: 189
Yes,its called inside the paintComponent()...
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41127
    
  45
I just noticed that you aren't clearing the area first. If you do the drawing yourself in paintComponent then you need to do that. Put something like the following at the top of the drawing method (I took this straight out of the Swing Tutorial, which you should really work through):


Why are you still keeping the mousePressed method? That needlessly complicates the code and makes it more brittle.
shalini gnana
Ranch Hand

Joined: Sep 18, 2007
Posts: 189
I have some doubt...
1.why should i have to do that?
2.should i have to add those codes to the
paintComponent() or to drawdig()...
3.should i have to use mouseCliked() method or
some other...
please help me out?


If i drag a rectangle,the old position should
be moved to a new mouse position and repainteed..
In my case,only a copy of the rectangle is painted in
a new mouse position..and the old position is not
repainted..
[ February 26, 2008: Message edited by: shalini gnana ]
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41127
    
  45
1.why should i have to do that?

What is the "that" you're referring to?

2.should i have to add those codes to the paintComponent() or to drawdig()...

I'm not sure what "those codes" refers to, but it doesn't really matter in which method the painting occurs, as long as it's called from paintComponent.

3.should i have to use mouseCliked() method or some other...

Please read what I posted earlier. I've now said repeatedly that you need to implement mouseDragged, but not mousePressed (or any other Mouse[Motion]Listener methods).

If i drag a rectangle,the old position should be moved to a new mouse position and repainteed.. In my case,only a copy of the rectangle is painted in a new mouse position..and the old position is not repainted..

Is your code now clearing the drawing area as I suggested in my previous post?
shalini gnana
Ranch Hand

Joined: Sep 18, 2007
Posts: 189
1.I were talking about the code:
g.setColor(getBackground());
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(getForeground());


2.I'm using the mouseDragged method in my code:


3.I don't have idea,how to clear the old position..Please help me...
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41127
    
  45
1.I were talking about the code:

3.I don't have idea,how to clear the old position..

#1 (which is the code that clears the component) answers #3. That code should be the first thing you do in the paintComponent method.

2.I'm using the mouseDragged method in my code

But you still have this "clickedRectangle" object that is set somewhere else - if your current code still has a mousePressed method, delete it now.
shalini gnana
Ranch Hand

Joined: Sep 18, 2007
Posts: 189
I had removed that mousePressed method..Even then i'm encountered with the same problem...Please help me to rectify it..I'm completely blank
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41127
    
  45
Post the complete paintComponent and mouseDragged methods so we can have a look at it.
shalini gnana
Ranch Hand

Joined: Sep 18, 2007
Posts: 189

Please help me...
[ February 26, 2008: Message edited by: shalini gnana ]
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
Can't help with your posted code, but here's a simple demo of dragging multiple panels

maybe you can adapt some of it for your code.

Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41127
    
  45
Shalini, PatienceIsAVirtue.

Since you've put a fair amount of effort into it, I'm going to post something that works.

By the way, Michael's code is somewhat similar, but it obscures part of the problem because the event handling isn't centralized in one component.
[ February 27, 2008: Message edited by: Ulf Dittmer ]
shalini gnana
Ranch Hand

Joined: Sep 18, 2007
Posts: 189
Wow!!great work!!


I'm using JAppelt,so i'm unable to use JFrame in my program.
I'm finding some problem in using your comcept,
1.I'm calling more than one method in paintComponent(),so i could able to
call that method inside another method..
2.I'm extending the panel in the class ,so how can i create another
panel Object...
3.I created Map and implemented in the same way you,did..I couldn't
able to drag now...


Here is my complete code...





Please help me...
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41127
    
  45
I'm using JAppelt,so i'm unable to use JFrame in my program.

That doesn't really make a difference. Where my code adds a JPanel to a JFrame, yours can add it to the JApplet.

1.I'm calling more than one method in paintComponent(),so i could able to call that method inside another method.

Not following. Why does it matter how many methods are called inside of paintComponent?

2.I'm extending the panel in the class ,so how can i create another panel Object...

I'm not sure what you're asking, but where my code uses a JPanel you'd use an Imagepanel. No other panel is necessary.

3.I created Map and implemented in the same way you,did..I couldn't able to drag now...

Is the mouseDragged method being called with reasonable x/y coordinates?

Why did you replace translate with setLocation? That's doing something different, so you can't expect it to work the same way.

You also need to get rid of the ScrollPane that's part of the Imagepanel - you already have a ScrollPane in the mousedrag class.
shalini gnana
Ranch Hand

Joined: Sep 18, 2007
Posts: 189
In your code you placed everything inside the constructor...
Even the paintComponent()..Its quite difficult
for me to follow.because,i have to place all the
methods that i'm going to call in the paintmethod..I can't keep it everything is a
seperate method.Is there any other way for dragging the
drawing..

Thanks alot...
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41127
    
  45
You don't need another method of dragging/drawing -the one outlined above works nicely- you need to understand and adapt the code that's already there.

If you're not familiar with anonymous classes -which is what allowed me to keep the complete JPanel declaration in the constructor-, note that all the methods are part of the class that extends JPanel. That's what you need to do in your code too, even if you're declaring that class differently.

What specifically are you having difficulties with when you try to adapt my approach to your code? Have you addressed the points I mentioned in my previous post?
[ February 28, 2008: Message edited by: Ulf Dittmer ]
shalini gnana
Ranch Hand

Joined: Sep 18, 2007
Posts: 189
My doubt is, 1.My inner class had extended a panel, as per your code,should i have to use another panel inside the constructor,like you used:



JPanel panel = new JPanel()
{
@Override


2.In my code,i add some methods to paintComponent() should i have to write that method,too inside the constructor... 3.I tried,in the way you said.but nothing was painted in the panel...



I did how you said in the mousedragg

rt.translate((int) (p.getX() - lastP.getX()),(int) (p.getY() - lastP.getY()));
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41127
    
  45
1.My inner class had extended a panel, as per your code,should i have to use another panel inside the constructor

No. The Imagepanel serves as the JPanel to use.

2.In my code,i add some methods to paintComponent() should i have to write that method,too inside the constructor.

You're not using anonymous classes, and thus you need to remove the JPanel constructor call in the Imagepanel constructor. Your class has a name -Imagepanel- and you're creating it in the init method. The other methods live in the same place as the paintComponent method; just like it was/as in your previous code, and in my code above.

Have you tried this without using a ScrollPanel? It's easier to get the basics to work right before adding additional features.
shalini gnana
Ranch Hand

Joined: Sep 18, 2007
Posts: 189
sorry for the repeated posting...
If you don't mind, can you explain me want should i have to
do..

Its enough,to create a map and to modify the mouseDragg method....
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41127
    
  45
Something like this should work if you add the other methods I posted earlier:

[ February 28, 2008: Message edited by: Ulf Dittmer ]
shalini gnana
Ranch Hand

Joined: Sep 18, 2007
Posts: 189
I tried but still its not working...

Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41127
    
  45
I suggest you completely remove the Imagepanel constructor and the Imagepanel.panel method. As far as I can tell they do very little useful (and some harm by adding an additional set of event listeners), and you still have that ScrollPane in there that needlessly complicated things; it may well be the source of the problem.

If it still doesn't work after you did that, tell us in detail what is actually happening - is the mouseDragged method being called at the appropriate times with reasonable values?
shalini gnana
Ranch Hand

Joined: Sep 18, 2007
Posts: 189
If its a single rectangle- hard coded,(i.e., not an array of rectangle)then i can able to
drag(inspite of the scrollpane)...but the problem arises only when a list of values comes.
shalini gnana
Ranch Hand

Joined: Sep 18, 2007
Posts: 189
Any help,please!!
[ March 03, 2008: Message edited by: shalini gnana ]
shalini gnana
Ranch Hand

Joined: Sep 18, 2007
Posts: 189
Anyone help me please...
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41127
    
  45
Originally posted by Ulf Dittmer:
If it still doesn't work after you did that, tell us in detail what is actually happening - is the mouseDragged method being called at the appropriate times with reasonable values?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: MouseDragg
 
Similar Threads
I am trying to create dynamic text
Autoscrolling for marquee selection
Mousedragging not working after updating the array.
Swing related
Scrolling Components