create a JLabel
object as in pages 25, 26 of the GUI notes
add the JLabel
objects to the JPanel
add the JPanel
to the ContentPane
try it out
Use the code from
Section 11.7.2 to create a
class. This version controls the speed of the ball using the
up-arrow and down-arrow keys. The notes describe how keystrokes are
dealt with on pages 27 and 28, but not in detail. Since this one
just use keystrokes, no Swing components are needed, so the ContentPane
object is not strictly necessary; canvas
can be used instead.
To recognize keystrokes, the class implements KeyListener.
This interface defines three methods, but we only need one, keyPressed.
Because the interface requires that all three methods must be
provided, two have empty bodies. This is an acceptable and
often-used Java idiom.
Notice that you can do strange things to falling balls, stopping them or
making them fall "upwards." It can be fun to play with the ≈
physics of alternate universes, but in our universe things fall without
stopping. Modify the program to keep the speed from going beyond
FAST_SPEED when pressing up-arrow, or lower than SLOW_SPEED when
pressing down-arrow. You'll want to make sure you have those
constants defined in your class.