Hi everyone! I finally have a nicely working version of my program and I am looking to clean up the file input/output.
Currently I have used Strings, Files, and URLs, in a blended mess of concatenation, and though it all works perfectly,
I need to expand in re-usability, and professionalism. My desire is to make your "Standard" file opener, exactly like say Microsoft word.
The viewer will allow the user to look through the folder visually and select a file to load, as well as when saving see if they are saving
over a file name already being used.
If anyone knows of a good library that already has a class that does this, I would be extremely grateful, as putting together the GUI to do it from scratch seems almost as complex as my program itself.
An example of the code I use currently to achieve these results:
public CenterGrid(String fileName) { JPanel gridHolder = new JPanel(); File file = new File(fileName); //Not responsible for checking for valid fileName. That is up to the loader. BufferedReader reader; // TODO: z CONVERTING URL EXAMPLE: (Project Aid) // System.getProperty("user.dir") = C:\Users\Jons\Desktop\TestFile Loading // curLineInfo = /Images/Terrain/Grass.png // URL NEEDS TO LOOK LIKE = file:/C:/Users/Jons/Desktop/TestFile Loading/Images/Terrain/Grass.png try { reader = new BufferedReader(new FileReader(file)); String curLineInfo; myHeight = Integer.parseInt(reader.readLine());// TODO: x currently ignoring standard error checking. need to make sure everything is covered. myLength = Integer.parseInt(reader.readLine());// TODO: x currently ignoring standard error checking. need to make sure everything is covered. tiles = new Tile[myHeight*myLength]; //creates array long enough for the area of Tile objects. gridHolder.setLayout(new GridLayout(myHeight,myLength)); int idx = 0;// keeps track of the array index while ((curLineInfo = reader.readLine()) != null) {// TODO: x currently ignoring standard error checking. need to make sure everything is covered. Tile tile = new Tile(); String terrainStringURL = ("file:/" + System.getProperty("user.dir").replace('\\','/') + curLineInfo); //build curLineInfo into a useable URL Terrain terrainFromFile = new Terrain(new URL(terrainStringURL), tile); tile.addTerrain(terrainFromFile); gridHolder.add(tile); tiles[idx]=(tile); idx++; } } catch(FileNotFoundException e){} catch (IOException ex) {}// TODO: x currently ignoring standard error checking. need to make sure everything is covered. add(gridHolder); gridHolder.setVisible(true); }
So I have sections of zoo like appearance I would prefer to clean up before I handle my programs ability to load multiple folders.
I will write a class to clean up the "on the spot" code look, but before I do, I wanted to see if I could get the golden apple and instead
get the windows style loader, and just rewrite to use that classes functionality instead.
Hopefully I am being clear.
I will post a .zip file that has the .jar and the folders needed to run the program so you can get a feel for what is is capable of.
Thanks as always,
Jonathan