View Single Post
  #2 (permalink)  
Old 08-04-2007, 01:34 AM
hardwired hardwired is offline
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
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:
Code:
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();
Code:
// <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) {} }
Reply With Quote