Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

mouse events in same class

 
Nathalie Monvoisin
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi, here is my problem. i have my paint application, i use the following mouse event to draw my shapes(mousePressed, mouseReleased, mouseDragged). Now after the user paint a circle for example, he must be able to drag the shape; to be able to do this. i think i must again implement the different mouse Events. I have done it and the drag is not working. can anyone tell me, how can i achieve this. can we implement multiple (mousePressed, mouseReleased, mouseDragged) in the same java class?? Thank you
 
Tushar Goel
Ranch Hand
Posts: 927
4
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Nathalie, oracle docs are having very good explanation. They have used same scenario as you mentioned but they have used square instead of circle.
You may check it at: demo
 
Nathalie Monvoisin
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can i implement mouse pressed ans mouse release twice in the same java class?? because in the oracle doc. when loading the form the square is already on the the Panel. In my application when loading it the frame is empty, then the user use the mouse to draw the circle then it should be able to drag the shape
 
Campbell Ritchie
Sheriff
Posts: 51388
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you mean can you add two listeners to the same Component? You can. But why?
 
Nathalie Monvoisin
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
one to draw. and another one for dragging
 
Paul Clapham
Sheriff
Posts: 21567
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can add two MouseListeners to the same component, yes. But when a mouse event occurs, both of those listeners will be notified. If you were hoping for a solution where one listener would be notified when the user was drawing and the other listener would be notified when the user was dragging, well, that isn't going to happen. It would be better to have only one MouseListener, and have it distinguish between drawing and dragging.
 
Nathalie Monvoisin
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how can i make it distinguish between drawing and dragging? create 2 classes one is triggered when drawing and the other one triggered when dragging??
 
Paul Clapham
Sheriff
Posts: 21567
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You write code which does one thing when the mouse actions imply that drawing is taking place, and which does another thing when the mouse actions imply that dragging is taking place. It makes no difference whether this code is in one class or two classes or seven classes. Start by analyzing what mouse events occur when the user is drawing, and what mouse events occur when the user is dragging. Hopefully there is some way to distinguish between those two series of events. At any rate you can't write any code until you have figured out what needs to be done, which is what that analysis process is for.
 
Nathalie Monvoisin
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i know what my mouse actions do for my drawing. my moussePressed take my current coordinate. my mousseReleased draw my shape from my starting point to ending point and my mousseDragged get the final x & y position after the mouse is dragged. i need the same set of mouse actions to be able to drag the shape after it is drawn
 
Paul Clapham
Sheriff
Posts: 21567
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, okay. Then your code needs to know when a shape has been drawn, doesn't it?

Or perhaps there's more to it than that. What else is required for the user to tell the code to drag a shape? Does the first mouse press have to be anywhere inside the shape, or does it have to be on the border, or what? Perhaps noticing that the mouse press is already inside a shape would tell you that dragging is about to happen?

If so, then what happens if there are two shapes overlapping? How do you know which one to drag? Or is only one shape allowed?

That's what I mean by the design you need to do before you start coding. You have to understand what sort of things can happen and what you should do in each case.
 
Nathalie Monvoisin
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My application is a paint application, on the left i have a series of button. When the user selects an option he can draw a specific shape. i idea was when the shape is drawn, if the user click on the shape he will be able to drag it any where on the frame.
 
Campbell Ritchie
Sheriff
Posts: 51388
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving discussion to our GUIs forum.
 
Paul Clapham
Sheriff
Posts: 21567
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, so there's only one shape. So you need to know when the user has finished drawing the shape. Then you set a flag in the class which tells you that, and every subsequent mouse-down event is the start of a drag operation.
 
Carey Brown
Bartender
Posts: 1939
24
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nathalie Monvoisin wrote:My application is a paint application, on the left i have a series of button. When the user selects an option he can draw a specific shape. i idea was when the shape is drawn, if the user click on the shape he will be able to drag it any where on the frame.

If you want to determine if the user clicked on a particular shape:

Bounding boxes are the simplest and least CPU intensive check you can make. Simply check mouse XY against shape minX,minY,maxX,maxY and see if it is contained. If not then you're not inside the shape. This test is not definitive though and for most shapes (i.e. not rectangles) you'll need to apply some formula to see if the mouse XY is truly inside the shape. For shapes like circles or ellipses you should be able to find some formulas on the internet.
 
Campbell Ritchie
Sheriff
Posts: 51388
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A lot of shape classes in the java.awt.geom package have methods which tell you whether you are in the shape or intersecting the shape. Check them out to see whether they do what you want.
 
Nathalie Monvoisin
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can i write a function what will be applied on the shape after it has been drawn?? The function will move the shape
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic