HW#5 --- last modified January 16 2019 00:35:13..A solution set is posted here. Due date: Nov.14 in addition to you source code a project report ========= will be due start of class. This project report should contain a brief description of what GUI designs you tried during Iteration 2 and Iteration 3 and should also have the three signatures of the three people you tried these designs on. Files to be submitted: cs151sec3hw5file1.jar ====================== (After unjarring, your programs will be run with the commands: java cs151sec3hw5file1 java cs151sec3hw5file2 java cs151sec3hw5file3 ) Purpose: To get more familiar with Java's GUI and I/O frameworks. ======== To use the iterative development process, the Composite, Decorator and State patterns. To think a little about GUI usability issues. Specification: ============== For this homework you will create a computer version of a toy resembling either Mr.or Mrs. Potato Head. Your program will be developed in three iterations: Iteration 1.(cs151sec3hw5file1) Your program should launch a JFrame on which a potato shaped object is draw. On this image should be drawn small circles at places where the hat, eyes, ears, nose, mouth, arms, and feet could be drawn. Clicking on these circles makes (depending on which one was clicked) either a hat, eyes, nose, etc. appear. To do Iteration 1 you should create a subclass of JPanel (say called HeadPanel) which you add to the JFrame. Use the same technique as in the last homework to paint a GIF image of a potato onto this panel any time paintComponent is called. You should also create a class called Hole which implements Serializable. A Hole has an centerX and a centerY location, a width and a height as well as an ImageIcon. Hole's have a drawHole(Graphics g) method which draws the Hole to the Graphics context g. If the ImageIcon is null this method draws a circle centered at centerX, centerY of dimensions width and height. Otherwise, it draws centered at these coordinates the ImageIcon. A Hole also has a method setImageIcon(ImageIcon img). Your HeadPanel should have an array of Hole's. Besides drawing the image of a potato each Hole on the HeadPanel should be have its drawHole method called in paintComponent. Add a MouseListener subclass to the HeadPanel and use the getX() and getY() methods of MouseEvent to figure out where on the potato image the mouse was clicked. Then check if this point is contained in any Hole on the HeadPanel. If so, call its setImageIcon() method with the correct image for that part of the body (you can hard code this for this iteration). You can be a bit sloppy and treat any click within the Hole's rectangle as a click on the circle part of the Hole. To set up your HeadPanel you should have a constuctor HeadPanel(String head, Hole[] holes). head is the name of the file with the potato (or other image) and holes is an array of holes where we can click. Your HeadPanel's should also have setHoles(Hole[] h), and getHoles() method. Iteration 2. (cs151sec3hw5file2) In this iteration you allow the user to select which kind of body part to plug into a given place. For instance, you can put eyes where the mouth belongs. Or you may choose to place eyes with glasses rather than eyes without glasses on the head. Your design from Iteration 2 should be kept extensible so that it is easy to add new potato head parts. It is up to you to decide how to do the selection of the part to place on the potato head. One option would be to use a list of buttons on the side of the HeadPanel with icons on them like in the draw program example in Chapter 7. You would probably want to arrange the buttons into groups. Say groups of eye parts. You would also want to think of some way so that the user knows what the currently selected part is. Another option would be to use a JPopupMenu that appears when a given hole is clicked allowing the user to select the part to place there. You can write a new MouseListener for the HeadPanel in this iteration and use the State pattern from Chapter 7 in the book to appropriate modify just clicked Holes according to the currently selected body part. You should try a couple of GUI designs for this iteration but submit only your best. You should have three people try out each design and sign on your project report that they tested it. Write in your project report what you learned from these tests. Iteration 3. (cs151sec3hw5file3) In this iteration you should add a JMenuBar to your program along with menus to further allow the user to control your potato head. Options you must support are saving and loading a potato head, and an exit option. To save potato heads to and from the disk you can use the the getHoles and setHoles method of HeadPanel and also the readObject and writeObject method of ObjectInputStream and ObjectOutputStream to read and write the array of Holes. You can use JFileChooser to have the user select filenames. The GUI design issue which is up to you how to resolve in this phase is how to let the user undo the placement of a body part. Again, test a couple solutions on three friends and choose the better solution, get your friends to sign a sheet of paper saying they tested your program. Point Breakdown =============== Departmental coding guidelines followed....1pt One page project report containing friend signature................................1pt Iteration 1 program works as described.....1pt Hole and HeadPanel classes as described....1pt Iteration 2 program works as described.....1pt State pattern use constructively...........1pt (Iteration 3) Save and load potato heads works...........1pt Can undo body part placement...............1pt Total......................................8pts There will be a 1 point bonus for people who develop especially cool potato heads. A solution set is posted here. |