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...
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?
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
Re: get mouse x and y coords from method new object listener call
Quote:
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.
Re: get mouse x and y coords from method new object listener call
Quote:
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.
Re: get mouse x and y coords from method new object listener call
Quote:
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
}
Re: get mouse x and y coords from method new object listener call
Quote:
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