Lab 7
Get started now
## Objectives * Read text data from a file using `Scanner` * Employ exception handling to gracefully manage corrupt data files * Throw an exception when invalid data is passed to a constructor ## Resources * [WinPlotterFX Documentation](WinPlotterFX.htm) * [WinPlotterFX JAR file](WinPlotterFX.jar) ## Assignment In this assignment you will revist the work you did for [Lab 4](Lab4). In particular, you will write your own code to create a `WinPlotterFX`<sup>1</sup> instance and draw shapes in the window. The characteristics of the window and the shapes to be drawn must be read from a file. ## Details ### Shape Class Upgrades First, you must revisit each of the classes implemented for lab 4: `Shape`, `Triangle`, `LabeledTriangle`, `Rectangle`, `LabeledRectangle`, and `Circle`. You should incorporate any feedback you received from your instructor and add checks to the constructors to ensure that no shape is created with unrealistic dimensions. In particular, if a non-positive value is passed to a constructor for one of the dimensions (width, height, base, radius), an `IllegalArgumentException` with an appropriate message must be thrown. You must also implement the `Point` class described in lab 4. ### FaceMaker Replacement You must implement a `ShapeLoaderApp` class to replace the `FaceMaker` class you developed in Lab 4. Your implementation must read a file that contains information about the picture, create a window, and then draw all of the specified shapes on the window. The program must ask the user to select a file; however, you may determine how you obtain the file location from the user. An example file is shown below: <figure><pre> Demonstration Picture 800 600 #F0F0F0 P: 40 40 #000000 C: 40 40 #FF0000 30 T: 60 60 #00FF00 100 50 R: 160 160 #0000FF 50 150 LT: 360 360 #00FF00 100 50 Tri LR: 460 460 #0000FF 150 100 Reck Tangle Angle Rangle </pre><figcaption>Figure 1: Sample input file</figcaption></figure> <figure>[![Sampe Results](lab7.png)](lab7.png)<figcaption>Figure 2: Picture generated by the example in Figure 1</figcaption></figure> The file has the following format: * Line 1: contains the title for the window * Line 2: contains the dimensions of the window specified as integer values for **width** (first) and **height** (second) separated by whitespace * Line 3: contains the background color for the image specified as a [hex triplet](http://en.wikipedia.org/wiki/Web_colors) (six-digit hexadecimal) color * Line 4-end: contains the specification of a particular type of shape (one per line) The shape formats are specified as: * **Point** - `P: X Y COLOR` * **Circle** - `C: X Y COLOR RADIUS` * **Triangle** - `T: X Y COLOR BASE HEIGHT` * **Rectangle** - `R: X Y COLOR WIDTH HEIGHT` * **Labeled Triangle** - `LT: X Y COLOR BASE HEIGHT LABEL` * **Labeled Rectangle** - `LR: X Y COLOR WIDTH HEIGHT LABEL` Note: * Although a single space is shown between each field of the shape specification, your program must allow for any non-newline whitespace to be present between fields. For example, `P: X Y COLOR` (tab between `X` and `Y`) should be considered as valid. * `LABEL` can contain non-newline whitespace. I.e., anything on the line after the `HEIGHT` is considered to be part of the `Label`. Your `ShapeLoaderApp` must extend `Application` and instantiate a `WinPlotterFX`<sup>1</sup> object, which extends `Stage`. The `ShapeLoaderApp` must implement the following methods: ### Instance methods * **`start()`** &mdash; This method must make use of a `TextInputDialog` or `FileChooser` to obtain the filename from the user. It must read the header information from the file (the first three lines) and create a `WinPlotterFX` object with the appropriate characteristics. * **`readShapes()`** &mdash; Reads all lines after the header information (lines 4 - end) and stores each shape in a `List` of shapes. This method uses the `parseShape()` method to create the appropriate shape. If `parseShape()` throws an exception, no shape is added for that line of the input file. * **`drawShapes()`** &mdash; Draws all of the shapes in the shape list. ### Class methods * **`main()`** &mdash; The main method just calls `launch()`. * **`parseShape()`** &mdash; Accepts a `String` that should contain one line from the input file and returns an instance of the appropriate shape. The line from the input file should represent one of the known shapes in the correct format. If the format does not match one of the specified shapes, an exception must be thrown (to be handled by the `readShapes()` method). * **`stringToColor()`** &mdash; Accepts a `String` that should contain a hex triplet and returns a `Color` instance of the appropriate color. If the argument is not a hex triplet (does not begin with the number symbol, is not seven characters long, or if any of the last six characters are not a valid hexadecimal digit), an `InputMismatchException` must be thrown. ### Handling Exceptions There are a number of situations that could cause your program to throw an exception. For example, if the file is not found, can't be opened, or contains incorrectly formatted data. If the program is unable to correctly read the picture header information (the first three lines) from the file, the program should display an appropriate `Alert` dialog. The program should then end. If an error is encountered while reading a line of the file after the header information, a useful error message should be written to the console and the program should continue on to the next line in the file. You should experiment with a number of input files containing invalid formatting. Without doing this it is unlikely that your code will handle all of the possible ways that things could go wrong. ## <sup>1</sup>Just for Fun You may choose to make use of the `WinPlotterFX` class to do your drawing or implement your own GUI to draw the shapes. ## Lab Deliverables > See your professor's instructions for details on submission guidelines and due dates. >* Dr. Taylor's students: See below >* All other students should refer to Blackboard > >If you have any questions, consult your instructor. ## Acknowledgment This laboratory assignment was developed by [Dr. Chris Taylor](/taylor/).

Tuesday, 14-Jan-2020 02:30:16 CST