15.15. Programming Exercises

 
[Page 487 ( continued )]

Programming Exercises

Sections 14.2 “14.3

14.1 ( Finding which button has been clicked on the console ) Add the code to Exercise 12.1 that will display a message on the console indicating which button has been clicked.
14.2 ( Using ComponentEvent ) Any GUI component can fire a ComponentEvent . The ComponentListener defines the componentMoved , componentResized , componentShown , and componentHidden methods for processing component events. Write a test program to demonstrate ComponentEvent .
[Page 488]
14.3* ( Finding which button has been clicked on a panel ) Write a program that creates a user interface with two buttons named OK and Cancel and a message panel for displaying a message. When you click the OK button, a message "The OK button has been clicked" is displayed. When you click the Cancel button, a message "The Cancel button has been clicked" is displayed, as shown in Figure 14.19(a).
Figure 14.19. (a) Exercise 14.3 displays which button is clicked on a message panel. (b) Exercise 14.4 displays the mouse position. (c) Exercise 14.9 uses the arrow keys to draw the lines.

Section 14.4 Mouse Events

14.4* ( Displaying the mouse position ) Write two programs, one displays the mouse position when the mouse is clicked (see Figure 14.19(b)), and the other displays the mouse position when the mouse is pressed and the display disappears when the mouse is released.
14.5* ( Setting background color using a mouse ) Write a program that displays the background color of a panel as black when the mouse is pressed and as white when the mouse is released.
14.6** ( Alternating two messages ) Write a program to rotate two messages "Java is fun" and "Java is powerful" displayed on a panel with a mouse click.

Section 14.5 Key Events

14.7** ( Entering and displaying a string ) Write a program that receives a string from the keyboard and displays it on a panel. The Enter key signals the end of a string. Whenever a new string is entered, it is displayed on the panel.
14.8* ( Displaying a character ) Write a program to get a character input from the keyboard and display the character where the mouse points.
14.9* ( Drawing lines using the arrow keys ) Write a program that draws line segments using the arrow keys. The line starts from the center of the frame and draws toward east, north, west, or south when the right-arrow key, up-arrow key, left-arrow key, or down-arrow key is clicked, as shown in Figure 14.19(c).

Section 14.6 (Optional) Animation Using the Timer Class

14.10* ( Displaying a flashing label ) Write a program that displays a flashing label.

Hint

To make the label flash, you need to repaint the panel alternately with the label and without the label (blank screen) at a fixed rate. Use a boolean variable to control the alternation .


14.11* ( Controlling a moving label ) Modify Listing 14.9, AnimationDemo.java, to control a moving label using the mouse. The label freezes when the mouse is pressed, and moves again when the button is released.

[Page 489]
14.12** ( Displaying a running fan ) Listing 13.6, DrawArcs.java, displays a motionless fan. Write a program that displays a running fan.
14.13** ( Slidesshow ) Twenty-five slides are stored as image files (slide0.jpg, slide1.jpg, slide24.jpg) in the image directory downloadable along with the source code in the book. The size of each image is pixels. Write a Java application that automatically displays the slides repeatedly. Each slide is shown for a second. The slides are displayed in order. When the last slide finishes, the first slide is redisplayed, and so on. (Hint: place a label in the frame and set a slide as an image icon in the label.)
14.14** ( Raising flag ) Write a Java program that animates raising a flag, as shown in Figure 14.20. (See §13.13, "Displaying Images," on how to display images.)
Figure 14.20. A flag is rising upward.

14.15** ( Racing car ) Write a Java program that simulates car racing, as shown in Figure 14.21(a). The car moves from left to right. When it hits the right end, it restarts from the left and continues the same process. You can use a timer to control the animation. Redraw the car with new base coordinates (x, y), as shown in Figure 14.21(b).
Figure 14.21. (a) Exercise 14.15 displays a moving car. (b) You can redraw a car with a new base point.

 


[Page 490]
 


Introduction to Java Programming-Comprehensive Version
Introduction to Java Programming-Comprehensive Version (6th Edition)
ISBN: B000ONFLUM
EAN: N/A
Year: 2004
Pages: 503

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net