Console Drawing Board
Introduction
A drawing board is a kind of desk which can be used for any kind of drawing, writing or sketching on a sheet of paper. This program, console drawing board, is a Java program that provide a console interface and enable you to draw several figures, including rectangles and triangles.Prerequisite
Program Structure
There are 5 classes, ConsoleBoard, ScreenManager, Figure, Rectangle, and Triangle. The entry point (main function) of this program is in ConsoleBoard.java.The class, ConsoleBoard.java, has four attributes:
- BufferReader (br) which is reponsible for taking inputs from the user with readLine function
- String (in) which is the input string
- PrintWriter (pw) which prints program messages
- ScreenManager (sm) which manages and plots the figures of the drawing board
- Drawing: the user can draw a rectangle by specifying the width, height, the center of x, and the center of y or a triangle by providing the three vertices.
- Erasing: the user can erase any drawn figure.
- Centering: the user can center any drawn figure.
- Example: there is a robot thread that trigger key board events to show the exmaple usage of this program.
- Exit: exit this program
sm.printScreen();
while (true) {
pw.printf("Welcome to the drawing board !\n1.draw\n2.erase\n3.center\n4.example\n5.exit : ");
try {
in = br.readLine();
if (in.equals("5"))
break;
} catch (IOException e) {
e.printStackTrace();
continue;
}
switch (in) {
case "1":
draw();
break;
case "2":
erase();
break;
case "3":
center();
break;
case "4":
example();
break;
default:
pw.printf("Wrong entry number : %s\n", in);
break;
}
}
public class ScreenManager {
public ScreenManager() {
pw = new PrintWriter(System.out, true);
db = new int[2500];
fgs = new ArrayList<Figure>();
}
public void draw(int x,int y){
db[x+50*y]++;
}
public void erase(int x,int y){
db[x+50*y]--;
}
public void addFig(Figure fg){
fgs.add(fg);
fg.draw(this);
}
public void delFig(Figure fg){
fgs.remove(fg);
fg.erase(this);
}
public ArrayList<Figure> getFigList(){
return fgs;
}
public void printScreen() {
pw.print('+');
for (int i = 0;i<50;i++)pw.print((char)('-'+65248));
pw.println('+');
for (int i = 0; i < db.length; i++) {
if(i%50==0)pw.print('|');
if(db[i]>0)pw.print((char)('*'+65248));
else pw.print((char)12288);
if ((i+1)%50==0)
pw.println('|');
}
pw.print('+');
for (int i = 0;i<50;i++)pw.print((char)('-'+65248));
pw.println('+');
}
protected ArrayList<Figure> fgs;
protected PrintWriter pw;
protected int[] db;
}
- Drawing
- Erasing
- Centering
- Detail
public abstract class Figure extends Component{
public Figure(){
centerPoint = new Point( 0, 0);
name = "None";
}
public Figure(String n,Point p){
centerPoint = new Point(p);
name = new String(n);
}
abstract public void draw(ScreenManager sm);
abstract public void erase(ScreenManager sm);
abstract public void center(ScreenManager sm);
abstract public void showDetail(PrintWriter pw);
public String getName(){
return name;
}
protected Point centerPoint;
protected String name;
/**
*
*/
private static final long serialVersionUID = 1L;
}