Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Confused about JPanel.getY()

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Confused about JPanel.getY()

    I am a complete newcomer to Java, so apologies for this basic question. I have a JPanel sitting inside of a JFrame. I call the panel's getY() method, and expect the result to give me the y-coordinate of the panel's top edge in the frame's coordinate system. But it isn't even close. The below example demonstrates what I'm encountering. First it prints (after "XXX") the panel's y-coordinate according to getY() (and its border thickness, for good measure). Then, whenever the mouse moves, it prints the (x,y) coords of the mouse pointer in the frame's coordinate system.

    According the output printed after XXX, the panel's y-coordinate is 58 and its border thickness if 4. But moving the mouse around on my screen, I'm still well inside the button panel at y-coordinate 58. The frame's border seems to start around y-coordinate 85 or so.

    I assume this has something to do with the fact that I'm using a layout manager, but perhaps I'm mistaken. In any event, how do I get the correct y-coordinate for the top of the panel in question?

    Thanks in advance for help!

    import java.awt.BorderLayout;
    import java.awt.Container;
    import java.awt.Component;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseMotionListener;
     
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JButton;
    import javax.swing.BoxLayout;
    import javax.swing.border.Border;
    import javax.swing.BorderFactory;
     
    public class Example extends JFrame implements MouseMotionListener {
     
        public Example() {
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
    	// Get Pane and set layout
    	Container pane = getContentPane();
    	pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS));
     
    	// Add Buttons
    	JPanel buttonPanel = new JPanel();
    	buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.Y_AXIS));
    	pane.add(buttonPanel);
     
    	JButton button1 = new JButton("Button1");
    	button1.setAlignmentX(Component.CENTER_ALIGNMENT);
            buttonPanel.add(button1);
     
    	JButton button2 = new JButton("Button2");
    	button2.setAlignmentX(Component.CENTER_ALIGNMENT);
            buttonPanel.add(button2);
     
    	// Set up the drawing area
    	JPanel drawingPanel = new JPanel();
    	drawingPanel.setAlignmentY(Component.BOTTOM_ALIGNMENT);
    	Border raisedBorder = BorderFactory.createRaisedBevelBorder();
    	Border loweredBorder = BorderFactory.createLoweredBevelBorder();
    	Border compoundBorder = BorderFactory.createCompoundBorder(raisedBorder, loweredBorder);
    	drawingPanel.setBorder(compoundBorder);
    	drawingPanel.setPreferredSize(new Dimension(1000,1000));
    	drawingPanel.setMaximumSize(new Dimension(1000,1000));
    	pane.add(drawingPanel);
     
    	// Set up the main window
    	pane.setPreferredSize(new Dimension(1050,1100));
     
    	addMouseMotionListener(this);
            pack();
            setVisible(true);
    	System.out.printf("XXX: %d, %d\n", drawingPanel.getY(),compoundBorder.getBorderInsets(drawingPanel).top);
        }
     
        public void paint(Graphics g) {
            paintComponents(g);
     
        }
     
        public void mouseMoved(MouseEvent me) {
    	int x = me.getX();
    	int y = me.getY();
    	System.out.printf("(%d, %d)\n", x, y);
     
        }
     
        public void mouseDragged(MouseEvent me) {
        }
     
        public static void main(String[] args) {
            JFrame.setDefaultLookAndFeelDecorated(true);
            Example frame = new Example();
        }
    }
    Last edited by jnewcomer; October 31st, 2011 at 09:39 AM. Reason: Highlight code as requested


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,162
    My Mood
    Hungover
    Thanks
    141
    Thanked 595 Times in 510 Posts

    Default Re: Confused about JPanel.getY()

    When posting code, please use highlight tags to preserve formatting.

    Your MouseListener is on the JFrame, so the coordinates that it prints out may or may not include the JFrame border, the top bar with the close/minimize buttons, etc. Your drawingPanel is added to your JPanel pane, so its coordinates do not include those things.

    You might want to check out the SwingUtilities class for converting mouse locations to and from different coordinate systems.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Confused about JPanel.getY()

    Quote Originally Posted by KevinWorkman View Post
    When posting code, please use highlight tags to preserve formatting.

    Your MouseListener is on the JFrame, so the coordinates that it prints out may or may not include the JFrame border, the top bar with the close/minimize buttons, etc. Your drawingPanel is added to your JPanel pane, so its coordinates do not include those things.

    You might want to check out the SwingUtilities class for converting mouse locations to and from different coordinate systems.
    Edited to highlight the code as you requested. Thanks for pointing that out; I didn't know about it.

    Will check into the SwingUtilities as you suggest, but I'm still curious to know the semantics of JPanel.getY(). You're correct that the mouslistener coords ignore the top border of the window (since I can get them to display y=0 just below that top border). But that does't seem to explain it. Assuming an inconsistency where getY() is not ignoring that top border, getY() should return a number that is too large. The problem is that its return value is too small.

    Thanks again.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,162
    My Mood
    Hungover
    Thanks
    141
    Thanked 595 Times in 510 Posts

    Default Re: Confused about JPanel.getY()

    The drawingPanel.getY() is in its parent's coordinate system- the JPanel you're calling pane. However, the MouseListener is on the JFrame, which is pane's parent. I don't really know why you're expecting anything different than what's happening.

    Say I have panel1, which has a y coordinate (in its parent, let's say a JFrame) of zero. That panel1 contains two more JPanels, panelTop and panelBottom. The y-coord of panelTop (in panel1's coordinate system) is 0, and panelBottom's y-coord is 500. Now I add panel2 to panelBottom. What would you expect its y-coord (in its parent's, panelBottom's, coordinate system) to be? If your answer isn't zero, I recommend you put together a sample program that does exactly that.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    Oct 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Confused about JPanel.getY()

    Quote Originally Posted by KevinWorkman View Post
    The drawingPanel.getY() is in its parent's coordinate system- the JPanel you're calling pane. However, the MouseListener is on the JFrame, which is pane's parent. I don't really know why you're expecting anything different than what's happening.

    Say I have panel1, which has a y coordinate (in its parent, let's say a JFrame) of zero. That panel1 contains two more JPanels, panelTop and panelBottom. The y-coord of panelTop (in panel1's coordinate system) is 0, and panelBottom's y-coord is 500. Now I add panel2 to panelBottom. What would you expect its y-coord (in its parent's, panelBottom's, coordinate system) to be? If your answer isn't zero, I recommend you put together a sample program that does exactly that.
    I naively expected (pane.getY() == 0) which would imply that pane and the JFrame have the same y-coordinate system. If pane.getY() == 0, you would share my confusion, right? If so, you presumably expect pane.getY() > 0 (in fact, pane.getY() ~= 30 based on my numbers in my first post), right? I'll check that when I get home from work.

    Thanks for your help!

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,162
    My Mood
    Hungover
    Thanks
    141
    Thanked 595 Times in 510 Posts

    Default Re: Confused about JPanel.getY()

    Quote Originally Posted by jnewcomer View Post
    I naively expected (pane.getY() == 0) which would imply that pane and the JFrame have the same y-coordinate system. If pane.getY() == 0, you would share my confusion, right? If so, you presumably expect pane.getY() > 0 (in fact, pane.getY() ~= 30 based on my numbers in my first post), right? I'll check that when I get home from work.

    Thanks for your help!
    Yeah, exactly. That's what I was trying to point out in my first post, sorry if I wasn't clear. I believe that ~30 is from the JFrame's title bar.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Trouble with getX() and getY()
    By mdegges in forum What's Wrong With My Code?
    Replies: 15
    Last Post: July 13th, 2011, 10:48 AM
  2. I'm confused...
    By acolar in forum Object Oriented Programming
    Replies: 1
    Last Post: April 14th, 2011, 12:14 PM
  3. [SOLVED] mouse getX and getY are off?
    By Brt93yoda in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 6th, 2010, 12:49 AM
  4. How to copy image from one jpanel to another jpanel
    By ramanavarayuri1986 in forum AWT / Java Swing
    Replies: 0
    Last Post: February 15th, 2010, 01:36 AM
  5. Creating and displaying a JPanel inside another JPanel
    By JayDuck in forum AWT / Java Swing
    Replies: 1
    Last Post: April 7th, 2009, 08:02 AM