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.

Class Diagram

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.

class Svg { private ArrayList<Shape> shapes; private double width; private double height; : void addShape(Shape shape) { ... } void render(PrintStream out) { ... } :

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.

abstract class Shape { private ArrayList<String> styles; : abstract void renderAttributes(PrintStream out); :

The Line class contains fields of type double representing two points in Cartesian Space:

class Line extends Shape { private double x1; private double y1; private double x2; private double y2; :

The Rectangle class contains fields of type double representing a location and size in Cartesian Space:

class Rectangle extends Shape { private double x; private double y; private double width; private double height; :

The Circle class contains fields of type double representing a center coordinate and radius in Cartesian Space:

class Circle extends Shape { private double cx; private double cy; private double r; :

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.”.

$ java ChengJade4 Invalid command-line arguments. $ java ChengJade4 file1.txt file2.txt file3.txt 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().

$ java ChengJade4 . Failed to read input file: . (Access is denied). $ java ChengJade4 missing.txt Failed to read input file: missing.txt (The system cannot find the file specified).

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.

$ cat bad-input.txt SVG 320 200 RECT 10 10 20 bad-height END $ java ChengJade4 bad-input.txt Invalid token: 'bad-height'.

SVG Header

If the first token from the input file is not SVG, the application terminates with a Parsing Error.

$ cat bad-input.txt PNG 50 25 $ java ChengJade4 bad-input.txt Invalid token: 'PNG'.

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.

$ cat bad-input.txt SVG six nine $ java ChengJade4 bad-input.txt Invalid token: 'six'.

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.

$ cat bad-input.txt SVG 320 200 POLYGON 5 $ java ChengJade4 bad-input.txt Invalid token: 'POLYGON'.

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.

$ cat bad-input.txt SVG 320 200 LINE 10 20 bad-x2 60 $ java ChengJade4 bad-input.txt Invalid token: 'bad-x2'.

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.

$ cat bad-input.txt SVG 320 200 RECT 10 20 bad-width 60 $ java ChengJade4 bad-input.txt Invalid token: 'bad-width'.

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.

$ cat bad-input.txt SVG 320 200 CIRCLE 10 20 bad-radius $ java ChengJade4 bad-input.txt Invalid token: 'bad-radius'.

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.

<svg width='WIDTH' height='HEIGHT'> : (shapes) : </svg>

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.

STYLE := Key1:Value1;Key2:Value2;...;Keyn:Valuen;

Line shapes are formatted as follows.

<line x1='X1' y1='Y1' x2='X2' y2='Y2' style='STYLE' />

Rectangle shapes are formatted as follows.

<rect x='X' y='Y' width='WIDTH' height='HEIGHT' style='STYLE' />

Circle shapes are formatted as follows.

<circle cx='CX' cy='CY' r='R' style='STYLE' />

Examples

$ cat input.txt svg 300 300 rect 5 5 290 290 fill #f8f8f8 end line 5 5 295 295 stroke #804040 stroke-width 2 stroke-dasharray 5,10 end line 5 295 295 5 stroke #804040 stroke-width 2 stroke-dasharray 5,10 end rect 5 5 290 290 fill none stroke #c0c0c0 stroke-width 2 end circle 150 150 75 stroke #444 stroke-width 2 fill #844 fill-opacity 0.25 end $ java ChengJade4 input.txt <svg width='300.0' height='300.0'> <rect x='5.0' y='5.0' width='290.0' height='290.0' style='fill:#f8f8f8;'/> <line x1='5.0' y1='5.0' x2='295.0' y2='295.0' style='stroke:#804040;stroke-width:2;stroke-dasharray:5,10;'/> <line x1='5.0' y1='295.0' x2='295.0' y2='5.0' style='stroke:#804040;stroke-width:2;stroke-dasharray:5,10;'/> <rect x='5.0' y='5.0' width='290.0' height='290.0' style='fill:none;stroke:#c0c0c0;stroke-width:2;'/> <circle cx='150.0' cy='150.0' r='75.0' style='stroke:#444;stroke-width:2;fill:#844;fill-opacity:0.25;'/> </svg>

Note: The graphic above renders as follows:

Resources