Java Reference
In-Depth Information
LISTING 9.14
continued
page.setColor (Color.black);
page.drawOval (centerX-3, centerY-3, 6, 6);
if
(current !=
null
)
{
page.drawLine (current.x, current.y, centerX, centerY);
page.drawString ("Distance: " + fmt.format(length), 10, 15);
}
}
//*****************************************************************
// Represents the listener for mouse events. Demonstrates the
// ability to extend an adapter class.
//*****************************************************************
private class
OffCenterListener
extends
MouseAdapter
{
//--------------------------------------------------------------
// Computes the distance from the mouse pointer to the center
// point of the applet.
//--------------------------------------------------------------
public void
mouseClicked (MouseEvent event)
{
current = event.getPoint();
length = Math.sqrt(Math.pow((current.x-centerX), 2) +
Math.pow((current.y-centerY), 2));
repaint();
}
}
}
SELF-REVIEW QUESTIONS
(see answers in Appendix N)
SR 9.25
What is an adapter class?
SR 9.26
Is an
OffCenterPanel
object a
JPanel
? Is it a
JComponent
? Is it a
Container
? Is it an
Object
? Explain.
SR 9.27
In the private listener class defined for the
RubberLinesPanel
class in
Chapter 8, five empty mouse event methods were included. Why are
there no such methods included for the private
OffCenterListener
class defined in this section?
Search WWH ::
Custom Search