Mike Lolo
Mike Lolo

Reputation: 21

error MouseMotionListener, MouseListener

I am getting an error :

AniWorld is not abstract and does not override abstract method mouseMoved(MouseEvent) in MouseMotionListener
public class AniWorld extends  Applet implements MouseMotionListener, MouseListener

The code is:

 import java.awt.*;
import java.lang.*;
import java.applet.*;
import java.net.*;
import java.net.MalformedURLException;
import java.applet.Applet.*;
import java.io.IOException;
import java.awt.event.*;

public class AniWorld extends  Applet implements MouseMotionListener, MouseListener
{
public Stage      stage;
GridBagLayout     layout;
public void init()
{
    addMouseMotionListener(this);
    addMouseListener(this);
    setBackground( Color.white );
    layout = new GridBagLayout();
    setLayout( layout );
    int width = Integer.parseInt( getParameter( "width" ).trim() );
    int height = Integer.parseInt( getParameter( "height" ).trim() );
    stage = new Stage( width-10,height-10);
    constrain( stage,this,layout,1,0,1,6,
               GridBagConstraints.NONE,
               GridBagConstraints.CENTER,0,0 );

    stage.addBall();
    stage.addRect();
    stage.addArc();
    stage.start();
}
public void start()
{
    stage.start();
} // end of start()



public static void constrain(
                 Component     component,
                 Container     container,
                 GridBagLayout layout,
                 int           gridx,
                 int           gridy,
                 int           gridwidth,
                 int           gridheight,
                 int           fill,
                 int           anchor,
                 double        weightx,
                 double        weighty
                 ) {

    GridBagConstraints gbc = new GridBagConstraints();

    gbc.gridx      = gridx;
    gbc.gridy      = gridy;
    gbc.gridwidth  = gridwidth;
    gbc.gridheight = gridheight;
    gbc.fill       = fill;
    gbc.anchor     = anchor;
    gbc.weightx    = weightx;
    gbc.weighty    = weighty;

    layout.setConstraints( component,gbc );

    container.add( component );

    } // end of constrain()

public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
}

Upvotes: 0

Views: 3780

Answers (4)

Ankit Velani
Ankit Velani

Reputation: 1

Yes AniWorld , you have to override all the methods of Interface ,means if you are implementing MouseListener then you must override the all five method and if you are leaving one method to override then 100% you will get same error ...you are implementing both listeners , so you have to override , mouseCliked() , mouseEntered() , mouseExited() ,mousePressed() ,mouseReleased(),mouseDragged() ,mouseMoved()

after all method overriding you will successful without any error ....

Upvotes: 0

Firenze
Firenze

Reputation: 308

You have to implement (or at least leave a blank implementation for) the methods MouseDragged(MouseEvent e) and MouseMoved(MouseEvent e) because you're implementing the MouseMotionListener interface.

Here the documentation for MouseMotionListener

Upvotes: 1

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

Another reason not to have your GUI class implement listener interfaces. The best solution is not to do this, but instead to use an anonymous inner class that extends MouseAdapter or a private inner class that does the same. Otherwise you're asking your GUI to do too much and are setting yourself up for possible debugging problems in the future.

Upvotes: 2

Eric Rosenberg
Eric Rosenberg

Reputation: 1533

You need to add

public void mouseMoved(MouseEvent) 

To AniWorld, or remove 'MouseMotionListener' from the list of implemented interfaces.

The reason you are getting that error is because when you say you implement an interface you have to provide implementations for all the methods in the interface, or your class needs to be abstract.

Upvotes: 1

Related Questions