Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

implement the MouseMotionListener interface and use the mouseDragged method’s Mo

ID: 3693065 • Letter: I

Question

       implement the MouseMotionListener interface and use the mouseDragged method’s MouseEvent object to get the position.
       implement the MouseEventListener interface and use the getX and getY methods to get the position..
       implement the MouseListener interface and use the MouseEvent object parameter to the mouseClicked method to get the position.
       implement the MouseEvent interface and use the getX and getY methods to get the position.

       throws block.
       catch block.
       try block.
       finally block.

Question 5.5. (TCO 5) In order to get an event associated with the mouse being dragged with a button depressed and determine the position of the mouse cursor, a Java program must (Points : 6)

Explanation / Answer

5.5)

import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
/*
<applet code="Mouse" width=500 height=500>
</applet>
*/
public class Mouse extends Applet
implements MouseListener,MouseMotionListener
{
   int X=0,Y=20;
   String msg="MouseEvents";
   public void init()
   {
       addMouseListener(this);
       addMouseMotionListener(this);
      
   }
   public void mouseEntered(MouseEvent m)
   {
      
       repaint();
   }
   public void mouseExited(MouseEvent m)
   {
       repaint();
   }
   public void mousePressed(MouseEvent m)
   {

       repaint();
   }
   public void mouseReleased(MouseEvent m)
   {

       repaint();
   }
   public void mouseMoved(MouseEvent m)
   {
       X=m.getX();
       Y=m.getY();
       msg="";
       setBackground(Color.white);
       showStatus("Mouse Moved");
       repaint();
   }
   public void mouseDragged(MouseEvent m)
   {
X=m.getX();
       Y=m.getY();
       msg="X:"+m.getX()+" Y:"+m.getY();
       showStatus("Mouse dragged");
       repaint();
   }
   public void mouseClicked(MouseEvent m)
   {
       repaint();
   }
   public void paint(Graphics g)
   {
       g.drawString(msg,X,Y);
   }
}

-----------------------------------------------------------------------------

exmple program for 7.7 and 9.9

-----------------------------------------------------------------------------


public class ExceptionHan {
public static void main(String args[]){
try{
int data=1/0;
System.out.println(data);
}
catch(NullPointerException e){System.out.println(e);}
finally{System.out.println("finally block is always executed");}
System.out.println("rest of the code...");
}
}