COMP SCI 3013 & 7089 - Event Driven Computing Semester 2, 2018Assignment 3The due date: 11:59 pm, Wednesday, 31st of October 2018.This assignment is worth of 13% of the total course mark.1 OverviewIn this assignment, you will implement a complete graphical Map editor, in a classcalled MapEditor, that permits editing of maps and simple trip planning. The mapthat you display will be stored in the MapImpl class you wrote for assignment 2. YourMap editor program should be able to read a map from a file, or write one to a file,using the MapReaderWriter class you built previously. You will also be able to plantrips via the methods in your MapImpl class.2 SpecificationExercise 1 Building the GUIYour GUI will have a single large panel to display a representation of the mapyou are editing. There will be a menu-bar with two items to allow a user to controlthe program: A [File] menu that contains the options: [Open...], [Save as...],[Append...], and [Quit].– The [Open...] item pops up a file dialogue (JFileChooser) and allowsthe user to choose a map file to read in and display. Any existing map onthe display is discarded.– The [Save as...] item pops up a file dialogue (JFileChooser) andallows the user to choose a file, or enter a file name, into which a representationof the map currently presented on the screen will be written.– The [Append...] item pops up a file dialogue (JFileChooser) and allowsthe user to choose a map file to read in and append to the existing diagram.Note that the default file-extension for all map files is “.map”.– The [Quit] item allows the user to quit the program. If the user hasmade changes to the map that have not been saved to a file, the programshould warn the user and offer the choice of proceeding or cancelling. An [Edit] menu that contains the options: [New place], [New road], [Setstart], [Unset start], [Set end], [Unset end], and [Delete]. These optionare described in more details below.You should also add a keyboard shortcut for each file-menu item(see JMenuItem.setAccelerator()). These are: [File] → [Open]: Control-O [File] → [Append]: Control-A1COMP SCI 3013 & 7089 - Event Driven Computing Semester 2, 2018 [File] → [Save as]: Control-S [File] → [Quit]: Control-QAttach a listener to the [Quit] item that will cause your program to quit. For themoment, attach listeners to the other menu items that simply print out an appropriatemessage (such as “Open selected”, when the open menu item is selected). Please,make sure that your editor can be resized!Exercise 2 Verify the basicsYou should build all of the above parts in a class named MapEditor and thenverify that each part is working before proceeding. Your MapEditor class shouldcontain a main() method so that your program can be executed. Verify that allthe menu items function correctly and print out the expected messages. Verify that[Quit] causes the program to quit. Do not proceed until you have verified all is well!Exercise 3 Read and write filesExtend your program so that the [Open], [Append], and [Save as] menu itemscorrectly open and read in, append, and save map files. You should use the MapImpland MapReaderWriter classes you wrote for assignment 2 to do this job. Open a fileand verify that you can successfully save it again. Read in a file, append another toit, and write out the result. Check that the result is correct.If an exception is thrown by the MapReaderWriter code, your program shouldpop up a dialogue box (JDialog) that displays the error message, and then allow theuser to press an [Ok] button to dismiss it. Read in a file that has errors and checkthat the error dialogue pops up and can be dismissed. After an error, verify that youcan still open another file correctly. Once again, test thoroughly.Exercise 4 Build the map panelCreate a new class, named MapPanel, that extends JPanel and implements MapListener,and add it to the main JFrame of your MapEditor. Make sure that the layout managerfor this panel is set to null, or you will experience a lot of strange behaviourlater on. MapPanel will need to implement the three methods of the MapListenerinterface. For the moment, each of the three methods should simply print out amessage that says something like “placesChanged” when the method is called.Exercise 5 Test the MapListenerSimilarly to exercise 3, test your program by reading in files. Each time thereader-writer adds a place to your MapImpl, you should find that the MapListeneryou have implemented gets called and prints its message. For example, if the file youopen contains four places, there should be four calls to placesChanged() printedout. Similarly, there should be calls to roadsChanged() each time a road is added,and to otherChanged() if a start or end place is read. Note that through all thesetests MapPanel will be blank since nothing has been added to it yet. Don not worry,that is coming soon!2COMP SCI 3013 & 7089 - Event Driven Computing Semester 2, 2018Exercise 6 Create a PlaceIconBuild a new class named PlaceIcon that extends JComponent and implementsPlaceListener. This class will be used to display a place on the screen. You willneed to override the paintComponent() method to make the PlaceIcon painting acoloured square on the screen.Exercise 7 Test the PlaceIconThe easiest way to test the PlaceIcon is to add some code to the constructor inthe MapPanel class so that it adds a PlaceIcon to the panel at a known location(such as 0,0). When you execute the program, you should see one square that youhave drawn. However, you probably will not, because you need to get quite a lot ofthings “right” for this to happen. You will need to do some debugging to figure outwhat is wrong. If all is well, add a few more PlaceIcon objects at other locationsto check that the program can handle multiple PlaceIcon objects on the screen atonce. Don not proceed until you have verified that all is well!Exercise 8 Display placesModify the placesChanged() method in the MapPanel so that each time it iscalled it calls the underlying MapImpl to find out what places have changed. Todo this you will need to compare the list of places in the MapImpl (returned bygetPlaces()) with the list of places stored in your MapPanel. (You will need aninstance variable to help with this.)Build code so that whenever a new place (say p) is added, your program createsa new PlaceIcon and adds the PlaceIcon as a listener to the new place p. Arrangethat the placeChanged() method in the place icon sets the location of the PlaceIconto the coordinates of the place p and then calls repaint().Then add the place icon to the MapPanel, so it will be displayed. Note: For eachplace there will be a corresponding PlaceIcon registered as a listener of that place.The PlaceIcon will need to keep a pointer to its underlying Place so that it can laterget information from the place (such as its location). In future, whenever the placechanges, your PlaceIcon will receive a call to placeChanged() that tells it aboutthe change.Exercise 9 Verify that the PlaceIcons appearOnce again, read in a file as in exercise 3. You already know that each time a placeis added your placesChanged() method will be called (verified in exercise 5). Now,with the new code you have added, you should find that your program displays anew icon for each new place. Remember that the essence of testing is simplicity first.The first file you test should have just one place in it. When that works correctly,you can try more complex files!Exercise 10 Add mouse actionsModify the PlaceIcon class so that it adds a MouseListener to the JComponent.You will need to implement five methods: mouseEnetered(), mouseExited(), mousePressed(),mouseClicked(), and mouseReleased(). Simply make each body printing out amessage that says something trivial, such as “mousePressed”. Compile and execute3COMP SCI 3013 & 7089 - Event Driven Computing Semester 2, 2018your program. Verify that when the mouse is pressed, clicked, released, etc. withinthe MapPanel, the appropriate message is printed. Do not proceed until you haveverified all is well!Exercise 11 Select PlaceIconsModify the PlaceIcon code so that it includes a boolean field named isSelected.Modify the mouseListener code in MapPanel so that when the mouse is clicked onan icon, the selected field of the PlaceIcon is flipped from false to true or from trueto false. Change the paintComponent() method in the PlaceIcon so that you cansee the state of this variable by painting a filled square when the place is selected,and an outline square when it is not. Read in a map containing several places. Clickthe mouse on the places and verify that you are able to select and deselect places,and that each time you do so the icon changes. (Hint: If this does not happen, tryresizing the window a little bit. If resizing makes the display show correctly, thenyour program is missing a call to repaint.) Do not proceed until you have verified allis well!Exercise 12 Complete the place-selection codeModify the code in MapPanel so that clicking on a place deselects any places thatare already selected. Clicking twice on the same place should select then deselect it.Clicking on the background should clear all selections. Test thoroughly!Exercise 13 Set/Unset start and end placesAdd an item to the [Edit] menu that allows a user to set start place. To use thisoption, the user first selects a place (by clicking on it) and then chooses the menuoption [Set start]. The program should set the selected place as the start place bycalling setStartPlace() in MapImpl. If more than one place is selected, pop up adialogue that says “only one place can be selected”, waits for the user to click [Ok],and then ignores the operation. If you have not done it earlier, you may need to addsome code to the PlaceIcon object’s paintComponent() method to display a startplace in some visibly-different way (e.g. a different colour or a thicker border). Ifyour MapImpl works correctly, you will not need to do anything to make the displayupdate because the PlaceListener for the selected place will automatically notifythe corresponding PlaceIcon. If this does not happen, think carefully about whatneeds to be changed. (Hint: You may need to modify your MapImpl.) Add a menuitem [Set end] place, that behaves similarly to [Set start] place. Add a menuitem [Unset start] place that unsets the start place. Add a menu item [Unsetend] place that unsets the end place. Test thoroughly!Exercise 14 Add mouse-motion actionsModify the PlaceIcon class so that it adds a MouseMotionListener to theJComponent. You will need to implement two methods: mouseMoved, and mouseDragged.Initially, make these methods printing a simple text and verify that they are working.Now write code so that pressing the mouse-button, when the cursor is over a placeicon,and then dragging the mouse causes that place-icon to be dragged. You willneed to work out how far the mouse has moved, and then call moveBy() on the Place4COMP SCI 3013 & 7089 - Event Driven Computing Semester 2, 2018attached to the PlaceIcon. (You will probably find you need information from themousePressed method to help you do this.) You should not need to add anythingto cause the place to be repainted after the move because the listener code you havebuilt in the Place will (should!) automatically call the attached PlaceIcon and tellit about the change. If all is well, you will be able to click-drag a PlaceIcon on thescreen and it will follow your mouse. Do not proceed until you have verified all iswell!Exercise 15 Add a selection boxModify the MapPanel code so that when the mouse-button is pressed over thebackground (not on a PlaceIcon), your program draws a selection-rectangle on thescreen that follows the mouse pointer. If you drag the mouse right-and-down, thebox will grow normally. If you move up or to the left, you will probably find that thebox is not drawn on the screen because the Rectangle class cannot handle rectangleswith negative width or height. You will need to modify your code so that selectionworks correctly in this case. If all is well, when you press-drag on the MapPanelbackground, a box will appear and follow your mouse. When you release the mouse,the rectangle should disappear. Note that you should be able to press-drag in anydirection, and the selection box should always be drawn. Hint: By now your code forhandling the mouse will be getting reasonably complex. You may want to considerdesigning and implementing an FSA to keep track of what is happening. You willcertainly need it for later stages of the assignment.Exercise 16 Make the selection box workNow when you can draw the selection box, we need to make it working correctly.Each time the mouseDragged method is called, check to see if the selectionboxintersects any of the PlaceIcon objects of the MapPanel. You can get thelist of PlaceIcon objects by calling getComponents(). You can check for intersectionusing the rectangle.intersects() method and the getBounds() methodof the PlaceIcon. If you find a PlaceIcon that is inside the selection rectangle,set its isSelected flag to true. If the PlaceIcon does not intersect the selectionrectangle, set its isSelected flag to false. (If your code is correct, the icon willautomatically change to show they are selected/deselected because of calls made tothe placeChanged() listeners.) The effect of all this should be that when you dragthe selection rectangle over place icons, the ones inside the rectangle are displayedas selected, and the ones outside are displayed as not selected. Test thoroughly!Exercise 17 Display the roadsBuild a new class named RoadIcon that extends JComponent and implementsRoadListener. This class will be used to display a road on the screen. RoadIconand Road will operate very similarly to PlaceIcon and Place, so you may be ableto copy quite a lot of the code. You will need to override the paintComponent()method to make the RoadIcon drawing a straight line between the two places, andto draw text that shows the name and length of the road (in the middle of the road).Your roads need to come neatly from the boundary of the square (the printed lecturenotes show how to do this in a few lines of code). Modify the MapPanel so that5COMP SCI 3013 & 7089 - Event Driven Computing Semester 2, 2018the roadsChanged() method adds a RoadIcon to the MapPanel whenever a Road isadded to the underlying MapImpl. You can test your implementation by reading ina file that has two places and one road. The places should be displayed correctlybecause of the testing you have done in earlier steps. If all is well, your RoadIconwill correctly display the road on the screen. If it is not, displayed, or is in the wrongposition, you will need to do some debugging. Do not proceed until you have verifiedall is well!Exercise 18 Verify that roads workMake a small file that has three places and three roads. Load it into your editorand display it. If all is well, there will be three places and three roads showing. Nowclick-drag on one of the places and move it on the screen. If all is well, the place willmove (because you verified this behaviour earlier), and the road will move also. If itdoes not, there is probably an error in your MapImpl, where the Road is not callingroadChanged when either of the end-points of the road changes – think carefullyabout how to fix this. Important: The right solution is very simple code, the wrongsolution is very messy! Test!Exercise 19 Add a new placeAdd an entry [Add place] to the [Edit] menu that allows a user to add a newplace. When the user selects this menu item, a dialogue (use JDialog) that asks theuser for the name of the place and offers [Ok] and [Cancel] options. If the userpresses [Cancel], the operation is cancelled. If the user presses [Ok], the programattempts to create a new place with the given name, in the middle of the screen.If there is an error (e.g. the name is illegal, or the place already exists), display adialog showing the error message, wait until the user clicks [Ok], and then ignore theoperation. To create a new place, you need only to add the place to the MapImpl bycalling addPlace(). The listener mechanisms that you have built and tested earliershould then automatically create the corresponding icon for the place on the screen,and display it. Once the new place is displayed, the user can move it to the desiredlocation on the screen using the mouse (click-drag). You can verify that the placehas been added correctly by saving the modified map to a file and checking that thenew place is in the file. Check carefully.Exercise 20 Add a new roadAdd an entry [Add road] to the [Edit] menu that allows the user to add a newroad. This works very similarly to [Add place]. When the user selects this menuitem, a dialogue should pop up to ask the user for the name of the road and its length,and offer [Ok] and [Cancel] options. If the user presses [Cancel], the operation iscancelled. If the user presses [Ok], the program changes the mouse-cursor to a crossand waits for the user to click on the start-place for the road. Once the user haschosen the start place, the program should draw a rubber-band line from the startplace to the mouse cursor. (The line should follow the mouse around as the mouseis moved.) When the user clicks on the second place, the program inserts the roadinto the MapImpl. (Hint: You will probably need to build an FSA in the MapPanelto manage this behaviour.) If there is an error (e.g. the name is illegal or the road6COMP SCI 3013 & 7089 - Event Driven Computing Semester 2, 2018already exists) display a dialog showing the error message, wait until the user clicks[Ok], and then ignore the operation. (Hint: Make certain that when an error occurs,your FSA ends up in the right state.) To create a new road, you need only to add theroad to the MapImpl. The listener mechanism that you have built and tested earliershould automatically create the icon for the road on the screen. You can verify thatthe road has been added correctly by saving the modified map to a file and checkingthat the new road is in the file.Exercise 21 Update trip distanceYour GUI must display the distance for the current trip (as calculated by yourMapImpl). Please note that if there are multiple paths from the start point to theend point, it is not required to identify the shortest path when calculating the tripdistance.You can randomly select one path and display the distance of that path. Ifthe endpoint is not reachable from the start point, display “No route” instead of thedistance. Whenever the start- or end-place changes, your program will need to recomputethe distance. You will know that a change has occurred, because your MapImplwill (should!) call the otherChanged() listener. If a route exists, your MapPanelmust highlight the roads on that route - use a brighter colour, or a thicker line. Ifyour MapImpl and RoadImpl are correct, this should require only trivial changes toyour RoadIcon. If it does not work, think carefully about what exactly needs to bechanged.Exercise 22 Delete places (BONUS)You do not need to complete this step to receive full marks. If you do implementit correctly, you will receive one bonus mark. Add a menu item [Delete] thatdeletes the selected items from the map. To use this operation, the user selects oneor more places on the map (by clicking on them or by using the selection rectangle),and then chooses the delete option from the menu. The program now deletes thoseplaces from the map. You will probably need to modify the code that implementsplacesChanged() so that it correctly handles the removal of a place or a road. If youdo this correctly, you should find that the rest of your program will correctly handlethe changes to the display. When you delete a place, there are many things thatneed to be done (unhook listeners, remove the Place object, remove the PlaceIconobject, etc) – it is easy to do an incomplete job, and then weird things will happen.You may need to print out the contents of some of your data-structures to be sureyou have got this right. You can verify that deletion is working correctly by savingthe modified map to a file and checking that the deleted items are no longer in thefile.Exercise 23 Delete roads (BONUS)You do not need to complete this step to receive full marks. If you do implementit correctly, you will receive one bonus mark. Extend the behaviour of the [Delete]menu item so that it can delete a road. To use this option, the user selects one (ormore roads) and/or places on the map (by clicking on them or by using the selection7COMP SCI 3013 & 7089 - Event Driven Computing Semester 2, 2018rectangle), and then chooses the delete option from the menu. The program shoulddelete those roads and/or places from the map. The implementation issues are similarto the previous step. You will find that making roads selectable is tricky because thebounding box covers a large area of the screen.3 Testing and AssessmentSince the program is controlled by a GUI, it cannot be tested automatically. We willtest each GUI by hand after the hand-in deadline. During testing, we will check forfeatures in the order described in the steps above. Marks will be awarded for eachfeature that works correctly.3.1 Submission instructions for programming codeFirst, type the following command, all on one line (replacing aXXXXXXX with yourusername):svn mkdir --parents -m EDChttps://version-control.adelaide.edu.au/svn/aXXXXXXX/2018/s2/edc/assignment3Then, check out this directory and add your files:svn co https://version-control.adelaide.edu.au/svn/aXXXXXXX/2018/s2/edc/assignment3cd assignment3svn add *.javasvn commit -m assignment3 solutionNext, go to the web submission system at:https://cs.adelaide.edu.au/services/websubmission/Navigate to 2018, Semester 2, Adelaide, Event Driven Computing, then Assignment3. Click Make a New Submission for This Assignment and indicate that youagree to the declaration. The script will then check whether your code compiles. Youcan make as many resubmissions as you like.转自:http://ass.3daixie.com/2018110131238800.html
讲解:COMP SCI 3013、Java、 GUI/MapReaderWriter、JavaDatabase|Ja
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- By clicking to agree to this Schedule 2, which is hereby ...