// Graphics g;

        /// <summary>
        /// all logic to run command in application
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_run_Click(object sender, EventArgs e)
        {
            Regex regexDrRect   = new Regex(@"drawto (.*[\d])([,])(.*[\d]) rectangle (.*[\d])([,])(.*[\d])");
            Regex regexDrCircle = new Regex(@"drawto (.*[\d])([,])(.*[\d]) circle (.*[\d])");
            Regex regexDrTri    = new Regex(@"drawto (.*[\d])([,])(.*[\d]) triangle (.*[\d])([,])(.*[\d])([,])(.*[\d])");


            Regex regexClear = new Regex(@"clear");
            Regex regexReset = new Regex(@"reset");
            Regex regexMT    = new Regex(@"moveto (.*[\d])([,])(.*[\d])");

            Regex regexR = new Regex(@"rectangle (.*[\d])([,])(.*[\d])");
            Regex regexC = new Regex(@"circle (.*[\d])");
            Regex regexT = new Regex(@"triangle (.*[\d])([,])(.*[\d])([,])(.*[\d])");



            Match matchDrRect   = regexDrRect.Match(txt_cmd.Text.ToLower());
            Match matchDrCircle = regexDrCircle.Match(txt_cmd.Text.ToLower());
            Match matchDrTri    = regexDrTri.Match(txt_cmd.Text.ToLower());

            Match matchClear = regexClear.Match(txt_cmd.Text.ToLower());
            Match matchReset = regexReset.Match(txt_cmd.Text.ToLower());
            Match matchMT    = regexMT.Match(txt_cmd.Text.ToLower());

            Match matchR = regexR.Match(txt_cmd.Text.ToLower());
            Match matchC = regexC.Match(txt_cmd.Text.ToLower());
            Match matchT = regexT.Match(txt_cmd.Text.ToLower());



            if (matchDrRect.Success || matchDrCircle.Success || matchDrTri.Success || matchClear.Success ||
                matchReset.Success || matchMT.Success || matchR.Success || matchC.Success || matchT.Success)
            {
                //----------------RECTANGLE WITH DrawTo-----------------------//
                if (matchDrRect.Success)
                {
                    try
                    {
                        g      = Pnl_Draw.CreateGraphics();
                        _size1 = int.Parse(matchDrRect.Groups[1].Value);
                        _size2 = int.Parse(matchDrRect.Groups[3].Value);
                        _size3 = int.Parse(matchDrRect.Groups[4].Value);
                        _size4 = int.Parse(matchDrRect.Groups[6].Value);



                        ShapeFactory shapeFactory = new ShapeFactory();
                        Shape        c            = shapeFactory.GetShape("rectangle");

                        c.set(texturestyle, bb, paintcolor, _size1, _size2, _size3, _size4);
                        c.Draw(g);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }

                //----------------RECTANGLE-----------------------//


                else if (matchR.Success)
                {
                    try
                    {
                        g      = Pnl_Draw.CreateGraphics();
                        _size1 = int.Parse(lbl_StartPosX.Text);
                        _size2 = int.Parse(lbl_StartPosY.Text);
                        _size3 = int.Parse(matchR.Groups[1].Value);
                        _size4 = int.Parse(matchR.Groups[3].Value);

                        ShapeFactory shapeFactory = new ShapeFactory();
                        Shape        c            = shapeFactory.GetShape("rectangle");
                        c.set(texturestyle, bb, paintcolor, _size1, _size2, _size3, _size4);

                        c.Draw(g);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("Error! Parameter should be in this form: \"rectangle width, height\"");
                    }
                }

                //----------------CIRCLE-----------------------//
                else if (matchC.Success)
                {
                    try
                    {
                        g      = Pnl_Draw.CreateGraphics();
                        _size1 = int.Parse(lbl_StartPosX.Text);
                        _size2 = int.Parse(lbl_StartPosY.Text);
                        _size3 = int.Parse(matchC.Groups[1].Value);


                        ShapeFactory shapeFactory = new ShapeFactory();
                        Shape        c            = shapeFactory.GetShape("circle");
                        c.set(texturestyle, bb, paintcolor, _size1, _size2, _size3 * 2, _size3 * 2);
                        //c.draw(set);
                        c.Draw(g);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("Error! Parameter should be in this form: \"circle radius\"");
                    }
                }

                // ----------------TRIANGLE WITH DrawTo---------------------- -//
                else if (matchDrTri.Success)
                {
                    try
                    {
                        g      = Pnl_Draw.CreateGraphics();
                        _size1 = int.Parse(matchDrTri.Groups[1].Value);
                        _size2 = int.Parse(matchDrTri.Groups[3].Value);

                        _size3 = int.Parse(matchDrTri.Groups[4].Value);
                        _size4 = int.Parse(matchDrTri.Groups[6].Value);
                        _size5 = int.Parse(matchDrTri.Groups[8].Value);


                        xi1 = _size1;
                        yi1 = _size2;
                        xi2 = Math.Abs(_size3);
                        yi2 = _size2;

                        xii1 = _size1;
                        yii1 = _size2;
                        xii2 = _size1;
                        yii2 = Math.Abs(_size4);

                        xiii1 = Math.Abs(_size3);
                        yiii1 = _size2;
                        xiii2 = _size1;
                        yiii2 = Math.Abs(_size4);

                        ShapeFactory shapeFactory = new ShapeFactory();
                        Shape        c            = shapeFactory.GetShape("triangle");
                        c.set(texturestyle, bb, paintcolor, xi1, yi1, xi2, yi2, xii1, yii1, xii2, yii2, xiii1, yiii1, xiii2, yiii2);
                        //===============================
                        c.Draw(g);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }

                // ----------------TRIANGLE---------------------- -//

                else if (matchT.Success)
                {
                    try
                    {
                        g      = Pnl_Draw.CreateGraphics();
                        _size1 = int.Parse(lbl_StartPosX.Text);
                        _size2 = int.Parse(lbl_StartPosY.Text);

                        _size3 = int.Parse(matchT.Groups[1].Value);
                        _size4 = int.Parse(matchT.Groups[3].Value);
                        _size5 = int.Parse(matchT.Groups[5].Value);


                        xi1 = _size1;
                        yi1 = _size2;
                        xi2 = Math.Abs(_size3);
                        yi2 = _size2;

                        xii1 = _size1;
                        yii1 = _size2;
                        xii2 = _size1;
                        yii2 = Math.Abs(_size4);

                        xiii1 = Math.Abs(_size3);
                        yiii1 = _size2;
                        xiii2 = _size1;
                        yiii2 = Math.Abs(_size4);

                        ShapeFactory shapeFactory = new ShapeFactory();
                        Shape        c            = shapeFactory.GetShape("triangle"); //new rectangles();
                        c.set(texturestyle, bb, paintcolor, xi1, yi1, xi2, yi2, xii1, yii1, xii2, yii2, xiii1, yiii1, xiii2, yiii2);
                        c.Draw(g);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("Error! Parameter should be in this form\"triangle side, side, side\"");
                    }
                }

                // ----------------CLEAR------------------------//

                else if (matchClear.Success)
                {
                    Pnl_Draw.Refresh();
                    this.Pnl_Draw.BackgroundImage = null;
                }


                // ----------------RESET------------------------//
                else if (matchReset.Success)
                {
                    _size1             = 0;
                    _size2             = 0;
                    lbl_StartPosX.Text = _size1.ToString();
                    lbl_StartPosY.Text = _size2.ToString();
                }

                // ----------------MOVETO------------------------//

                else if (matchMT.Success)
                {
                    try
                    {
                        _size1 = int.Parse(matchMT.Groups[1].Value);
                        _size2 = int.Parse(matchMT.Groups[3].Value);

                        lbl_StartPosX.Text = _size1.ToString();
                        lbl_StartPosY.Text = _size2.ToString();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }
            }
            else
            {
                MessageBox.Show("Invalid Syntax!!");
            }
        }
Exemple #2
0
        /// <summary>
        /// a methos that belongs to the menustrip which contains the dropdown
        /// run. when pressing the command in the text editor gets executed
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void runToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //variable declarations
            String command = textEditorControl1.Text;

            string[] syntax = command.Split(' ');

            //check if the first string in the editor is draw
            if (syntax[0].Equals("draw"))
            {
                //exception handling
                try
                {
                    //use of Split function
                    param = syntax[2].Split(',');

                    //check if the second string in the editor is shape name i.e. rectangle
                    if (syntax[1].Equals("rectangle"))
                    {
                        //if there is problem with the parameter the message is shown
                        if (checkParam(param, "rectangle") == false)
                        {
                            MessageBox.Show("Parameters Not Enough");
                        }

                        if (checkParamValue(param) == true)
                        {
                            MessageBox.Show("Parameters Not Valid");
                        }

                        try
                        {
                            //convert the splitted parameters to int from string
                            int p1 = Convert.ToInt32(param[0]);
                            int p2 = Convert.ToInt32(param[1]);
                            int p3 = Convert.ToInt32(param[2]);
                            int p4 = Convert.ToInt32(param[3]);

                            //shapefactory class object
                            ShapeFactory shapeFactory = new ShapeFactory();
                            Shape        shape1       = shapeFactory.GetShape("rectangle");
                            //call the draw method in getshape method
                            shape1.draw(p1, p2, p3, p4, 0, 0, 0, 0, 0, 0);
                        }
                        catch (Exception)
                        {
                        }
                    }

                    //checks if the first word in the texteditor is circle
                    else if (syntax[1].Equals("circle"))
                    {
                        if (checkParam(param, "circle") == false)
                        {
                            MessageBox.Show("Parameters Not Enough");
                        }

                        if (checkParamValue(param) == true)
                        {
                            MessageBox.Show("Parameters Not Valid");
                        }

                        try
                        {
                            int p1 = Convert.ToInt32(param[0]);
                            int p2 = Convert.ToInt32(param[1]);
                            int p3 = Convert.ToInt32(param[2]);
                            int p4 = Convert.ToInt32(param[3]);

                            // MessageBox.Show("Draw rectangle with parameter" + parameter1 + "and" + parameter2);

                            ShapeFactory shapeFactory = new ShapeFactory();
                            Shape        shape2       = shapeFactory.GetShape("circle");
                            shape2.draw(p1, p2, p3, p4, 0, 0, 0, 0, 0, 0);
                        }
                        catch (Exception)
                        {
                        }
                    }

                    else if (syntax[1].Equals("triangle"))
                    {
                        if (checkParam(param, "triangle") == false)
                        {
                            MessageBox.Show("Parameters Not Enough");
                        }

                        if (checkParamValue(param) == true)
                        {
                            MessageBox.Show("Parameters Not Valid");
                        }

                        try
                        {
                            int p1 = Convert.ToInt32(param[0]);
                            int p2 = Convert.ToInt32(param[1]);
                            int p3 = Convert.ToInt32(param[2]);
                            int p4 = Convert.ToInt32(param[3]);
                            int p5 = Convert.ToInt32(param[4]);
                            int p6 = Convert.ToInt32(param[5]);

                            // MessageBox.Show("Draw rectangle with parameter" + parameter1 + "and" + parameter2);

                            ShapeFactory shapeFactory = new ShapeFactory();
                            Shape        shape3       = shapeFactory.GetShape("triangle");
                            shape3.draw(p1, p2, p3, p4, p5, p6, 0, 0, 0, 0);
                        }
                        catch (Exception)
                        {
                        }
                    }

                    else if (syntax[1].Equals("polygon"))
                    {
                        if (checkParam(param, "polygon") == false)
                        {
                            MessageBox.Show("Parameters Not Enough");
                        }

                        if (checkParamValue(param) == true)
                        {
                            MessageBox.Show("Parameters Not Valid");
                        }

                        try
                        {
                            int p1  = Convert.ToInt32(param[0]);
                            int p2  = Convert.ToInt32(param[1]);
                            int p3  = Convert.ToInt32(param[2]);
                            int p4  = Convert.ToInt32(param[3]);
                            int p5  = Convert.ToInt32(param[4]);
                            int p6  = Convert.ToInt32(param[5]);
                            int p7  = Convert.ToInt32(param[6]);
                            int p8  = Convert.ToInt32(param[7]);
                            int p9  = Convert.ToInt32(param[8]);
                            int p10 = Convert.ToInt32(param[9]);

                            // MessageBox.Show("Draw rectangle with parameter" + parameter1 + "and" + parameter2);

                            ShapeFactory shapeFactory = new ShapeFactory();
                            Shape        shape7       = shapeFactory.GetShape("polygon");
                            shape7.draw(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10);
                        }
                        catch (Exception)
                        {
                        }
                    }


                    else
                    {
                        MessageBox.Show("Invalid Graphics Command!");
                    }
                }
                catch (Exception)
                {
                    MessageBox.Show("Invalid Draw Command Executed");
                }
            }



            if (syntax[0].Equals("repeat"))
            {
                try
                {
                    repeatValue = Convert.ToInt32(syntax[1]);
                }
                catch (Exception)
                {
                    MessageBox.Show("Loop Value Incorrect");
                }
                try
                {
                    if (syntax[2].Equals("rectangle"))
                    {
                        string operatorValue = syntax[3];

                        if (operatorValue != "+" && operatorValue != "-")
                        {
                            MessageBox.Show("Operator Sign Incorrect");
                        }

                        try
                        {
                            int increaseValue = Convert.ToInt32(syntax[4]);

                            ShapeFactory shapeFactory = new ShapeFactory();
                            Shape        shape4       = shapeFactory.GetShape("rectangle");
                            shape4.repeatParam(repeatValue, operatorValue, increaseValue);
                        }
                        catch (Exception)
                        {
                            MessageBox.Show("Integer Value Incorrect");
                        }
                    }



                    else if (syntax[2].Equals("circle"))
                    {
                        string operatorValue = syntax[3];

                        if (operatorValue != "+" && operatorValue != "-")
                        {
                            MessageBox.Show("Operator Sign Incorrect");
                        }

                        try
                        {
                            int increaseValue = Convert.ToInt32(syntax[4]);

                            ShapeFactory shapeFactory = new ShapeFactory();
                            Shape        shape5       = shapeFactory.GetShape("circle");
                            shape5.repeatParam(repeatValue, operatorValue, increaseValue);
                        }
                        catch (Exception)
                        {
                            MessageBox.Show("Integer Value Incorrect");
                        }
                    }



                    else if (syntax[2].Equals("triangle"))
                    {
                        string operatorValue = syntax[3];

                        if (operatorValue != "+" && operatorValue != "-")
                        {
                            MessageBox.Show("Operator Sign Incorrect");
                        }

                        try
                        {
                            int increaseValue = Convert.ToInt32(syntax[4]);

                            ShapeFactory shapeFactory = new ShapeFactory();
                            Shape        shape6       = shapeFactory.GetShape("triangle");
                            shape6.repeatParam(repeatValue, operatorValue, increaseValue);
                        }
                        catch (Exception)
                        {
                            MessageBox.Show("Integer Value Incorrect");
                        }
                    }

                    else if (syntax[2].Equals("polygon"))
                    {
                        /*for(int i = 0; i < syntax.Length; i++)
                         * {
                         *  MessageBox.Show(syntax[i]);
                         * }*/


                        string operatorValue = syntax[3];

                        if (operatorValue != "+" && operatorValue != "-")
                        {
                            MessageBox.Show("Operator Sign Incorrect");
                        }

                        try
                        {
                            int increaseValue = Convert.ToInt32(syntax[4]);

                            ShapeFactory shapeFactory = new ShapeFactory();
                            Shape        shape77      = shapeFactory.GetShape("polygon");
                            shape77.repeatParam(repeatValue, operatorValue, increaseValue);
                        }
                        catch (Exception)
                        {
                            MessageBox.Show("Integer Value Incorrect");
                        }
                    }



                    else
                    {
                        MessageBox.Show("Invalid Repeat Command!");
                    }
                }
                catch (Exception)
                {
                    MessageBox.Show("Invalid Repeat Command Executed");
                }
            }


            //check if the first string in the texteditor is var
            if (syntax[0].Equals("var"))
            {
                string[] param;

                int counterValue = Convert.ToInt32(syntax[3]);

                string shapeValue = syntax[5];

                int counterCheckValue = Convert.ToInt32(syntax[10]);

                if (counterValue != counterCheckValue)
                {
                    MessageBox.Show("Counter value isnot equal to: " + counterCheckValue);
                }

                if (shapeValue == "rectangle")
                {
                    param = syntax[6].Split(',');

                    int a = Convert.ToInt32(param[0]);
                    int b = Convert.ToInt32(param[1]);
                    int c = Convert.ToInt32(param[2]);
                    int d = Convert.ToInt32(param[3]);

                    ShapeFactory shapeFactory = new ShapeFactory();
                    Shape        shape11      = shapeFactory.GetShape("rectangle");
                    shape11.ifParam(a, b, c, d, 0, 0, counterValue);
                }

                else if (shapeValue == "circle")
                {
                    param = syntax[6].Split(',');

                    int a = Convert.ToInt32(param[0]);
                    int b = Convert.ToInt32(param[1]);
                    int c = Convert.ToInt32(param[2]);
                    int d = Convert.ToInt32(param[3]);

                    ShapeFactory shapeFactory = new ShapeFactory();
                    Shape        shape12      = shapeFactory.GetShape("circle");
                    shape12.ifParam(a, b, c, d, 0, 0, counterValue);
                }

                else if (shapeValue == "triangle")
                {
                    param = syntax[6].Split(',');
                    int a  = Convert.ToInt32(param[0]);
                    int b  = Convert.ToInt32(param[1]);
                    int c  = Convert.ToInt32(param[2]);
                    int d  = Convert.ToInt32(param[3]);
                    int ce = Convert.ToInt32(param[4]);
                    int f  = Convert.ToInt32(param[5]);

                    ShapeFactory shapeFactory = new ShapeFactory();
                    Shape        shape13      = shapeFactory.GetShape("triangle");
                    shape13.ifParam(a, b, c, d, ce, f, counterValue);
                }


                /*for(int i = 0; i < syntax.Length; i++)
                 *  {
                 *      MessageBox.Show(syntax[i]);
                 *  }*/
            }
        }