C:\jexp>javac painttest.java
painttest.java:39: interface expected here
class MouseMotionListener implements MouseMotionListener {
^
painttest.java:14: cannot resolve symbol
symbol : class MouseMotionHandler
location: class PaintTest
addMouseMotionListener(new MouseMotionHandler());
^
2 errors
Error one: Do not name your class with same name as the interface you are implementing. Confuses the compiler.
Error two: Wrong spelling:
addMouseMotionListener(new [i]ClassName[/i]());
must match:
class [i]ClassName[/i] implements MouseMotionListener {
Another typo:
public void MouseMoved(MouseEvent e) {
This prev.x = current.x = e.getX(); can be reduced to prev.x = e.getX();
// <applet code="PaintTest" width="300" height="300"></applet>
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class PaintTest extends Applet {
String str;
Point prev, current;
public void init() {
str = "Initialized";
addMouseMotionListener(new MotionHandler());
addMouseListener(new MouseHandler());
prev = new Point(0, 0);
current = new Point(0, 0);
showStatus("Initializing");
}
public void paint(Graphics g) {
showStatus("Applet Painted");
}
//handler inner classes
class MouseHandler extends MouseAdapter {
public void mousePressed(MouseEvent e) {
prev.x = e.getX();
prev.y = e.getY();
}
}
class MotionHandler extends MouseMotionAdapter {
public void mouseDragged(MouseEvent e) {
str = "Mouse Dragged";
showStatus(str);
current.x = e.getX();
current.y = e.getY();
Graphics g = getGraphics();
g.setColor(Color.blue);
g.drawLine(prev.x, prev.y, current.x, current.y);
prev.x = current.x;
prev.y = current.y;
}
public void mouseMoved(MouseEvent e) {
str = "Mouse Moved";
showStatus(str + " x = " + e.getX() + ", y = " + e.getY());
}
}
}