Adnan

Adnan

  • NA
  • 24
  • 2.4k

Draw Canvas line problem

Jul 8 2017 9:12 AM
Hi folks,
I have a created a Canvas line. here is my source code:
 
  1. using System;  
  2.   
  3. namespace DemoCanvas  
  4. {  
  5.     public class Canvas  
  6.     {  
  7.         //char[][] canvasArray;  
  8.         char[,] canvasArray;  
  9.         int w, h;  
  10.         public Canvas(int w, int h)  
  11.         {  
  12.             h += 2;  
  13.             w += 2;  
  14.             this.w = w;  
  15.             this.h = h;  
  16.             canvasArray = new char[h,w];  
  17.             drawLine(0, 0, this.w - 1, 0, '-');  
  18.             drawLine(0, this.h - 1, this.w - 1, this.h - 1, '-');  
  19.             drawLine(0, 1, 0, this.h - 2, '|');  
  20.             drawLine(this.w - 1, 1, this.w - 1, this.h - 2, '|');  
  21.         }  
  22.   
  23.         public void bucketFill(int x, int y, char mchar)  
  24.         {  
  25.             if ((int)canvasArray[y, x] != 0)  
  26.             {  
  27.                 return;  
  28.             }  
  29.   
  30.             if (x > 0 || x < this.h || y > 0 || y < this.w)  
  31.             {  
  32.                 if ((int)canvasArray[y, x] == 0)  
  33.                     canvasArray[y, x] = mchar;  
  34.                 bucketFill(x + 1, y, mchar);  
  35.                 bucketFill(x - 1, y, mchar);  
  36.                 bucketFill(x, y - 1, mchar);  
  37.                 bucketFill(x, y + 1, mchar);  
  38.             }  
  39.         }  
  40.         public void drawLine(int x1, int y1, int x2, int y2, char mchar)  
  41.         {  
  42.             for (int i = y1; i <= y2; i++)  
  43.             {  
  44.                 for (int j = x1; j <= x2; j++)  
  45.                 {  
  46.                     canvasArray[i, j] = mchar;  
  47.                 }  
  48.             }  
  49.         }  
  50.   
  51.         public void drawRectangle(int x1, int y1, int x2, int y2, char mchar)  
  52.         {  
  53.             drawLine(x1, y1, x2, y1, mchar);  
  54.             drawLine(x1, y1, x1, y2, mchar);  
  55.             drawLine(x2, y1, x2, y2, mchar);  
  56.             drawLine(x1, y2, x2, y2, mchar);  
  57.         }  
  58.   
  59.         public void render()  
  60.         {  
  61.             for (int i = 0; i < this.h; i++)  
  62.             {  
  63.                 for (int j = 0; j < this.w; j++)  
  64.                 {  
  65.                     Console.WriteLine(canvasArray[i, j]);  
  66.                 }  
  67.                 //Console.WriteLine();  
  68.             }  
  69.         }  
  70.     }  
  71.   
  72.     class Program  
  73.     {  
  74.         static void Main(string[] args)  
  75.         {  
  76.             string command = "";  
  77.             while (command != "Q")  
  78.             {  
  79.                 Console.WriteLine("Enter Your Command:(E.g C 4 2) ");  
  80.                 command = Console.ReadLine();  
  81.                 draw(command);  
  82.             }  
  83.             Console.WriteLine("Program Exited!");  
  84.         }  
  85.   
  86.         static Canvas canvas;  
  87.         private static void draw(String command)  
  88.         {  
  89.             char ch = command[0];  
  90.             String[] cmd;  
  91.   
  92.             switch (ch)  
  93.             {  
  94.                 case 'C':  
  95.                     cmd = command.Split(' ');  
  96.                     canvas = new Canvas(int.Parse(cmd[1]), int.Parse(cmd[2]));  
  97.                     canvas.render();  
  98.                     break;  
  99.                 case 'L':  
  100.                     cmd = command.Split(' ');  
  101.                     if (canvas == null)  
  102.                     {  
  103.                         Console.Error.WriteLine("Please draw a canvas first");  
  104.                         return;  
  105.                     }  
  106.                     canvas.drawLine(int.Parse(cmd[1]), int.Parse(cmd[2]), int.Parse(cmd[3]), int.Parse(cmd[4]), 'X');  
  107.                     canvas.render();  
  108.                     break;  
  109.                 case 'R':  
  110.                     cmd = command.Split(' ');  
  111.                     if (canvas == null)  
  112.                     {  
  113.                         Console.Error.WriteLine("Please draw a canvas first");  
  114.                         return;  
  115.                     }  
  116.                     canvas.drawRectangle(int.Parse(cmd[1]), int.Parse(cmd[2]), int.Parse(cmd[3]), int.Parse(cmd[4]), 'X');  
  117.                     canvas.render();  
  118.                     break;  
  119.                 case 'B':  
  120.                     cmd = command.Split(' ');  
  121.                     if (canvas == null)  
  122.                     {  
  123.                         Console.Error.WriteLine("Please draw a canvas first");  
  124.                         return;  
  125.                     }  
  126.                     canvas.bucketFill(int.Parse(cmd[1]), int.Parse(cmd[2]), cmd[3][0]);  
  127.                     canvas.render();  
  128.                     break;  
  129.             }  
  130.         }  
  131.     }  
  132. }  
Please copy/paste and run it.

Where 4 is for Width and 2 is for height

I want to display output:
 _ _
|     |
|     |
|     |
|_ _ |

I am waiting for your response.
Thanks in Advance




Answers (1)