31.3. Table Models and Table Column Models

 
[Page 912 ( continued )]

Programming Exercises

Sections 27.1 “27.6

27.1* ( Enabling MessagePanel to fire ActionEvent ) The MessagePanel class in §13.11, "Case Study: The MessagePanel Class," is a subclass of JPanel ; it can fire a MouseEvent , KeyEvent , and ComponentEvent , but not an ActionEvent . Modify the MessagePanel class so that it can fire an ActionEvent when an instance of the MessagePanel class is clicked. Name the new class MessagePanelWithActionEvent . Test it with a Java applet that displays the current time in a message panel whenever the message panel is clicked, as shown in Figure 27.9.
[Page 913]
Figure 27.9. The current time is displayed whenever you click on the message panel.


27.2* (Creating custom event sets and source components ) Develop a project that meets the following requirements:
  • Create a source component named MemoryWatch for monitoring memory. The component generates a MemoryEvent when the free memory space exceeds a specified highLimit or is below a specified lowLimit . The highLimit and lowLimit are customizable properties in MemoryWatch .

  • Create an event set named MemoryEvent and MemoryListener . The MemoryEvent simply extends java.util.EventObject and contains two methods , freeMemory and totalMemory , which return the free memory and total memory of the system. The MemoryListener interface contains two handlers, sufficientMemory and insufficientMemory . The sufficientMemory method is invoked when the free memory space exceeds the specified high limit, and insufficientMemory is invoked when the free memory space is less than the specified low limit. The free memory and total memory in the system can be obtained using

     Runtime runtime = Runtime.getRuntime(); runtime.freeMemory(); runtime.totalMemory(); 
  • Develop a listener component that displays free memory, total memory, and whether the memory is sufficient or insufficient when a MemoryEvent occurs. Make the listener an applet with a main method to run standalone.

27.3** ( The Hurricane source component ) Create a class named Hurricane with properties name and category and its accessor methods. The Hurricane component generates an ActionEvent whenever its category property is changed. Write a listener that displays the hurricane category. If the category is 2 or greater, a message "Hurricane Warning!!!" is displayed, as shown in Figure 27.10.
Figure 27.10. Whenever the hurricane category is changed, an appropriate message is displayed in the message panel.

27.4** (The Clock source component) Create a JavaBeans component for displaying an analog clock. This bean allows the user to customize a clock through the properties, as shown in Figure 27.11. Write a test program that displays four clocks, as shown in Figure 27.12.
Figure 27.11. The Clock component displays an analog clock.
(This item is displayed on page 914 in the print version)

Figure 27.12. The program displays four clocks using the Clock component.
(This item is displayed on page 914 in the print version)

27.5* ( Creating ClockWithAlarm from Clock ) Create an alarm clock, named ClockWithAlarm , which extends the Clock component built in the preceding exercise, as shown in Figure 27.13. This component contains two new properties, alarmDate and alarmTime . alarmDate is a string consisting of year, month, and day, separated by commas. For example, 1998,5,13 represents the year 1998, month 5, and day 13. alarmTime is a string consisting of hour , minute, and second, separated by commas. For example, 10,45,2 represents 10 hours, 45 minutes, and 2 seconds. When the clock time matches the alarm time, ClockWithAlarm fires an ActionEvent . Write a test program that displays the alert message "You have an appointment now" on a dialog box at a specified time (e.g., date: 2004,1,1 and time: 10,30,0).
[Page 914]
Figure 27.13. The ClockWithAlarm component extends Clock with alarm functions.



[Page 915]
27.6*** ( The Tick source component ) Create a custom source component that is capable of generating tick events at variant time intervals, as shown in Figure 27.14. The Tick component is similar to javax.swing.Timer . The Timer class generates a timer at fixed time intervals. This Tick component can generate tick events at variant time intervals as well as at fixed time intervals.
Figure 27.14. Tick is a component that generates TickEvent .

The component contains the properties tickCount , tickInterval , maxInterval , minInterval , and step . The component adjusts the tickInterval by adding step to it after a tick event occurs. If step is 0, tickInterval is unchanged. If step > 0 , tickInterval is increased. If step < 0 , tickInterval is decreased. If tickInterval > maxInterval or tickInterval < minInterval , the component will no longer generate tick events.

The Tick component is capable of registering multiple listeners, generating TickEvent objects at variant time intervals, and notifying the listeners by invoking the listeners' handleTick method. The UML diagram for TickEvent and TickListener is shown in Figure 27.15.

Figure 27.15. TickEvent and TickListener comprise an event set for a tick event.


[Page 916]

Create an applet named DisplayMovingMessage , and create a panel named MovingMessage to display the message. Place an instance of the panel in the applet. To enable the message to move rightward, redraw the message with a new incremental x -coordinate. Use a Tick object to generate a tick event and invoke the repaint method to redraw the message when a tick event occurs. To move the message at a decreasing pace, use a positive step (e.g., 10) when constructing a Tick object.

 


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