Results 1 to 15 of 15
- 12-21-2009, 12:10 AM #1
Member
- Join Date
- Nov 2009
- Posts
- 26
- Rep Power
- 0
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.
c is a Canvas, displaying the grid and the checkers.Java Code:public void mouseClicked(MouseEvent e) { int iXCor = c.getX(); int iYCor = c.getY(); System.out.println(""+iXCor+", "+iYCor); }
Does anyone have any idea how I can get getX() and getY() return the value of the mouse position?
- 12-21-2009, 12:26 AM #2
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
Look at the MouseEvent for the appropriate method.Does anyone have any idea how I can get getX() and getY() return the value of the mouse position?
- 12-21-2009, 12:29 AM #3
Member
- Join Date
- Nov 2009
- Posts
- 26
- Rep Power
- 0
That is what the API says. That's why I don't get it and decided to open a thread here :)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.
- 12-21-2009, 12:31 AM #4
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
So why are you using the Canvas to get the X and Y values?
- 12-21-2009, 12:40 AM #5
Member
- Join Date
- Nov 2009
- Posts
- 26
- Rep Power
- 0
- 12-21-2009, 12:43 AM #6
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
You said you read the MouseEvent API, so why are you using the Canvas? The methods are defined in the MouseEvent!
- 12-21-2009, 12:52 AM #7
Member
- Join Date
- Nov 2009
- Posts
- 26
- Rep Power
- 0
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?
- 12-21-2009, 01:02 AM #8
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
You said you read the MouseEvent.getX() API method description. Please post the description from the API here in this thread.How do I get getX() to return the value of the mouse X position relative to the canvas boundaries?
- 12-21-2009, 01:06 AM #9
Member
- Join Date
- Nov 2009
- Posts
- 26
- Rep Power
- 0
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
- 12-21-2009, 01:14 AM #10
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
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.
-
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-21-2009, 06:42 AM #12
Member
- Join Date
- Nov 2009
- Posts
- 26
- Rep Power
- 0
-
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.
- 12-21-2009, 06:49 AM #14
Member
- Join Date
- Nov 2009
- Posts
- 26
- Rep Power
- 0
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
-
Similar Threads
-
java return help
By mukul9999 in forum New To JavaReplies: 6Last Post: 02-15-2009, 06:23 AM -
Using int/int, 7/5 would return 1
By zoe in forum New To JavaReplies: 2Last Post: 12-02-2008, 11:25 AM -
about 'return'.
By helloworld in forum New To JavaReplies: 9Last Post: 11-28-2008, 04:08 AM -
if..else..return
By mqdias in forum New To JavaReplies: 1Last Post: 08-10-2007, 04:20 PM -
Return to try block
By Freddie in forum New To JavaReplies: 2Last Post: 05-11-2007, 08:58 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks