/** * WorldCreator.java * * @author David Tran * @version 1.1 */ package world; import java.io.BufferedReader; import java.io.IOException; import java.io.FileReader; import java.util.StringTokenizer; import java.util.zip.DataFormatException; import java.io.FileNotFoundException; import java.io.File; import javax.swing.JFileChooser; import java.net.URL; import background.*; /** * A WorldCreator creates a World with particular Background objects * whose description and location are given in a specified file, * where each line of data is of the following format:

* * backgroundName xCoordinate yCoordinate width height

* * where backgroundName is the predetermined name refering to some background * (e.g. Vertical-Road, Lake, House)
* where xCoordinate refers to the leftmost x-coordinate of the background
* where yCoordinate refers to the uppermost y-coordinate of the background
* where width refers to the maximum horizontal distance from the xCoordinate * of the background object
* where height refers to the maximum vertical distance from the yCoordinate * of the background object

*/ public class WorldCreator { /** Name of a file to use if a good file name is not supplied as a parameter. *Generally the best place to put this file is in the same package (directory) *as the class which is processing it; in this case, the same directory as *WorldCreator.java. */ public static final String defaultFilename = "World.txt"; /** * Modifies a World with Background objects taken from a file. * * @return - World instance of a World object, with the modifed background objects. */ public static World modifyWorld(World w) { return modifyWorld(null, w); } /** * Modifies a currently existing World by adding Background objects * specified in the fileName. * If the world does not exist (i.e. w is null), a new World is created * and returned, in which the Background objects will be added. * * @param fileName the name of the file describing the background objects; * if the fileName is null or empty or if the file cannot be found, * a built-in default is tried; if that fails, * the user sees a File Chooser dialog and can browse to the desired file. * @param w the currently existing World * @return World - instance of a World object if file was read successfully, * otherwise null */ public static World modifyWorld(String fileName, World w) { //The starter code in the method can be removed for Step 1. //For Step 2, you should put it back in. if (w == null) { w = new World(); } if (fileName == null || fileName.equals("")) { fileName = defaultFilename; } java.io.File f = getFile(fileName); //make sure there is such a file //At this point, if f is not null, then the file exists. String fullAbsolutePath = null; if (f != null) { fullAbsolutePath = f.getAbsolutePath(); assert fullAbsolutePath != null; System.out.println("Using background text file " + fullAbsolutePath); } else{ System.out.println("Tried and tried, but STILL could not open a file," + " not even " + fileName); } //If fullAbsolutePath is not null, you should be able to create a stream on it. //You might also use f directly for creating the stream. //Starter code for the method ends here... } /** [DO include in the starter code] * Displays a dialog box to choose the file, if the requested one doesn't exist. * @return the file that the user selected or null if user selected cancel */ private static File getFile () { return getFile(null); } /** Locate the requested file, if possible, or a file * selected by the user. * For best results, place the file in the same directory as this class, * or in some other package used by the application. * Displays a dialog box to choose the file, if the one requested doesn't exist * * @return the file that was requested, or one the user selected, or null if user selected cancel */ private static File getFile(String requestedFilename) { String startingPath; if (requestedFilename != null) { //see if the file exists java.io.File requestedFile = new File(requestedFilename); if (requestedFile.exists() && requestedFile.isFile()) { return requestedFile; } //The file wasn't found via its original requested name, in the default location. //Look for it on the class path java.net.URL fileURL = ClassLoader.getSystemResource(requestedFilename); if (fileURL != null) { //found it on the class path String fullName = fileURL.getPath(); requestedFile = new File(fullName); assert requestedFile.exists(); if (requestedFile.isFile()) { return requestedFile; } } } //Let user browse for it, starting from the directory of the current class Class wcc = WorldCreator.class; String myClassName = wcc.getName(); String myClassFilename = myClassName.replace('.', '/'); myClassFilename += ".class"; java.net.URL classURL = ClassLoader.getSystemResource(myClassFilename); if (classURL != null) { startingPath = classURL.getPath(); } else { startingPath = "."; //last resort } JFileChooser fc = new JFileChooser(startingPath); int retVal = fc.showOpenDialog(null); fc.requestFocus(); if (retVal == JFileChooser.APPROVE_OPTION) { File file = fc.getSelectedFile(); assert file != null && file.exists(); if (file.isFile()) { return file; } } //totally out of luck return null; } }