-
December 4th, 2008, 04:44 PM
#1
get mouse x and y coords from method new object listener call
what's non trivila here is ,IMO that I would need to get the coordinates out of the following construction:
Code:
frame.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {e.getX(); e.getY();}
});
the compilers allow only the following
Code:
final int a;
final int b;
frame.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {a=e.getX(); b=e.getY();}
});
but then I cannot reference to them later as they are being errored out by the compiler as not initialized. If I try to assign values in the declaration, the compiler doesn't allow that either...
-
December 4th, 2008, 06:03 PM
#2
Re: get mouse x and y coords from method new object listener call
Can you explain what you trying to achieve and where you need to use the x, y co-ordinates?
-
December 4th, 2008, 08:27 PM
#3
Re: get mouse x and y coords from method new object listener call
If you declare a and b 'final' you can't assign values to them unless you do it in the declaration or in a constructor.
To answer your question, we need to know the answer to Keang's question.
All truths are easy to understand once they are discovered; the point is to discover them...
G. Galilie
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
-
December 5th, 2008, 01:43 AM
#4
Re: get mouse x and y coords from method new object listener call
Originally Posted by EVS
what's non trivila here is ,IMO that I would need to get the coordinates out of the following construction:
Code:
frame.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {e.getX(); e.getY();}
});
the compilers allow only the following
Code:
final int a;
final int b;
frame.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {a=e.getX(); b=e.getY();}
});
but then I cannot reference to them later as they are being errored out by the compiler as not initialized. If I try to assign values in the declaration, the compiler doesn't allow that either...
Declaration of listener is in separate place from its implementation. You are trying to put both together.
It goes like this:
Code:
//somewhere in constructor
jPanelA.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jPanelAMouseClicked(evt);
}
});
//somewhere in class
private void jPanelAMouseClicked(java.awt.event.MouseEvent evt) {
int x= evt.getX();
int y= evt.getY();
}
If you decide to use NetBeans IDE, it will do this automatically for you.
Last edited by postmortem; December 5th, 2008 at 01:49 AM.
-
December 5th, 2008, 05:04 AM
#5
Re: get mouse x and y coords from method new object listener call
Originally Posted by postmortem
//somewhere in constructor
jPanelA.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jPanelAMouseClicked(evt);
}
});
//somewhere in class
private void jPanelAMouseClicked(java.awt.event.MouseEvent evt) {
int x= evt.getX();
int y= evt.getY();
}
Err how does this help? The OP already has access to the x,y co-ordinates within the listener object's called method, extracting the code to another method makes no difference - it's just a different style.
-
December 5th, 2008, 08:39 AM
#6
Re: get mouse x and y coords from method new object listener call
Originally Posted by dlorde
If you declare a and b 'final' you can't assign values to them unless you do it in the declaration or in a constructor.
Or in a static block. It looks like he might be trying to do the following, but in a method.
final int x;
final int y;
static
{
x = //mouse w/e
y = //mouse w/e
}
Last edited by ProgramThis; December 5th, 2008 at 08:44 AM.
-
December 5th, 2008, 10:41 AM
#7
Re: get mouse x and y coords from method new object listener call
Originally Posted by ProgramThis
Or in a static block.
Yes - although he'd have to declare them static:
Code:
static final int x;
static final int y;
static
{
x = //mouse w/e
y = //mouse w/e
}
And, of course, it can't be done in a method.
If you lie to the compiler, it will get its revenge...
Henry Spencer
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|