1. Environment and program execution
In this first Data Access CEPA (Continuous Evaluation Practice Activity), we are going to implement the file persistence part of a geometric figures project partially done.
You are provided with a compressed file with the APAC1_AD Gradle project, with the figure hierarchy implementation and various utilities.
The project has been created with gradle init, which creates a small structure of directories and files for us, such as the gradlew and gradlew.bat launchers (which we will not use).
In order to build the project we will:
and to run the project:
| Bash | |
|---|---|
the --console plain option skip showing messages saying about the program execution. In addition, to pass some arguments to the program (in our program the height and width of the canvas) we must indicate as follows --args="500 500". The string in the double quotes will be the args variable of main.
Once the program is running, the following prompt will be showed:
| Text Only | |
|---|---|
This program let us draw a scene formed by several figures. The available options supported by the current version are (in Valencian):
dimensions ample alt: It sets the height and with of the canvas.cercle x y radi #color: It adds a circle to the current scene. The circle will be placed atx,ycoordinates, with the given radius and a color, in#RRGGBBformat. This color is validated by a utility function.rectangle x y height width #color: It adds a rectangle placed inx,ycoordinates, with the given measures and color.linia x1 y1 x2 y2 #color: It adds a line from the(x1,y1)starting point to the(x2,y2)final point and specified color. A fixed thickness by 3 pixels is establishe by the program.draw: It will open a JavaFX window and draw all the figures stored and introduced in a canvas. When we will close the windows, the app will be finished.
2. New behaviors
In order to improve the base program, we must add the date persistence, adding functions to store and recover scenes. The orders wue have to implement are:
import [file.txt | file.obj]\(\rightarrow\) It will import from disk scenes configurations, stored in text files or object files. The program will decide the format taking into account the file extension.export [file.txt | file.obj |file.svg | file.json]\(\rightarrow\) It will export to disk current scene configuration, saving it in text files, object files, svg format (a special xml format) or json format. The program will decide the format taking into account the file extension.
Note
All the files will be saved in the root project folder
2.1. Sample files of scenes
Text file
| Text Only | |
|---|---|
Note
This kind of xml is maded because after export this scene, you may open it with Inkscape. You can use it to prove you have done it well.
JSON format
3. Project structure
First, we have build.gradle file, with information about project building, with graphics and JSON libraries. In our project structure, we have:
| Text Only | |
|---|---|
- App \(\rightarrow\) main class. It shows the prompt and it creates the canvas. You don't have to modify this class.
- figura, cercle, linia, rectangle, punt \(\rightarrow\) these classes form the inheritance tree (except punt). They have the necessary attributes and methods. You probably need to add methods to get the representation of that class to text, json or xml. You must do the necessary classes serializables too.
- escena \(\rightarrow\) it contains some attributes and the collection of figures
- FileManager \(\rightarrow\) class that have to deal with the storing process. We have to do the main work on it, as we can show in the next point.
4. FileManager class
As we explained, here is where you have to store and recover the information in several formats.
4.1. exists method
| Java | |
|---|---|
It will return a logic value depending on the existence of the file or no.
4.2. importFromText method
| Java | |
|---|---|
Note that, in addition to the figures, the dimensions command may appear, which will indicate that we will have to modify the dimensions of the Scene.
4.3. importFromObj method
| Java | |
|---|---|
Importing a serialized scene in object format, in the format indicated at the beginning of this document. In the imgs folder, you have a couple of images to test. As you can see, it receives the path to the file in String format, and it will return an object of type Scene.
4.4. exportText method
| Java | |
|---|---|
This method will export a given scene to a text file, in the format specified above, to be able to read them with importFromText.
The method will receive the Scene object, and a String with the name of the file to be saved, with a .txt extension.
For the implementation of this method, you will find it useful to implement a method called getAsText (or similar) in each type of figure, and returns the figure itself in the format of interest. In this way, to export the scene, we will go through the different figures and obtain the representation of each one.
4.5. exportObj method
| Java | |
|---|---|
This method will export a given scene to an objects file, to be able to read it with importFromObj.
The method will receive the Scene object, and a String with the name of the file to be saved, with an .obj extension.
Warning
Remember the use of Serializable modifier on the needed classes.
4.6. exportSVG method
| Java | |
|---|---|
This method will export a given scene to a svg file, in the xml format specified above in the previous pages. The method will receive the Scene object, and a String with the name of the file to be saved, with a .svg extension.
Some details of the format:
- We include
<?xml version="1.0" encoding="UTF-8" standalone="no"?>always. - The root element is the
<svg>tag, with two attributes width and height. - Every figure inheritor will be the equivalent
rect,circleandlinetag. Look at the document sample. - To do a correct circle position, you have to add the radius to the figure position:
this.posicio.getX()+this.radiandthis.posicio.getY()+this.radi
Note
To get a correct implementation of this method (and others that will become) is interesting to proceed as we notice:
- declare an abstract method in Figura class.
getAsSVG(), for instance. - implement this method on every descent class.
4.7. exportJSON method
| Java | |
|---|---|
This method will create a JSON representation of the scene that the method receives. As the note given in the last point, is an interesting practice to create the getAsJson() method in the top class an implement in the bottom classes.
:::note
To do a more efficient work, implement the methods as follows:
importFromText, so you can import a full scene provided and could draw it.exportToText, so you can check your scene will be the same as the original one.exportToObj, so you can save a scene imported from textimportFromObj, to check the exported one has been well done.- At the end the rest of exports, to SVG and JSON :::
5. Practice delivery
To do the practice upload you have to fulfill the following:
- Execute
gradle cleanin order to clean all the packages and libraries. - Compress the project folder. Zip format is mandatory.
- Upload the practice at the aules platform, avoiding emails or other communication tools.