辅导Java程序、辅导Java编程、辅导JAVA留学生MapEditor

- 首页 >> Java编程

In this assignment, you will implement a complete graphical Map editor, in a class

called MapEditor, that permits editing of maps and simple trip planning. The map

that you display will be stored in the MapImpl class you wrote for assignment 2. Your

Map 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 plan

trips via the trip() method in your MapImpl class. 2 Specification

Exercise 1 Building the GUI

Your GUI will have a single large panel to display a representation of the map you are editing. There will be a menu-bar with two items to allow a user to control the program: • A [File] menu that contains the options: [Open...], [Save as...], [Append...], and [Quit]. – The [Open...] item pops up a file dialogue (JFileChooser) and allows the

user to choose a map file to read in and display. Any existing map on the

display is discarded. – The [Save as...] item pops up a file dialogue (JFileChooser) and allows the

user to choose a file, or enter a file name, into which a representation of

the map currently presented on the screen will be written. – The [Append...] item pops up a file dialogue (JFileChooser) and allows the

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 has made

changes to the map that have not been saved to a file, the program should

warn the user and offer the choice of proceeding or cancelling. • An [Edit] menu that contains the options: [New place], [New road], [Set start], [Unset

start], [Set end], [Unset end], and [Delete]. These option are described in more details

below. 1

You should also add a keyboard shortcut for each file-menu item (see JMenuItem.setAccelerator()). These are: • [File]->[Open]: Control-O

• [File]->[Append]: Control-A

• [File]->[Save as]: Control-S

• [File]->[Quit]: Control-Q

Attach a listener to the [Quit] item that will cause your program to quit. For the

moment, attach listeners to the other menu items that simply print out an appropriate

message (such as “Open selected”, when the open menu item is selected). Please, make sure that your editor can be resized!

Exercise 2 Verify the basics

You should build all of the above parts in a class named MapEditor and then verify

that each part is working before proceeding. Your MapEditor class should contain a

main() method so that your program can be executed. Verify that all the 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 files


GUI by hand after the hand-in deadline. During testing, we will check for features in the order

described in the steps above. Marks will be awarded for each feature that works correctly.


Extend your program so that the [Open], [Append], and [Save as] menu items correctly

open and read in, append, and save map files. You should use the MapImpl and

MapReader-Writer classes you wrote for assignment 2 to do this job. Open a file and

verify that you can successfully save it again. Read in a file, append another to it, and write out the result. Check that the result is correct.

If an exception is thrown by the MapReaderWriter code, your program should pop up

a dia-logue box (JDialog) that displays the error message, and then allow the user to

press an [Ok] button to dismiss it. Read in a file that has errors and check that the

error dialogue pops up and can be dismissed. After an error, verify that you can still

open another file correctly. Once again, test thoroughly. Exercise 4 Build the map panel

Create 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 manager for this

panel is set to null, or you will experience a lot of strange behaviour later on. MapPanel will need to implement the three methods of the MapListener interface. For the moment, each of the three methods should simply print out a message that

says something like “placesChanged” when the method is called. 2

Exercise 5 Test the MapListener

Similarly to exercise 3, test your program by reading in files. Each time the reader-writer

adds a place to your MapImpl, you should find that the MapListener you have implemented

gets called and prints its message. For example, if the file you open contains four places,

there should be four calls to placesChanged() printed out. 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 these tests MapPanel will be blank since nothing has been added

to it yet. Don not worry, that is coming soon!

Exercise 6 Create a PlaceIcon

Build a new class named PlaceIcon that extends JComponent and implements PlaceListener. This class will be used to display a place on the screen. You will need to override the paint- Component() method to make the PlaceIcon painting a coloured square on the screen. Exercise 7 Test the PlaceIcon

The easiest way to test the PlaceIcon is to add some code to the constructor in the

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 you have drawn. However, you probably will not, because you need to get quite a lot of things “right” for this to happen. You will need to do some debugging to figure out what is wrong. If all is well, add a few more

PlaceIcon objects at other locations to check that the program can handle multiple PlaceIcon

objects on the screen at once. Don not proceed until you have verified that all is well!

Exercise 8 Display places

Modify the placesChanged() method in the MapPanel so that each time it is called it

calls the underlying MapImpl to find out what places have changed. To do this you will

need to compare the list of places in the MapImpl (returned by getPlaces()) with the list

of places stored in your MapPanel. (You will need an instance variable to help with this.)

Build code so that whenever a new place (say p) is added, your program creates a

new PlaceIcon and adds the PlaceIcon as a listener to the new place p. Arrange that

the placeChanged() method in the place icon sets the location of the PlaceIcon to

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 each place

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 later get

information from the place (such as its location). In future, whenever the place changes, your PlaceIcon will receive a call to placeChanged() that tells it about the change.


站长地图