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.
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...");
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.