Programming Assignment 4
Generating Scalable Vector Graphics
Objectives
To demonstrate an understanding of inheritance, polymorphism, and abstract base classes.
Requirements
You will implement a Java console application that reads drawing commands from a text file and writes Scalable Vector Graphics (SVG) to Standard Output.
The application uses these drawing commands to build a collection of concrete classes: Line, Rectangle, and Circle. These concrete classes derive from an abstract base class, Shape. This collection of shapes is maintained as a private field of an Svg class.
The Svg class maintains the collection of shapes and two fields of type double representing the width and height of the graphic. The class provides a method, addShape, that receives Shape objects and adds them to its private collection. The Svg class also provides a second method, render, that writes SVG elements to a specified output stream.
The Shape class maintains an array of styles, which are stored as a collection of String types obtained from reading two tokens, a Key and a Value, and joining them with the ‘:’ character. The class also defines an abstract method, renderAttributes, that derived classes use to write XML attributes to an output stream.
The Line class contains fields of type double representing two points in Cartesian Space:
The Rectangle class contains fields of type double representing a location and size in Cartesian Space:
The Circle class contains fields of type double representing a center coordinate and radius in Cartesian Space:
When all commands have been read from the text file, the application writes to Standard Output a series of SVG elements based on the commands from the input file. The implementation writes the SVG elements by passing System.out to the render method of an Svg instance.
Note: Be sure to look at the Example section for sample input and output, and be sure to download the sample input files from the Resource section.
The application determines the name of the text file from the command-line arguments. If the user supplies no arguments, or if the user supplies two or more arguments, the application terminates after writing to Standard Error, “Invalid command-line arguments.”.
If any I/O error occurs while reading the input file, the application terminates after writing to Standard Error, “Failed to read input file: XXX”, where XXX is the value returned by Exception.getMessage().
The application ignores case for all comparisons of tokens read from the input file.
Parsing Errors
If a Parsing Error, such as NumberFormatException, occurs while reading the input file, the application terminates after writing to Standard Error “Invalid token: 'XXX'.”, where XXX is the unsupported or invalid token read from the file.
SVG Header
If the first token from the input file is not SVG, the application terminates with a Parsing Error.
The application then reads two more tokens from the input file representing the width and then the height of the graphic, and if these two tokens cannot be parsed as double types, the application terminates with a Parsing Error.
If the SVG token, the width, and the height are valid, the application creates a new instance of the Svg class and initializes it with the specified width and height. Then the application loops over additional tokens of the input file. For each iteration, the application first reads one Command Token, and if this Command Token is not LINE, RECT, or CIRCLE, the application terminates with a Parsing Error.
Line Shapes
If the Command Token is LINE, the application reads four tokens that represent x1, y1, x2, and y2 values that are used to create a new instance of the Line class; if any of these values fail to parse as double types, the application terminates with a Parsing Error.
Rectangle Shapes
If the Command Token is RECT, the application then reads four tokens that represent x, y, width, and height values that are used to create a new instance of the Rectangle class; if any of these values fail to parse as double types, the application terminates with a Parsing Error.
Circle Shapes
If the Command Token is CIRCLE, the application then reads three tokens that represent cx, cy, and r values that are used to create a new instance of the Circle class; if any of these values fail to parse as double types, the application terminates with a Parsing Error.
Styles
After successfully parsing a LINE, RECT, or CIRCLE command, the application enters a Style Loop that reads style information for the class derived from Shape. For each iteration, the application reads a first token, and if this token is END, the shape is added to the Svg instance, the application exits the Style Loop, and it then continues to process new commands from the input file.
If the first token is not END, the application interprets it as a Key, reads a second token and interprets it as a Value, and then passes this Key-Value pair to the Shape class to add as a style. The application continues to read and add styles in this fashion until the END token is encountered.
SVG Output
When all tokens have been read from the input file, the application renders the Svg instance to Standard Output. The Svg class data is formatted as follows.
Between the opening and closing svg tags, the application renders each shape in the order they were defined in the input file. The format of each shape is specific to its type, but each contains a style attribute that is common to all shapes. Arrays of n styles are formatted as follows.
Line shapes are formatted as follows.
Rectangle shapes are formatted as follows.
Circle shapes are formatted as follows.
Examples
Note: The graphic above renders as follows:
Resources
- input.txt
- mystery-one.txt
- mystery-two.txt
- mystery-three.txt
- invalid-one.txt
- invalid-two.txt
- invalid-three.txt
- invalid-four.txt
- Relevant API
Student Performance and Statistics
Analysis on overall student performance and coverage of requirements