5.8 Homework Assignment 5
Due date and time: October 21, 2005 at 11:59:59am (noon)
This homework focuses on Event Listeners, Graphics, and Swing applications.
5.8.1 Part 1 - Employee Database Manager Swing Application
In your last 2 homeworks, you developed a data structure and an applet GUI for an employee data manager. In this homework, you will extend this work once again by implementing event listeners to make the interface interactive. Part of this interactivity requires you to maintain the data structure "underneath" the interface, as we have seen in class. You will abandon the Applet idea for this part altogether, and will instead implement the UI in Swing. You will see that this is not the most time-consuming part.
The recommended steps for this assignment are:
- Build a Swing
JFrame (see template)
- Move the AWT GUI interface you have developed in the last assignment to the Swing application.
- Buttons becomes JButtons
- Lists become JLists
- Checkboxes become JCheckBoxes
- Implement event listeners for all GUI components that will be used interactively. For example, the "Add" button for adding an employee should read the UI components' states (checked boxes, filled out textfields, etc.), create an Employee record, and add the data to the data structure.
You will have to use your own judgement about which event listeners you should add. The final Swing application must be functional in the sense of adding users, checking users' information, etc.
Before dealing with the actual data structure, it is recommended to simply use
to print the actions to the screen. For example, when pressing the "Add" button, the following information is of importance:
- What type of user? Is it a professor, student, or support staff? Use
to print the type to the command line window for the time being. Once you begin creating data structures, you can then use this information and place it in data objects instead.
- What's this person's name? Use the appropriate instance method (
) to obtain text from the JTextFields. Print it to the command line.
- Given the type of user, what are the special fields? Support staff may be unionized (use appropriate instance method to check whether the checkbox for this field has been checked or not). Print the result to the command line. Repeat for the other fields.
- Once you have enabled the event listeners and the correct information is printed to the command line, begin building data objects (Professor versus Student versus SupportStaff) and add the newly created object to a data structure. The data structure can be as simple as a hash table from the previous assignment.
- Now update your code to reflect newly added data in the second part of your GUI. This part, if you remember, allows for querying the data and paying the particular individuals. The UI included some list from which users can be selected. Similar to the example from class, you should create a method to update the list of current users by first emptying it out completely, and then rebuilding the contents by iterating over the current data structure. You can use an alternative approach, but you may find this one to be simplest.
- Error checking:
- When adding a user, you should do some error checking, i.e. check if the user already exists. If the user does exist, she cannot be added to the data structure. Instead, print an error message in the Status Bar which you should have implemented in the last assignment.
- Do similar error checking to see if the department name or support area name are empty. Every person must exist in some department.
- Add functionality to the interface used for querying the list of users, paying employees, and printing out their information.
If you are unsure about your code from the last homework assignment, you are welcome to use the posted solutions at the end of last lecture. There are no penalties involved with doing so.
5.8.2 Part 2 - Kaleidoscope
In this part you will finalize the Kaleidoscope applet by drawing filled shapes which you have implemented in a previous homework assignment. You will need to revisit those files, and make a few changes, outlined below. This part focuses more on the drawing aspect, and less on UI.
To implement a fully functional interface for the Kaleidoscope you will have to use the following template. It makes use of Multithreading, a topic not covered here. However, you can use the template and add your code in the specified areas and still make use of this functionality.
The following items are of interest:
Kaleidoscope: public void init()
: This is where an interface for an applet is built. The interface is already complete, according to the specifications from the previous assignment. If you like, you can change the UI. The ActionListener is already implemented and ready to be used.
Kaleidoscope: public void run()
: This method runs the thread, and loops until the thread is terminated by the Stop button. The refresh rate has been set to 100 milliseconds - you can change this value. Each iteration, a random shape should be generated with a random color. The shape is then drawn in a different class (KaleidoscopeCanvas), which is called from this run()
A Kaleidoscope should ideally have 4 or 8 mirrored images of the same object, a diagonally divided screen. If you generate a circle at position (100,100) in an 800x600 screen, then the same circle should also appear at position (700,100), (100,500), and (700,500).
It is up to you how you would like to implement this division functionality, as long as you adhere to the diagonal splitting of the screen. For example, you can make 4 (or 8) calls for drawing a shape in each iteration.
When generating random shapes, make sure that you are only using shapes that have been selected in the UI! You should check the states of the individual checkboxes.
For the sake of design, you may want to consider creating a few (private) methods to compute stuff and generate Shapes.
KaleidoscopeCanvas: public void drawShape()
: In the Kaleidoscope example, this method is called in each iteration within method run(). You should probably pass a Shape object to this method and create a reference to it within this class. The method call repaint() at the end of this method actually calls another method paint(Graphics g) at the end of the class. Method paint does the actual painting of graphics.
public void paint(Graphics g)
: When this method is called, a Graphics object is passed along. Given the previously referenced Shape object, you should call the Shape's draw method, and pass the Graphics object to the Shape. The shape can then draw itself onto the KaleidoscopeCanvas.
In order to reuse the Shape class files from one of the previous homeworks, you will need to make a few changes to them:
- Previously, the variable
color was of data type String. Now that we are dealing with actual colors, this variable needs to be modified to data type Color. Remember that class Color is defined in java.awt, and must be imported separately, i.e. import java.awt.*
- Previously, we used a method called
public void printInfo()
to print out a shape's information to the screen. This method can be kept, but will not be used for the final applet.
You should implement a method called
public void draw(Graphics g)
, which takes a Graphics object (defined in java.awt.*). When passed a Graphics object, the Shape should draw itself using the Graphics object. Basically, once you have g
, simply use it to draw stuff, e.g. g.drawLine(...)
A few more notes:
- The shape triangle does not exist by itself. You will have to figure out what method to use for drawing it.
- You should be using the drawing methods that FILL a shape, as opposed to DRAW (outline) it.
- Colors should be random. You can either select a color from an array of colors, or actually create random colors using RGB (red green blue) values. Quite frankly, random RGB values are easier to create than selecting randomly from an array. Use the Color constructor for
Color(int r, int g, int b) for this purpose.
To give you an idea of what a Kaleidoscope MIGHT look like, here is an Applet that implements it using only lines. Remember that you should be using 5 different kinds of shapes.
The source code to this applet is not very useful for your purposes, but to give you the benefit of the doubt, here it is:
| || Swipe.java
Thread for generating periodic swipe-screen effects||