CS U370 Assignment #7 Spring 2008, Prof. Hafner Assigned: Tuesday, April 1, 2008 Due: Tuesday, April 15, 2008 NOTE: Programs will not be accepted more than 24 hours late. This assignment may be done individually or in teams of 2 students. Both members of a team will receive the same grade on the assignment. A slightly higher standard will be applied to work submitted by teams. Only one student from a team should submit the programs; but the blaock comment at the start of the file should clearly indicate the names of both team members. Collaboration between teams is forbidden on this assignment. You are responsible for keeping your code hidden from all other students except your team partner. Part of your grade will be determined by how well you hide your code, part of your grade will be determined by how well you follow the instructions for submitting your code, part of your grade will depend upon the correctness of your code, part of your grade will will depend upon the readability of your code (e.g. formatting and comments). Turn in your work on this assignment before 10 pm on the due date by submitting 2 files: ClockIcon.java and Clock.java through the Web-based submission system. Your file should begin with a block comment containing: 1. Your name, as you want the instructor to write it. 2. Your email address. 3. Any remarks that you wish to make to the instructor. ---------------------------------------------------------------------- This assignment gives you practice with a complex set of inter-related classes, one of the most complex in existence: the Java GUI framework including awt (Abstract Windowing Toolkit) and Swing. You will make extensive use of interfaces, and learn to use the Composite and Observer patterns. This assignment requires the use of a subset of elements of the Java 6 Swing Graphics framework: Frames, Graphics contexts, Shapes, and Icons for Part 1, and additionally the use of Timers and Listeners to achieve animation in Part 2. You will also use the GregorianCalendar built-in class from java.util. If you do the extra credit assignment you will also have to use additional widgets such as buttons and text fields. All of your programs should be in the default package. (This is probably not a realistic assumption but it will simplify the grading of a large number of programs.) You will be graded on the correctness of your program, the appearance of your clock, and the quality (including readability) of your code. Comments are required explaining how it works. Part 1. Write a public class ClockIcon that implements the Icon interface type. The paintIcon method should draw an analog clock whose hour, minute and second hand show the current time (i.e., the time the program is invoked). To get the current hour, minute and second, construct an object of type GregorianCalendar with the default constructor. You will need to consult the Sun JDK API to find out the details of how to extract the hour, minute and second from a GregorianCalendar object. Part 2. Write a public class Clock that uses the ClockIcon, which has the following behavior: When invoked, it opens a JFrame window which displays an ANIMATED CLOCK (using the ClockIcon class from Part 1), which changes second by second to update the time. Clicking the close box must cause the program to terminate. The window must have an appropriate title. You will use a javax.swing.Timer object in this program, so that the clock will "keep time". The timer's action listener should invoke the repaint() method once per second, so that the clock continues to show the correct time. The Clock class must have a main() method that displays the window containing the clock and starts it running. For extra credit: Write your Clock.java program so that the Clock graphic appears in a window accompanied by widgets that allow you to change the time by specifying a different hour and minute. The Clock should initially show the current (Eastern Daylight) time, but if the time is changed the clock should be updated to the new time indicated (with seconds = 0) and continue running from that time. The interaction design should be thought out carefully. Grading: 15 points for correctness and appearance of Part 1. 15 points for correctness and appearance of Part 2. 1-6 points for the extra credit, depending on the appearance and smooth operation of the "Change Time" function. Hints: Trig functions in the Math class, will probably be useful. These functions take arguments in radians, not degrees. The Math class also has methods toDegrees() and toRadians() for conversions. Functions in the Graphics2D class (such as translate() and rotate()) may also be used, however, using these may require extra research. The java tutorial is a good place to start. It has a chapter on Swing, and a chapter on 2-D graphics under "Specialized Trails and Graphics."