/**
* WorldCreator.java
*
* @author David Tran
* @version 2.0
*/
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...
// Project 3 Part 1 Solution
// NOTE: Only the first 4 lines plus the return statement are required.
w.addWorldObject(new VerticalRoad(300.0, 0.0, 50.0, 90.0));
w.addWorldObject(new VerticalRoad(300.0, 350.0, 50.0, 150.0));
w.addWorldObject(new HorizontalRoad(0.0, 50.0, 600.0, 50.0));
w.addWorldObject(new HorizontalRoad(0.0, 200.0, 150.0, 50.0));
// Add all the other backgrounds to the world (not required)
w.addWorldObject(new HorizontalRoad(0.0, 350.0, 600.0, 50.0));
w.addWorldObject(new HorizontalRoad(0.0, 500.0, 600.0, 50.0));
w.addWorldObject(new VerticalRoad(0.0, 0.0, 50.0, 500.0));
w.addWorldObject(new VerticalRoad(150.0, 0.0, 50.0, 500.0));
w.addWorldObject(new Intersection(150.0, 50.0, 50.0, 50.0));
w.addWorldObject(new Intersection(150.0, 350.0, 50.0, 50.0));
w.addWorldObject(new Intersection(150.0, 500.0, 50.0, 50.0));
w.addWorldObject(new Lake(60.0, 100.0, 50.0, 50.0));
w.addWorldObject(new Tree(120.0, 100.0, 20.0, 20.0));
w.addWorldObject(new Tree(70.0, 170.0, 20.0, 20.0));
w.addWorldObject(new BaseballField(200.0, 100.0, 250.0, 250.0));
w.addWorldObject(new Tree(55.0, 325.0, 20.0, 20.0));
w.addWorldObject(new Tree(125.0, 325.0, 20.0, 20.0));
w.addWorldObject(new House(55.0, 250.0, 90.0, 50.0));
return w;
}
/** [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;
}
}