/// <summary> /// This is the method where user can type "run","clear" and "reset " command to execute, clear and reset commandline simulteneouly. /// </summary> /// <param name="sender">Reference to the control object</param> /// <param name="e">event data</param> private void executeInput_TextChanged(object sender, EventArgs e) { if (executeInput.Text.ToLower().Trim() == "run") { g = ShapeOutput.CreateGraphics(); string command = ProgramInput.Text.ToLower(); string[] commandline = command.Split(new String[] { "\n" }, StringSplitOptions.RemoveEmptyEntries); for (int k = 0; k < commandline.Length; k++) { string[] cmd = commandline[k].Split(' '); if (cmd[0].Equals("moveto") == true) { ShapeOutput.Refresh(); string[] param = cmd[1].Split(','); if (param.Length != 2) { MessageBox.Show("Please Input Valid Parameter!!!"); } else { Int32.TryParse(param[0], out x); Int32.TryParse(param[1], out y); MoveTo(x, y); } } else if (cmd[0].Equals("drawto") == true) { string[] param = cmd[1].Split(','); int x = 0, y = 0; if (param.Length != 2) { MessageBox.Show("Please Input Valid Parameter!!!"); } else { Int32.TryParse(param[0], out x); Int32.TryParse(param[1], out y); DrawTo(x, y); } } else if (cmd[0].Equals("rectangle") == true) { if (cmd.Length < 2) { MessageBox.Show("Please Input Valid Parameter!!!"); } else { string[] param = cmd[1].Split(','); if (param.Length < 2) { MessageBox.Show("Please Input Valid Parameter!!!"); } else { Int32.TryParse(param[0], out width); Int32.TryParse(param[1], out height); IShapes rectangle = factory.getShape("rectangle"); Rectangle r = new Rectangle(); r.set(Color.Black, x, y, width, height); r.Draw(g); } } } else if (cmd[0].Equals("circle") == true) { if (cmd.Length != 2) { MessageBox.Show("Please Input Valid Parameter!!!"); } else { if (cmd[1].Equals("radius") == true) { IShapes circle = factory.getShape("circle"); Circle c = new Circle(); c.set(Color.AliceBlue, x, y, radius); c.Draw(g); } else { Int32.TryParse(cmd[1], out radius); IShapes circle = factory.getShape("circle"); Circle c = new Circle(); c.set(Color.AliceBlue, x, y, radius); c.Draw(g); } } } else if (cmd[0].Equals("triangle") == true) { string[] param = cmd[1].Split(','); if (param.Length != 2) { MessageBox.Show("Please Input Valid Parameter!!!"); } else { Int32.TryParse(param[0], out width); Int32.TryParse(param[1], out height); IShapes triangle = factory.getShape("triangle"); Triangle r = new Triangle(); r.set(Color.AliceBlue, x, y, width, height); r.Draw(g); } } else if (cmd[0].Equals("filltriangle") == true) { string[] param = cmd[1].Split(','); if (param.Length != 2) { MessageBox.Show("Please Input Valid Parameter!!!"); } else { Int32.TryParse(param[0], out width); Int32.TryParse(param[1], out height); IShapes circle = factory.getShape("filltriangle"); Triangle r = new Triangle(); r.set(Color.AliceBlue, x, y, width, height); r.draw(g); } } else if (cmd[0].Equals("pen") == true) { if (cmd[1] == "red")//if red then color changes to red { c = Color.Red; } else if (cmd[1] == "blue")//if blue then color changes to blue { c = Color.Blue; } else if (cmd[1] == "yellow")//if yellow then color changes to yellow { c = Color.Yellow; } else { c = Color.AliceBlue;//default color } } else if (cmd[0].Equals("fillrectangle") == true) { if (cmd.Length < 2) { MessageBox.Show("Please Input Valid Parameter!!!"); } else { string[] param = cmd[1].Split(','); if (param.Length < 2) { MessageBox.Show("Please Input Valid Parameter!!!"); } else { Int32.TryParse(param[0], out width); Int32.TryParse(param[1], out height); IShapes rectangle = factory.getShape("fillrectangle"); Rectangle r = new Rectangle(); r.set(Color.Black, x, y, width, height); r.draw(g); } } } else if (cmd[0].Equals("fillcircle") == true) { if (cmd.Length != 2) { MessageBox.Show("Please Input Valid Parameter!!!"); } else { if (cmd[1].Equals("radius") == true) { IShapes circle = factory.getShape("fillcircle"); Circle c = new Circle(); c.set(Color.AliceBlue, x, y, radius); c.draw(g); } else { Int32.TryParse(cmd[1], out radius); IShapes circle = factory.getShape("fillcircle"); Circle c = new Circle(); c.set(Color.AliceBlue, x, y, radius); c.draw(g); } } } else if (!cmd[0].Equals(null)) { int errorLine = k + 1; MessageBox.Show("Invalid command recognised on line " + errorLine, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } else if (executeInput.Text.ToLower().Trim() == "clear") { ShapeOutput.Invalidate(); } else if (executeInput.Text.ToLower().Trim() == "reset") { ProgramInput.Clear(); MultilineProgramInput.Clear(); ShapeOutput.Invalidate(); size1 = 0; size2 = 0; xlabel.Text = size1.ToString(); ylabel.Text = size2.ToString(); } }
public Form1() { InitializeComponent(); g = ShapeOutput.CreateGraphics(); }