Consider an applet that implements MouseListener Assume that

Consider an applet that implements MouseListener. Assume that the applet has five instance data, int x1, x2, y1, y2, and boolean inside. The four int values represent the two end-points of a box (x1, y1 is the upper left hand point and x2, y2 is the lower right hand point). Which of the following properly defines code that will determine whenever the mouse button is clicked if the mouse is currently inside this box or not. If the mouse is inside the box, inside is set to true, otherwise it is set to false. 1. Requires both of the following methods: public void mouseEntered(MouseEvent me) { inside = true; } public void mouseExited(MouseEvent me) { inside = false; } 2. public void mouseMoved(MouseEvent me) { if (me.getX( ) >= x1 && me.getX( ) <= x2 && me.getY( ) >= y1 && me.getY( ) <= y2) inside = true; else inside = false; } 3. public void mousePressed(MouseEvent me) { if (me.getX( ) >= x1 && me.getX( ) <= x2 && me.getY( ) >= y1 && me.getY( ) <= y2) inside = true; else inside = false; } 4. public void mouseReleased(MouseEvent me) { if (me.getX( ) >= x1 && me.getX( ) <= x2 && me.getY( ) >= y1 && me.getY( ) <= y2) inside = true; else inside = false; } 5. public void mouseEntered(MouseEvent me) { if (me.getX( ) >= x1 && me.getX( ) <= x2 && me.getY( ) >= y1 && me.getY( ) <= y2) inside = true; else inside = false; }

Solution

3. public void mousePressed(MouseEvent me)
{
   if (me.getX( ) >= x1 && me.getX( ) <= x2 && me.getY( ) >= y1 && me.getY( ) <= y2)
       inside = true;
   else
       inside = false;
}
As per the question: whenever the mouse button is clicked
So method must be: mousePressed

As per the question: Mouse is currently inside this box or not
me.getX() will give the current X position
me.getY( ) will give the current Y position
As per the code: if (me.getX( ) >= x1 && me.getX( ) <= x2 && me.getY( ) >= y1 && me.getY( ) <= y2)
whill check the mouse X coordinate with specified x1 and x2 and mouse Y coordinate with specified y1 and y2
if it is true
   the boolean variable inside set to true
otherwise
   the boolean variable inside set to false

Consider an applet that implements MouseListener. Assume that the applet has five instance data, int x1, x2, y1, y2, and boolean inside. The four int values rep

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site