In this assignment, you are to develop a basic text-based drawing application. A sample drawing is
shown below.
It has
_ Borders which form a rectangle around a drawing area of 20 by 30
_ Two lines (the ground and the lamp post)
_ One circle (the lamp)
_ Two rectangles (the house walls and the door)
_ One triangle (the roof)
_ A string of text (“FOR SALE” displayed vertically)
Windows
Each drawing is a window on which shapes can be drawn. A window has, among other things,
_ the height
_ the width
_ the border character that is used to draw the borders
The window’s height and width represent the drawing area, excluding the borders. That is, a window of
height 20 and width 30 has 20 x 30 = 600 cells each of which contains a blank or a non-blank character.
2
Shapes
There are five kinds of shapes that your programs can draw:
_ Lines
_ Rectangles
_ Triangles
_ Circles
_ Texts (regarded as a kind of shape)
Each shape has
_ a base point (that will be further explained as we describe the specific shapes), and
_ a display character i.e. the character that is used to display the shape
Lines
Consider, as an example, the line below
Suppose the top point of the line is at position 10, 15. And suppose we take it to be the base point of the
line. Then the line can be specified as follows:
_ The row position of its base point, which is 10
_ The column position of its base point, which is 15
_ Its length, which is 4
_ Its row increment is 1
_ Its column increment is 1
_ And its drawing character, which is ‘*’
The row increment 1 and column 1 signifies that the line goes down and goes to the right from the base
point. For our drawing board, row increment and column increment can only take values -1, 0, or 1. In
other words, a line can be horizontal, vertical or can incline at an angle of 45 degrees.
3
Rectangles
Consider, as an example, the rectangle below
Suppose the top-left point is at position 10, 15. This point will be taken to be the base point. The line
can be specified as follows:
_ The row position of its base point, which is 10
_ The column position of its base point, which is 15
_ Its height, which is 3
_ Its width is 6
_ And its drawing character, which is ‘*’
Triangles
Our drawing board will draw only isosceles triangle. Consider, as an example, the triangle below
Suppose the left-most point is at position 10, 15. This point will be taken to be the base point. The
triangle can be specified as follows:
_ The row position of its base point, which is 10
_ The column position of its base point, which is 15
_ Its height, which is 3 (the perpendicular distance, in terms of number of cells, from the base point
to the opposite side)
_ Its row increment of the height, which is 0
_ Its column increment of the height, which is 1
_ And its drawing character, which is ‘*’
The row increment 0 and column 1 signifies that the height goes horizontally to the right from the base
point. For our drawing board, from its base point, the height can be in one of four directions: to the right
(row increment = 0, column increment = 1), or to the left, or up or down.
4
Circles
Consider the circle displayed below, which is a circle of radius 2.
The base point of a circle is its center. A circle is specified by
_ The row position of its base point (its center)
_ The column position of its base point (its center)
_ Its radius
_ And its drawing character, which is ‘+’
For our drawing boards, we draw a circle by plotting 20 points on its circumference. When a circle
is of small size, some of the 20 points overlap. Obviously, the display is often only a rather crude
approximation of a circle. (Hint: In reference to a coordinate system whose origin is the center O of the
circle, a point M on the circle has coordinates x = R cos(_) and y = R sin(_), where R is the radius
and _ is the angle between the x-axis to OM).
“Text Shapes”
Consider, as an example, the text display below
Suppose letter ‘H’ is at position 10, 15. This is the base point. This “text shape” can be specified as
follows:
_ The row position of its base point, which is 10
_ The column position of its base point, which is 15
_ Its text itself, which is “HELLO”
_ Its row increment is 1
_ Its column increment is 1
The row increment 1 and column 1 signifies that the line goes down and goes to the right from the base
point. For our drawing board, as for the lines, row increment and column increment of a text shape can
take values -1, 0, or 1.
5
Task 1
(a) Design and implement classes to represent the drawing window and the shapes.
(b) Write a program, called T1Main.java to generate and display a drawing on the screen.
The T1Main class is similar to the HouseForSale class shown below.
import java.util.*;
import java.io.*;
public class HouseForSale
{
public static void main(String [] args) throws Exception
{
{
// Create a window
Window w = new Window(20, 30, '*');
// Draw the ground
Line ground = new Line(19, 1, 29, 0, 1,'#');
w.addShape(ground);
// Draw the post
Line post = new Line(12, 5, 6, 1, 0, '#');
w.addShape(post);
// Draw the light
Circle light = new Circle(10, 5, 2, '+');
w.addShape(light);
// Draw the house
Rectangle house = new Rectangle(8, 16, 11, 10, '=');
w.addShape(house);
// Draw the door
Rectangle door = new Rectangle(11, 19, 8, 4, '=');
w.addShape(door);
// Draw the roof
Triangle roof = new Triangle(2, 21, 6, 1, 0, '*');
w.addShape(roof);
// Display text message
Text msg = new Text(2,10, "FOR SALE", 1, 0);
w.addShape(msg);
w.display();
}
}
}
It is required that the classes you develop must allow the HouseForSale program to run without any
changes.
6
The Window class must have
_ The constructor
Window(int numberOfRows, int numberOfColumns, char borderCharacter)
_ The method to add a shape
void addShape(Shape shape)
_ The method to remove a shape
void removeShape(Shape shape)
The shape that is most recently added is the one to be removed. This method can be called repeatedly
to remove several shapes.
This method will be useful in Task 4 in which we create a drawing interactively via a menu-driven
program.
_ The method to display the drawing on the screen
void display()
Row value increases from top to bottom, and column value increases from left to right. The top-left cell
has row = 1 and column = 1.
The Line class must have the constructor
Line(int rowBase, int colBase, int length, int rowIncrement,
int colIncrement, char drawingCharacter)
The Rectangle class must have the constructor
Rectangle(int rowBase, int colBase, int height, int width,
char drawingCharacter)
The Triangle class must have the constructor
Triangle(int rowBase, int colBase, int height, int rowIncrement,
int colIncrement, char drawingCharacter)
The Circle class must have the constructor
Circle(int rowBase, int colBase, int radius, int rowIncrement,
char drawingCharacter)
The Text class must have the constructor
Text(int rowBase, int colBase, String text, int rowIncrement,
int colIncrement)
7
In addition, every shape class (Line, Rectangle, Triangle, Circle, Text) must also have the method to
draw itself on a Window
void draw(Window window)
Task 2
(a) To the class Window, add and implement the following method
void writeToFile(String fileName)
The method saves the drawing window in the specified text file.
(b) Write a program, called T2Main.java, to generate a drawing, display it on the screen and save it
to a text file called T2Drawing.txt.
The format of the output text file must conform to the specification illustrated by the example below,
minus the comments.
20 30 // number of rows and columns of the drawing window
* // character for border
. // a dot to signal the end of the ``record''
line // the shape is a line
19 1 29 0 1 // row & column positions of base, length, row increment, column increment
# // display character
.
line
12 5 6 1 0
#
.
circle
10 5 2 // row position of base, column position of base Base, radius
+ // display character
.
rectangle
8 16 11 10 // row position of base, column position of base Base, height, width
= // display character
.
rectangle
11 19 8 4
=
.
triangle
2 21 6 1 0 // row & column positions of base, height, row increment, column increment
*
.
text
2 10 // row & column positions of base
FOR SALE // the text itself
1 0 // row increment, column increment
.
Task 3
(a) To the class Window, add and implement the following static method (class method)
Window readFile(String fileName)
The method reads the text file, constructs and returns the Window object representing the drawing
specified in the text file.
The input text file for this method has the same format as described in Task 2.
(b) Write a program, call it T3Main.java, to read the description of a drawing from a text file and
display the drawing on the screen.
Task 4
Write a menu-driven program, called T4Main.java, which enables the user to create and save drawings.
The program creates a window of size 20 by 20, and informs the user with the message
A window of size 20 by 20 has been created for you.
You can repeatedly draw various shapes.
You can remove the shapes (with the Undo option).
You can also save the specification of the drawing.
The program then displays the menu with the options shown in method below:
========
Options:
========
D: Display the Drawing
L: Draw a Line
R: Draw a Rectangle
T: Draw a Triangle
C: Draw a Circle
S: Insert a String of text
U: Undo (remove the shape most recently added)
W: Write to File
Q: Quit
Please select an option:
Option D displays the drawing on the screen.
For options L, R, T, C and S, the program prompts the user for the details of the shapes, creates those
shapes and adds them to the drawing.
Option U removes the shape that is most recently added among the shapes currently in the drawing
window object.
For option W, the program asks for the name of the file and saves the specification of the drawing to that
file. The format of the output file is as described in Task 2.