Here is my Code which output is as shown in Picture below.
I need to get values of x_coor and y_coor outside the mousePressed() method. but i am not able to do it. i have tried so far
Declaring variable in
Constructor.Declaring variable as global.
Declaring variable as static.
Declaring variable in
main().
but all not got so far i want.
Note: Do not Mention the problem I already know it. I need solution
public class Tri_Angle extends MouseAdapter {
Tri_Angle(){
// int x_coor=0;
// int y_coor=0;
}
public static void main(String[] args) {
JFrame frame = new JFrame ();
final int FRAME_WIDTH = 500;
final int FRAME_HEIGHT = 500;
frame.setSize (FRAME_WIDTH, FRAME_HEIGHT);
frame.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent me) {
int x_coor= me.getX();
int y_coor= me.getY();
System.out.println("clicked at (" + x_coor + ", " + y_coor + ")");
}
});
frame.setTitle("A Test Frame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
//This is what i want to do, but it does not know x_coor variable here.
if(x_coor>=0)
{
System.out.println("clicked at (" + x_coor + ", " + y_coor + ")");
}
}
}
