I was trying to learn applets and on executing this code, there are no compile-time errors but the problem is that i m not getting any response to my code. i used simple notepad and appletviewer to get it done. On clicking on the applet, the coordinates should be displayed but it’s not happening.I have tried entering the relevant html code and executing via a browser but the response is the same. Any help will be really appreciated
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code="MyMouseApplet.class"
width = "400"
height = "400">
</applet>*/
public class MyMouseApplet extends Applet implements MouseListener{
int x,y;
String str=" ";
public void init(){
this.addMouseListener(this);
}
public void paint(Graphics g){
g.drawString(str,x,y);
}
public void update(Graphics g){
paint(g);
}
public void mouseClicked(MouseEvent m)
{
int x = m.getX();
int y = m.getY();
str="x:" + x + "y:" + y;
repaint();
}
public void mouseExited(MouseEvent m){}
public void mouseEntered(MouseEvent m){}
public void mousePressed(MouseEvent m){}
public void mouseReleased(MouseEvent m){}
}
Your
mouseClickedfunction takesxandyout of scope. Do this:This will access the fields of your class, not local variables.