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:
  1. BufferReader (br) which is reponsible for taking inputs from the user with readLine function
  2. String (in) which is the input string
  3. PrintWriter (pw) which prints program messages
  4. ScreenManager (sm) which manages and plots the figures of the drawing board
ConsoleBoard.java starts from running an infinite loop that waits for user input. There are 5 functions:
  1. 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.
  2. Erasing: the user can erase any drawn figure.
  3. Centering: the user can center any drawn figure.
  4. Example: there is a robot thread that trigger key board events to show the exmaple usage of this program.
  5. 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;
			}
		}
ScreenManager keeps the information of the drawn Figure in an ArrayList fgs on the drawing board and the information of the grid in an array db with the size of 50 * 50.
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;
}
Figure.java is the parent class of different kind of figures, including Rectangle and Triangle. It defines four abstract functions:
  1. Drawing
  2. Erasing
  3. Centering
  4. 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;
}

Demonstration

Conclusion

This simple console board drawing program can provide a basic template for more sophisticated console plotting program.

History

Reference

  1. Console Drawing Board