Java Reference
In-Depth Information
6. Create a public class header with the name MoveIt that extends Applet and
implements the ActionListener.
7. Declare the following variables:
private Image cup;
private Panel keyPad;
public int top = 15;
public int left = 15;
private Button keysArray[];
8. Begin an init method with the following code:
public void init()
{
cup = getImage(getDocumentBase(), “cup.gif”);
Canvas myCanvas = new Canvas();
9. Construct the keypad Panel and the five buttons, labeled: Up, Left, Right,
Down, and Center.
10. Set the background color to blue.
11. Set the layout manager of the Frame to be BorderLayout and then set the
keypad layout manager to be BorderLayout.
12. Add the buttons to the keypad Panel in the appropriate areas.
13. Add myCanvas to the North and keypad to the South of the Frame.
14. Enter five addActionListener statements with the self-referential, this — one
for each of the buttons.
15. Enter the following code to drag the image using the paint method:
public void paint( Graphics g )
{
g.drawImage( cup, left, top, this );
}
16. Create an actionPerformed() method. Hint: Remember to use the
getActionCommand method and assign it to a variable, as you did in the
chapter.
17. Within the actionPerformed() method, write an if statement to test each of
the buttons as follows:
a. If the user clicks the Up button, subtract 15 from the top variable.
b. If the user clicks the Down button, add 15 to the top variable.
c. If the user clicks the Left button, subtract 15 from the left variable.
d. If the user clicks the Right button, add 15 to the left variable.
e. If the user clicks the Center button, set the top variable to 60 and the left
variable to 125.
18. Enter the code repaint(); to instruct the program to repaint the screen.
19. Compile the source code. Fix any errors and recompile, if necessary.
20. Run and then test the application by clicking each number and symbol.
21. Print a copy of the source code for your instructor.
Search WWH ::




Custom Search