0

I have written this applet abut mouse events. In cmd it gets compiled without any errors but when I run it using the appletviewer command, nothing happens. I tried different JDKs, but the problem persists.

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

/*<applet code="rat" width=150 height=150>
</applet>
*/

public class rat extends Applet implements MouseListener,MouseMotionListener
{
    String msg="";
    int mouseX=0,mouseY=0;

    public void init()
    {
       addMouseListener(this);  
       addMouseMotionListener(this);
    }

    public void mouseClicked(MouseEvent e)
    {
       mouseX=0;
       mouseY=10;
       msg="Mouse Clicked";
       repaint();
    }

    public void mouseEntered(MouseEvent e) 
    {
       mouseX=0;
       mouseY=10;
       msg="Mouse entered";
       repaint();
    }

    public void mouseExited(MouseEvent e) 
    {
       mouseX=0;
       mouseY=10;
       msg="Mouse exited";
       repaint();
    }

    public void mousePressed(MouseEvent e)
    {
       mouseX=e.getX();``
       mouseY=e.getY();`
       msg="Down";
       repaint();
    }

    public void mouseReleased(MouseEvent e) 
    {
       mouseX=e.getX();
       mouseY=e.getY();
       msg="up";
       repaint();
    }

    public void mouseDragged(MouseEvent e)
    {
       mouseX=e.getX();
       mouseY=e.getY();
       msg="*"; 
       showStatus("mouse dragged at"+mouseX+","+mouseY);
       repaint();
    }

    public void mouseMoved(MouseEvent e)
    {
       showStatus("mouse moved at"+mouseX+","+mouseY);
    }

    public void paint(Graphics g)
    {
       g.drawString(msg,mouseX,mouseY);
    }
}
JimiLoe
  • 950
  • 2
  • 14
  • 22
  • 1) Why code an applet? If it is due to spec by your instructor, 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) Why use AWT components rather than Swing? See [this answer](http://stackoverflow.com/a/6255978/418556) for many good reasons to abandon AWT. – Andrew Thompson Sep 02 '14 at 16:09
  • Use a logical & consistent code formatting style! The code indentation is intended to help people follow the program flow. – Andrew Thompson Sep 02 '14 at 16:10

0 Answers0