This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

please help me with actionlistener

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
following is the java source file code that compiles allright
========================================================
import java.awt.event.*;
import java.applet.Applet;
import java.awt.*;

public class movemouse extends Applet implements MouseMotionListener
{
int a,b,c,d;
public void mouseMoved(MouseEvent evt)
{
a=evt.getX();
b=evt.getY();
repaint();
}

public void mouseDragged(MouseEvent evt)
{
c=evt.getX();
d=evt.getY();

repaint();
}




public void paint(Graphics g)
{
g.setColor(Color.red);
g.drawLine(100,100,a,b);
g.setColor(Color.blue);
g.drawLine(c,d,c,d);
}

public void update(Graphics g)
{
paint(g);
}

}
=================================================================
but when i run this applet in appletviewer and on appletviewer
when i move mouse i should see red lines on applet when mouse move and blue when mouse dargged.but when i run this applet,i move mouse ,i can not see any lines on applet.i have done this type of applet with old methods like
public boolean mouseMove (Event e,int x,int y) {}
but this methods are deprecated so i have to use above code part methods .how can i do this please help me.more tips about events and listeners will be appriciated.
thank you very much.


 
alberto sierra
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure but i think you just have defined methods for handling the events, that is you have defined what shoud happen and also when, but you forgot to tell on which component this shoud happen. That is, you should register a listener for the applet itself in the init() method. So that when the mouse moves over the applet, your method should then envoked.

Try:

public void init() {
addMouseMotionListener(this);
}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic