Results 1 to 16 of 16
Like Tree2Likes
  • 2 Post By fatabass

Thread: Why is my frame moving upwards even though I do not move it in upper direction?

  1. #1
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Why is my frame moving upwards even though I do not move it in upper direction?

    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);
    			
    			
    		}
    Sample output:
    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 )

  2. #2
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default 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 ?

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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?

  4. #4
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default 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.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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.

  6. #6
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default 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 constructor
    Java 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

  7. #7
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default 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

  8. #8
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default 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.

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Why is my frame moving upwards even though I do not move it in upper direction?

    Quote Originally Posted by fatabass View Post
    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.
    OK. Again, if you get stuck you will likely need an sscce.

  11. #11
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default 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 ?

  12. #12
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default 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.

  13. #13
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Re: Why is my frame moving upwards even though I do not move it in upper direction?

    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));
    If anyone is interested and the solution is not clear, I can try to explain.

    It works fine now.
    DarrylBurke and Fubarable like this.

  14. #14
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default Re: Why is my frame moving upwards even though I do not move it in upper direction?

    I like the descriptive variable names.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  15. #15
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Why is my frame moving upwards even though I do not move it in upper direction?

    Quote Originally Posted by fatabass View Post
    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));
    If anyone is interested and the solution is not clear, I can try to explain.

    It works fine now.
    It's clear to me. That was my suggestion in post #3 of this thread.

  16. #16
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Re: Why is my frame moving upwards even though I do not move it in upper direction?

    A question:

    Can I make my frame visible while I am moving it around ?

    What should I work on for this ?

Similar Threads

  1. moving from one frame to another
    By aditi08 in forum AWT / Swing
    Replies: 1
    Last Post: 12-01-2011, 06:56 PM
  2. moving a square in a specified direction
    By totj in forum New To Java
    Replies: 15
    Last Post: 03-15-2011, 11:26 PM
  3. Can't get graphics to move opposite direction
    By Nimblyjacks in forum Java Applets
    Replies: 8
    Last Post: 12-22-2009, 01:46 AM
  4. how can i move one frame window to another window
    By santhosh_el in forum AWT / Swing
    Replies: 8
    Last Post: 06-10-2009, 03:36 PM
  5. how can i move from Frame to another
    By the swan in forum AWT / Swing
    Replies: 2
    Last Post: 04-04-2009, 04:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •