Results 1 to 16 of 16
- 02-23-2012, 10:45 PM #1
Senior Member
- Join Date
- Nov 2011
- Location
- Turkey
- Posts
- 378
- Blog Entries
- 24
- Rep Power
- 2
Why is my frame moving upwards even though I do not move it in upper direction?
Sample output:Java Code:@Override public void mousePressed(MouseEvent me) { oldPosX = me.getX(); oldPosY = me.getY(); System.out.println("Old pos x:" + oldPosX); System.out.println("Old pos y:" + oldPosY); } @Override public void mouseReleased(MouseEvent me) { newPosX = me.getX(); newPosY = me.getY(); int moveX = newPosX - oldPosX; int moveY = newPosY - oldPosY; System.out.println("You moved the mouse in x direction :" + moveX); System.out.println("You moved the mouse in y direction :" + moveY); mainFrame.setLocation((oldPosX - moveX), (oldPosY - moveY)); System.out.println("New pos x:" + newPosX); System.out.println("New pos y" + newPosY); }
Old pos x:222
Old pos y:12
You moved the mouse in x direction :33
You moved the mouse in y direction :0
New pos x:255
New pos y12
In this situation, my frame ends up somewhere higher on my screen, although newPosY is 12 and according to my formula:
oldPosY - moveY
it should end up in where it was, 12 - 0 = 12
What am I doing wrong, please help?
( Also a very bonus question: Can I make my frame visible as I move it around ? Right now it looks like it is not moving at all until I release the mouse )
- 02-23-2012, 10:52 PM #2
Senior Member
- Join Date
- Nov 2011
- Location
- Turkey
- Posts
- 378
- Blog Entries
- 24
- Rep Power
- 2
Re: Why is my frame moving upwards even though I do not move it in upper direction?
I added mainFrame.getLocation to my code.
When I press on the component I get:
Old pos x:294
Old pos y:4
java.awt.Point[x=283,y=170]
So it seems like the mousepressed and location of component are ok, but the Y values are very very strange ?
-
Re: Why is my frame moving upwards even though I do not move it in upper direction?
What is your MouseListener added to? Do you want to use and keep track of location on Screen?
- 02-23-2012, 11:04 PM #4
Senior Member
- Join Date
- Nov 2011
- Location
- Turkey
- Posts
- 378
- Blog Entries
- 24
- Rep Power
- 2
Re: Why is my frame moving upwards even though I do not move it in upper direction?
It is added to a MenuBar.
but MenuBar has a "*mainFrame" reference type, that is pointing to the JFrame object that the Menubar is on.
Because my mainFrame is undecorated, I want the user to press on the MenuBar and move the frame around.
-
Re: Why is my frame moving upwards even though I do not move it in upper direction?
Consider creating and posting an sscce. You know the drill.
- 02-23-2012, 11:12 PM #6
Senior Member
- Join Date
- Nov 2011
- Location
- Turkey
- Posts
- 378
- Blog Entries
- 24
- Rep Power
- 2
Re: Why is my frame moving upwards even though I do not move it in upper direction?
Java Code:public MainFrame(){ mainPanel = new MainPanel(); this.setJMenuBar(menuBar); // some more code here } // end MainFrame constructorJava Code:public class MainMenuBar extends JMenuBar { private JMenu fileMenu; private MainFrame mainFrame; int oldPosX; int oldPosY; int newPosX; int newPosY; public MainMenuBar(MainFrame frame) { mainFrame = frame; fileMenu = new FileMenu(); this.add(fileMenu); this.addMouseListener(new MenuBarMouseListener()); } class MenuBarMouseListener implements MouseListener { @Override public void mouseClicked(MouseEvent me) { //System.out.println("Mouse clicked!"); } @Override public void mouseEntered(MouseEvent arg0) { // TODO Auto-generated method stub } @Override public void mousePressed(MouseEvent me) { oldPosX = me.getX(); oldPosY = me.getY(); System.out.println("Old pos x:" + oldPosX); System.out.println("Old pos y:" + oldPosY); System.out.println(mainFrame.getLocation()); } @Override public void mouseReleased(MouseEvent me) { newPosX = me.getX(); newPosY = me.getY(); int moveX = newPosX - oldPosX; int moveY = newPosY - oldPosY; System.out.println("You moved the mouse in x direction :" + moveX); System.out.println("You moved the mouse in y direction :" + moveY); mainFrame.setLocation((oldPosX - moveX), (oldPosY - moveY)); mainFrame.validate(); mainFrame.repaint(); System.out.println("New pos x:" + newPosX); System.out.println("New pos y" + newPosY); } } // end class MenuBarMouseListener } // end class MainMenuBar
- 02-23-2012, 11:17 PM #7
Senior Member
- Join Date
- Nov 2011
- Location
- Turkey
- Posts
- 378
- Blog Entries
- 24
- Rep Power
- 2
Re: Why is my frame moving upwards even though I do not move it in upper direction?
A sample output:
Old pos x:231
Old pos y:16
java.awt.Point[x=283,y=170]
You moved the mouse in x direction :0
You moved the mouse in y direction :0
New pos x:231
New pos y16
java.awt.Point[x=231,y=16]
As you can see the "y" value of the mainFrame changes from 170 to 16, altough Old pos y:16 = New pos y and i moved the mouse in y direction: 0
- 02-23-2012, 11:29 PM #8
Senior Member
- Join Date
- Nov 2011
- Location
- Turkey
- Posts
- 378
- Blog Entries
- 24
- Rep Power
- 2
Re: Why is my frame moving upwards even though I do not move it in upper direction?
This question is on hold for a while, please do not spend time on it. I am trying something.. I will post a new message if i cant solve this.
-
Re: Why is my frame moving upwards even though I do not move it in upper direction?
Well, best of luck with this. Again, if you need help, you've have a better chance of getting results if you create and post an SSCCE
-
Re: Why is my frame moving upwards even though I do not move it in upper direction?
- 02-23-2012, 11:30 PM #11
Senior Member
- Join Date
- Nov 2011
- Location
- Turkey
- Posts
- 378
- Blog Entries
- 24
- Rep Power
- 2
Re: Why is my frame moving upwards even though I do not move it in upper direction?
Isnt what I posted an SSCCE 4 posts above ?
- 02-23-2012, 11:32 PM #12
Senior Member
- Join Date
- Nov 2011
- Location
- Turkey
- Posts
- 378
- Blog Entries
- 24
- Rep Power
- 2
Re: Why is my frame moving upwards even though I do not move it in upper direction?
Oh sorry, it has to be compilable ? Sorry.
I will no matter I solve it or not.
Give me a few minutes.
- 02-23-2012, 11:38 PM #13
Senior Member
- Join Date
- Nov 2011
- Location
- Turkey
- Posts
- 378
- Blog Entries
- 24
- Rep Power
- 2
Re: Why is my frame moving upwards even though I do not move it in upper direction?
If anyone is interested and the solution is not clear, I can try to explain.Java Code:@Override public void mousePressed(MouseEvent me) { whereMouseWas = me.getLocationOnScreen(); whereFrameWas = mainFrame.getLocation(); } @Override public void mouseReleased(MouseEvent me) { whereMouseWillBe = me.getLocationOnScreen(); mainFrame.setLocation(whereFrameWas.x-(whereMouseWas.x-whereMouseWillBe.x),whereFrameWas.y-(whereMouseWas.y-whereMouseWillBe.y));
It works fine now.
- 02-24-2012, 03:52 AM #14
Re: Why is my frame moving upwards even though I do not move it in upper direction?
I like the descriptive variable names.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
-
- 02-24-2012, 10:12 AM #16
Senior Member
- Join Date
- Nov 2011
- Location
- Turkey
- Posts
- 378
- Blog Entries
- 24
- Rep Power
- 2
Similar Threads
-
moving from one frame to another
By aditi08 in forum AWT / SwingReplies: 1Last Post: 12-01-2011, 06:56 PM -
moving a square in a specified direction
By totj in forum New To JavaReplies: 15Last Post: 03-15-2011, 11:26 PM -
Can't get graphics to move opposite direction
By Nimblyjacks in forum Java AppletsReplies: 8Last Post: 12-22-2009, 01:46 AM -
how can i move one frame window to another window
By santhosh_el in forum AWT / SwingReplies: 8Last Post: 06-10-2009, 03:36 PM -
how can i move from Frame to another
By the swan in forum AWT / SwingReplies: 2Last Post: 04-04-2009, 04:24 PM


2Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks