I am trying to implement the rotation of an object on a canvas. You guys have already helped me with setting up the objects on a canvas and it works fine now. The default for PopupMenus seems to be - on Windows anyway - to wait until the mouse is released. Is there any way to override this as I want the PopupMenu to show when the mouse is rightclicked.
I include the code and you will see what the problem is. The only mouse event that the method isPopupTrigger() responds to is mouseReleased. The program presents two pop-up menus. One when the canvas is clicked and one when the object on the canvas is clicked. I am trying to achieve the rotating of the clicked object. I think my whole logic may be a little haywire. I need to rotate the object around by dragging it.
The default for PopupMenus seems to be - on Windows anyway - to wait until the mouse is released. Is there any way to override this as I want the PopupMenu to show when the mouse is rightclicked.
I am a bit confused here. What exactly are you trying to do?
1) Show a popup on right click? Easiest way to do this is to use JComponent#setComponentPopupMenu(). This way you don't have to bother adding listeners, figuring out if the mouse event is a popup trigger and showing the popup.
2) ...show when the mouse is rightclicked. A "click" is technically a combination of a "press"+"release". Now if you want to show the popup on a "press" you can have your mouse listener call a JPopupMenu#show() on mousePressed.
Yes. I am not making quite clear what I am trying to do. If I click on a object such as a Line with a right click I want a pop up menu to comeup which has a Popup MenuItem entitled "Rotate Object". When I click on this MeuuItem I want to then left click on the object and then drag it through a rotation.
The real problem for me is understaning the code in the Popup MenuItem ActionListener to do this.
If I set up a class for each piece of drawing - ie a class for a Line, a class for a particular shape then I presume that I can use the setComponentPopupMenu() on each of the instantiated classes.
I have highlighted the relevant code. The MyInterp class and PathWrangler2 class have been changed.
Windows it seems from my reading waits for the MouseReleased before the PopupMenu appears - it is just what it does - irritating but that is Windows - anyway some people use Windows.
When the PopupMenu appears I see the boolean - rotating - to true in the PathWrangler2 class through an ActionListener. This means that when someone next clicks on the object the MousePressed method moves to a certain snippet of the code and rotates the object.
The actual rotating code I took from a Sun forum and just used for my own ends.
I have highlighted the code added.
The Main class - no code changed here
MyInterp code 1 line of code added in the ActionListener lst 4
The code for PathWrangler2 where most of the changes have occured
The rotating does not work well if you do not click back on an object and it whirls too quickly.