Java Reference
In-Depth Information
•Interface
MouseInputListener
(p. 513) of package
javax.swing.event
extends interfaces
MouseListener
and
MouseMotionListener
to create a single interface containing all their methods.
• Each mouse event-handling method receives a
MouseEvent
object (p. 494) that contains infor-
mation about the event, including the
x-
and
y
-coordinates where the event occurred. Coordi-
nates are measured from the upper-left corner of the GUI component on which the event
occurred.
• The methods and constants of class
InputEvent
(p. 514;
MouseEvent
's superclass) enable an ap-
plication to determine which mouse button the user clicked.
• Interface
MouseWheelListener
(p. 515) enables applications to respond to mouse-wheel events.
Section 12.15 Adapter Classes
• An adapter class (p. 518) implements an interface and provides default implementations of its
methods. When you extend an adapter class, you can override just the method(s) you need.
•
MouseEvent
method
getClickCount
(p. 521) returns the number of consecutive mouse-button
clicks. Methods
isMetaDown
(p. 528) and
isAltDown
(p. 521) determine which button was clicked.
Section 12.16
JPanel
Subclass for Drawing with the Mouse
•
JComponent
s method
paintComponent
(p. 522) is called when a lightweight Swing component is
displayed. Override this method to specify how to draw shapes using Java's graphics capabilities.
• When overriding
paintComponent
, call the superclass version as the first statement in the body.
• Subclasses of
JComponent
support transparency. When a component is opaque (p. 522),
paint-
Component
clears its background before the component is displayed.
• The transparency of a Swing lightweight component can be set with method
setOpaque
(p. 522;
a
false
argument indicates that the component is transparent).
• Class
Point
(p. 523) package
java.awt
represents an
x-y
coordinate.
• Class
Graphics
(p. 523) is used to draw.
•
MouseEvent
method
getPoint
(p. 524) obtains the
Point
where a mouse event occurred.
•Method
repaint (
p. 524), inherited indirectly from class
Component
, indicates that a component
should be refreshed on the screen as soon as possible.
•Method
paintComponent
receives a
Graphics
parameter and is called automatically whenever a
lightweight component needs to be displayed on the screen.
•
Graphics
method
fillOval
(p. 524) draws a solid oval. The first two arguments are the upper-left
x
-
y
coordinate of the bounding box, and the last two are the bounding box's width and height.
Section 12.17 Key Event Handling
• Interface
KeyListener
(p. 494) is used to handle key events (p. 494) that are generated when keys
on the keyboard are pressed and released. Method
addKeyListener
of class
Component
(p. 525)
registers a
KeyListener
.
•
KeyEvent
(p. 494) method
getKeyCode
(p. 528) gets the virtual key code (p. 528) of the pressed
key. Class
KeyEvent
contains virtual key-code constants that represent every key on the keyboard.
•
KeyEvent
method
getKeyText
(p. 528) returns a string containing the name of the pressed key.
•
KeyEvent
method
getKeyChar
(p. 528) gets the Unicode value of the character typed.
•
KeyEvent
method
isActionKey
(p. 528) determines whether the key in an event was an action
key (p. 525).
•
InputEvent
method
getModifiers
(p. 528) determines whether any modifier keys (such as
Shift
,
Alt
and
Ctrl
) were pressed when the key event occurred.