Declaring
x_pos and
y_pos as local variables in
paint hid the member variables of the same name. Solution: eliminate the declarations in
paint.
In java:
public void paint(Graphics g) {
// Change these - declaration/instantiations of local variables
int x_pos=e.getX(); // These hide the member variables
int y_pos=e.getY(); // declared above in class scope.
// to these - instantiations of member variables
x_pos=e.getX();
y_pos=e.getY();
// <applet code="CatcherRx" width="300" height="300"></applet>
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class CatcherRx extends Applet implements MouseMotionListener{
int x_pos;
int y_pos;
public void init(){
addMouseMotionListener(this);
}
public void mouseMoved(MouseEvent e){
x_pos=e.getX();
y_pos=e.getY();
repaint();
}
public void paint(Graphics g){
g.setColor(Color.blue);
g.fillRect(x_pos-25,y_pos-5,50,10);
}
public void mouseDragged(MouseEvent e) {}
}