0

I was writing a program in Eclipse regarding mouse Events. The whole code is :

import java.awt.*;    
import java.awt.event.*;    
import java.applet.*;

public class Mouse extends Applet implements MouseListener,MouseMotionListener {

    int X=0,Y=20;       
    String msg="MouseEvents";

    public void init() {
        addMouseListener(this);
        addMouseMotionListener(this);
        setBackground(Color.black);
        setForeground(Color.red);
    }

    public void mouseEntered(MouseEvent m) {
        setBackground(Color.magenta);
        showStatus("Mouse Entered");
        repaint();
    }

    public void mouseExited(MouseEvent m) {
        setBackground(Color.black);
        showStatus("Mouse Exited");
        repaint();
    }

    public void mousePressed(MouseEvent m) {
        X=10;
        Y=20;
        msg="NEC";
        setBackground(Color.green);
        repaint();
    }

    public void mouseReleased(MouseEvent m) {
        X=10;
        Y=20;
        msg="Engineering";
        setBackground(Color.blue);
        repaint();
    }

    public void mouseMoved(MouseEvent m) {
        X=m.getX();
        Y=m.getY();
        msg="College";
        setBackground(Color.white);
        showStatus("Mouse Moved");
        repaint();
    }

    public void mouseDragged(MouseEvent m) {
        msg="CSE";
        setBackground(Color.yellow);
        showStatus("Mouse Moved"+m.getX()+" "+m.getY());
        repaint();
    }

    public void mouseClicked(MouseEvent m) {
        msg="Students";
        setBackground(Color.pink);
        showStatus("Mouse Clicked");
        repaint();
    }

    public void paint(Graphics g) {
        g.drawString(msg,X,Y);
    }
}

In eclipse, I was getting 1 error in the 4th line that is the public class Mouse extends Applet implements MouseListener, MouseMotionListener. It said two fixes were available:

  1. Add unimplemented methods.

  2. Make mouse type abstract

Even after doing both i was getting exceptions. I cant understand what's wrong.

Sergei Bubenshchikov
  • 5,275
  • 3
  • 33
  • 60
  • 1) Why code an applet? If it is due to the teacher specifying it, please refer them to [Why CS teachers should **stop** teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) See [Java Plugin support deprecated](http://www.gizmodo.com.au/2016/01/rest-in-hell-java-plug-in/) and [Moving to a Plugin-Free Web](https://blogs.oracle.com/java-platform-group/entry/moving_to_a_plugin_free). 3) Why use AWT? See [this answer](http://stackoverflow.com/questions/6255106/java-gui-listeners-without-awt/6255978#6255978) .. – Andrew Thompson Dec 18 '16 at 21:20
  • .. for many good reasons to abandon AWT components in favor of Swing. 4) *"Even after doing both i was getting exceptions."* Always copy/paste error and exception output! 5) Compile & run-time exceptions are entirely separate. Just because something compiles, does not mean it will run without error. 6) When overriding a paint method, always call the super method to clear previously drawn pixels. – Andrew Thompson Dec 18 '16 at 21:20

0 Answers0