Results 1 to 15 of 15
  1. #1
    chielt is offline Member
    Join Date
    Nov 2009
    Posts
    26
    Rep Power
    0

    Default getX() and getY() return same value

    I am writing an othello/reversi program for my java course at university.

    I am now writing the part where clicking somewhere on the board, puts a 1 or a 2 (depending on which player's turn it is) in a string array. the method paint then loops through the array list to put a white or black oval on the matching point in the grid.

    I was having trouble to get it working so I modified my mouseClicked method for debugging, and found out getY() and getX() return the same value, regardless of the position. They always return 24 for X an 5 for Y.

    Java Code:
    	
    public void mouseClicked(MouseEvent e) 
    	{
    		int iXCor = c.getX();
    		int iYCor = c.getY();
    		System.out.println(""+iXCor+", "+iYCor);
    	}
    c is a Canvas, displaying the grid and the checkers.

    Does anyone have any idea how I can get getX() and getY() return the value of the mouse position?

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    Does anyone have any idea how I can get getX() and getY() return the value of the mouse position?
    Look at the MouseEvent for the appropriate method.

  3. #3
    chielt is offline Member
    Join Date
    Nov 2009
    Posts
    26
    Rep Power
    0

    Default

    Quote Originally Posted by camickr View Post
    Look at the MouseEvent for the appropriate method.
    int getX()
    Returns the horizontal x position of the event relative to the source component.
    int getY()
    Returns the vertical y position of the event relative to the source component.
    That is what the API says. That's why I don't get it and decided to open a thread here :)

  4. #4
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    So why are you using the Canvas to get the X and Y values?

  5. #5
    chielt is offline Member
    Join Date
    Nov 2009
    Posts
    26
    Rep Power
    0

    Default

    Quote Originally Posted by camickr View Post
    So why are you using the Canvas to get the X and Y values?
    Because the grid is in the canvas. The X and Y positions need to be rellative to the Canvas. I think using this.getX() and this.getY() would be pretty pointless.

  6. #6
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    You said you read the MouseEvent API, so why are you using the Canvas? The methods are defined in the MouseEvent!

  7. #7
    chielt is offline Member
    Join Date
    Nov 2009
    Posts
    26
    Rep Power
    0

    Default

    Quote Originally Posted by camickr View Post
    You said you read the MouseEvent API, so why are you using the Canvas? The methods are defined in the MouseEvent!
    I have 2 classes. On building the GUI and the other just the playing field. The GUI is an extention of Applet and the one extending is an extention of Canvas.

    The mouseListener is implemented in the GUI class. How do I get getX() to return the value of the mouse X position relative to the canvas boundaries?

  8. #8
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    How do I get getX() to return the value of the mouse X position relative to the canvas boundaries?
    You said you read the MouseEvent.getX() API method description. Please post the description from the API here in this thread.

  9. #9
    chielt is offline Member
    Join Date
    Nov 2009
    Posts
    26
    Rep Power
    0

    Default

    I quoted it from the API. It's in post #3. That's quoted from the MouseEvent API.

    getX

    public int getX()

    Returns the horizontal x position of the event relative to the source component.

    Returns:
    x an integer indicating horizontal position relative to the component

    getY

    public int getY()

    Returns the vertical y position of the event relative to the source component.

    Returns:
    y an integer indicating vertical position relative to the component


  10. #10
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    So why are you using Component.getX() instead of MouseEvent.getX()?

    If you need more help post your SSCCE as I obviously don't understand your problem.

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

    Default

    Have you tried camickr's suggestion, to use e.getX() and e.getY() in your code in the first post, not c.getX() and not c.getY()???

  12. #12
    chielt is offline Member
    Join Date
    Nov 2009
    Posts
    26
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Have you tried camickr's suggestion, to use e.getX() and e.getY() in your code in the first post, not c.getX() and not c.getY()???
    So great how putting the answer in a different sentence makes the difference. I was having a hard time getting Camrick's suggestion. It works, so I can move on. Thanks a lot guys.

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

    Default

    camickr was trying to gently lead you towards the solution, thereby giving the the benefit of having an aha! moment, and the possibility of a deeper understanding of your problem. I on the other hand unceremoniously shoved you into the solution pit where you found your solution, but at a greater risk of not finding the understanding too. I much prefer camickr's approach when I have the time and patience.

  14. #14
    chielt is offline Member
    Join Date
    Nov 2009
    Posts
    26
    Rep Power
    0

    Default

    I kinda got that. I am glad he did. As the mouseListener is added to the canvas, X and Y will be relative to canvas. The getX() and getY() are getting back values from the MouseEvent, being the object I need. The canvas has a different X and Y which I have no idea what they are :)

    thanks again

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

    Default

    Quote Originally Posted by chielt View Post
    I kinda got that. I am glad he did. As the mouseListener is added to the canvas, X and Y will be relative to canvas. The getX() and getY() are getting back values from the MouseEvent, being the object I need. The canvas has a different X and Y which I have no idea what they are :)

    thanks again
    read the API: the Canvas's x and y are the location of the canvas's origin (upper left corner usually) relative to its holding Container.

Similar Threads

  1. java return help
    By mukul9999 in forum New To Java
    Replies: 6
    Last Post: 02-15-2009, 07:23 AM
  2. Using int/int, 7/5 would return 1
    By zoe in forum New To Java
    Replies: 2
    Last Post: 12-02-2008, 12:25 PM
  3. about 'return'.
    By helloworld in forum New To Java
    Replies: 9
    Last Post: 11-28-2008, 05:08 AM
  4. if..else..return
    By mqdias in forum New To Java
    Replies: 1
    Last Post: 08-10-2007, 05:20 PM
  5. Return to try block
    By Freddie in forum New To Java
    Replies: 2
    Last Post: 05-11-2007, 09:58 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
  •