Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Programming Assignment 4 Generating Scalable Vector Graphics To demonstrate an understanding of inheritance, polymorphism, and abstract base classes. You will implement a Java console application

Programming Assignment 4

Generating Scalable Vector Graphics

To demonstrate an understanding of inheritance, polymorphism, and abstract base classes.

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.

class Svg { private ArrayList 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 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 Input Package for sample input and output, and be sure to download the sample input files associated with the project.

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 YiZhu4 Invalid command-line arguments. $ java YiZhu4 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 YiZhu4 . Failed to read input file: . (Access is denied). $ java YiZhu4 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 YiZhu4 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 YiZhu4 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 YiZhu4 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 YiZhu4 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 YiZhu4 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 YiZhu4 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 YiZhu4 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.

: (shapes) :

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.

Rectangle shapes are formatted as follows.

Circle shapes are formatted as follows.

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 YiZhu4 input.txt

Note: The graphic above renders as follows:

Resources

Relevant API o ArrayList

o File o IOException o PrintStream o Scanner

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Transactions On Large Scale Data And Knowledge Centered Systems X Special Issue On Database And Expert Systems Applications Lncs 8220

Authors: Abdelkader Hameurlain ,Josef Kung ,Roland Wagner ,Stephen W. Liddle ,Klaus-Dieter Schewe ,Xiaofang Zhou

2013th Edition

3642412203, 978-3642412202

More Books

Students also viewed these Databases questions