示例#1
0
 /// <summary>
 /// Initializing using constructors
 /// </summary>
 /// <param name="g"></param>
 public Drawing(Graphics g)
 {
     this.g       = g;
     v            = new Variable();
     m            = new methods();
     commandCheck = new commandCheck();
     xposition    = yposition = 0;
     p            = new Pen(Color.Black, 1);
     Brush        = new SolidBrush(Color.Black);
 }
示例#2
0
        /// <summary>
        /// To Check the user entered commands
        /// </summary>
        /// <param name="split"></param>
        /// <param name="newDrawing"></param>
        /// <param name="x"></param>
        public void CommandsEntered(String[] split, Drawing newDrawing, int n)
        {
            commandCheck commandCheck = new commandCheck();

            try
            {
                String[] method = split[0].Split('(', ')');


                //Checking entered commands and having their outputs
                if (split[0].Equals("moveto"))
                {
                    String[] parameters = split[1].Split(',');
                    int      x          = 0;
                    int      y          = 0;
                    bool     parameter  = false;

                    try
                    {
                        if (!int.TryParse(parameters[0], out x))
                        {
                            if (!newDrawing.v.exisitingVariable(parameters[0]))
                            {
                                parameter = true;
                            }
                            else
                            {
                                x = newDrawing.v.pullData(parameters[0]);
                            }
                            if (parameter)
                            {
                                newDrawing.commandCheck.Parameterscheck(false, parameters[0], n, newDrawing, numberOfLine);
                                numberOfLine = numberOfLine + 20;
                            }
                        }
                        if (!int.TryParse(parameters[1], out y))
                        {
                            if (!newDrawing.v.exisitingVariable(parameters[1]))
                            {
                                parameter = true;
                            }
                            else
                            {
                                y = newDrawing.v.pullData(parameters[1]);
                            }
                            if (parameter)
                            {
                                newDrawing.commandCheck.Parameterscheck(false, parameters[1], n, newDrawing, numberOfLine);
                                numberOfLine = numberOfLine + 20;
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        newDrawing.commandCheck.Parameterscheck(e, newDrawing, n, numberOfLine);
                        numberOfLine = numberOfLine + 20;
                    }
                    if (!newDrawing.error)
                    {
                        newDrawing.moveTo(x, y);
                    }
                }

                else if (split[0].Equals("drawto"))
                {
                    String[] data     = split[1].Split(',');
                    int      x        = 0;
                    int      y        = 0;
                    bool     variable = false;

                    try
                    {
                        if (!int.TryParse(data[0], out x))
                        {
                            if (!newDrawing.v.exisitingVariable(data[0]))
                            {
                                variable = true;
                            }
                            else
                            {
                                x = newDrawing.v.pullData(data[0]);
                            }
                            if (variable)
                            {
                                newDrawing.commandCheck.Parameterscheck(false, data[0], n, newDrawing, numberOfLine);
                                numberOfLine = numberOfLine + 20;
                            }
                        }
                        if (!int.TryParse(data[1], out y))
                        {
                            if (!newDrawing.v.exisitingVariable(data[1]))
                            {
                                variable = true;
                            }
                            else
                            {
                                y = newDrawing.v.pullData(data[1]);
                            }
                            if (variable)
                            {
                                newDrawing.commandCheck.Parameterscheck(false, data[1], n, newDrawing, numberOfLine);
                                numberOfLine = numberOfLine + 20;
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        newDrawing.commandCheck.Parameterscheck(e, newDrawing, n, numberOfLine);
                        numberOfLine = numberOfLine + 20;
                    }
                    if (!newDrawing.error)
                    {
                        newDrawing.drawTo(x, y);
                    }
                }

                else if (split[0].Equals("polygon"))
                {
                    String[]   data       = split[1].Split(',');
                    List <int> tempPoints = new List <int>();
                    int        i          = 0;
                    int        x          = 0;
                    int        y          = 0;
                    int        z          = 0;
                    bool       var        = false;

                    try
                    {
                        while (data.Length > i)
                        {
                            if (!int.TryParse(data[i], out x))
                            {
                                if (!newDrawing.v.exisitingVariable(data[i]))
                                {
                                    var = true;
                                }
                                else
                                {
                                    tempPoints.Add(newDrawing.v.pullData(data[i]));
                                }
                                if (var)
                                {
                                    newDrawing.commandCheck.Parameterscheck(false, data[i], n, newDrawing, numberOfLine);
                                    numberOfLine = numberOfLine + 20;
                                }
                            }
                            tempPoints.Add(x);
                            i++;
                        }
                    }
                    catch (Exception e)
                    {
                        newDrawing.commandCheck.Parameterscheck(e, newDrawing, n, numberOfLine);
                        numberOfLine = numberOfLine + 20;
                    }
                    if (!newDrawing.error)
                    {
                        int[] arr     = tempPoints.ToArray();
                        Shape polygon = new DrawPolygon(arr);
                        polygon.draw(newDrawing);
                    }
                }

                else if (split[0].Equals("rectangle"))
                {
                    String[] data = split[1].Split(',');
                    int      w    = 0;
                    int      h    = 0;

                    bool variable = false;

                    try
                    {
                        if (!int.TryParse(data[0], out w))
                        {
                            if (!newDrawing.v.exisitingVariable(data[0]))
                            {
                                variable = true;
                            }
                            else
                            {
                                w = newDrawing.v.pullData(data[0]);
                            }
                            if (variable)
                            {
                                newDrawing.commandCheck.Parameterscheck(false, data[0], n, newDrawing, numberOfLine);
                                numberOfLine = numberOfLine + 20;
                            }
                        }

                        if (!int.TryParse(data[1], out h))
                        {
                            if (!newDrawing.v.exisitingVariable(data[1]))
                            {
                                variable = true;
                            }
                            else
                            {
                                h = newDrawing.v.pullData(data[1]);
                            }
                            if (variable)
                            {
                                newDrawing.commandCheck.Parameterscheck(false, data[1], n, newDrawing, numberOfLine);
                                numberOfLine = numberOfLine + 20;
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        newDrawing.commandCheck.Parameterscheck(e, newDrawing, n, numberOfLine);
                        numberOfLine = numberOfLine + 20;
                    }
                    if (!newDrawing.error)
                    {
                        Shape rectangle = new DrawRectangle(w, h);
                        rectangle.draw(newDrawing);
                    }
                }

                else if (split[0].Equals("square"))
                {
                    String[] data = split[1].Split(',');
                    int      s    = 0;

                    bool variable = false;

                    try
                    {
                        if (!int.TryParse(data[0], out s))
                        {
                            if (!newDrawing.v.exisitingVariable(data[0]))
                            {
                                variable = true;
                            }
                            else
                            {
                                s = newDrawing.v.pullData(data[0]);
                            }
                            if (variable)
                            {
                                newDrawing.commandCheck.Parameterscheck(false, data[0], n, newDrawing, numberOfLine);
                                numberOfLine = numberOfLine + 20;
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        newDrawing.commandCheck.Parameterscheck(e, newDrawing, n, numberOfLine);
                        numberOfLine = numberOfLine + 20;
                    }
                    if (!newDrawing.error)
                    {
                        Shape square = new DrawRectangle(s, s);
                        square.draw(newDrawing);
                    }
                }

                else if (split[0].Equals("circle"))
                {
                    String[] data     = split[1].Split(',');
                    int      r        = 0;
                    bool     variable = false;
                    try
                    {
                        if (!int.TryParse(data[0], out r))
                        {
                            if (!newDrawing.v.exisitingVariable(data[0]))
                            {
                                variable = true;
                            }
                            else
                            {
                                r = newDrawing.v.pullData(data[0]);
                            }
                            if (variable)
                            {
                                newDrawing.commandCheck.Parameterscheck(false, data[0], n, newDrawing, numberOfLine);
                                numberOfLine = numberOfLine + 20;
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        newDrawing.commandCheck.Parameterscheck(e, newDrawing, n, numberOfLine);
                        numberOfLine = numberOfLine + 20;
                    }
                    if (!newDrawing.error)
                    {
                        Shape circle = new DrawCircle(r);
                        circle.draw(newDrawing);
                    }
                }

                else if (split[0].Equals("triangle"))
                {
                    String[] data = split[1].Split(',');
                    int      h    = 0;
                    int      b    = 0;
                    int      a    = 0;

                    bool variable = false;
                    try
                    {
                        if (!int.TryParse(data[0], out h))
                        {
                            if (!newDrawing.v.exisitingVariable(data[0]))
                            {
                                variable = true;
                            }
                            else
                            {
                                h = newDrawing.v.pullData(data[0]);
                            }
                            if (variable)
                            {
                                newDrawing.commandCheck.Parameterscheck(false, data[0], n, newDrawing, numberOfLine);
                                numberOfLine = numberOfLine + 20;
                            }
                        }

                        if (!int.TryParse(data[1], out b))
                        {
                            if (!newDrawing.v.exisitingVariable(data[1]))
                            {
                                variable = true;
                            }
                            else
                            {
                                b = newDrawing.v.pullData(data[1]);
                            }
                            if (variable)
                            {
                                newDrawing.commandCheck.Parameterscheck(false, data[1], n, newDrawing, numberOfLine);
                                numberOfLine = numberOfLine + 20;
                            }
                        }

                        if (!int.TryParse(data[0], out a))
                        {
                            if (!newDrawing.v.exisitingVariable(data[0]))
                            {
                                variable = true;
                            }
                            else
                            {
                                a = newDrawing.v.pullData(data[0]);
                            }
                            if (variable)
                            {
                                newDrawing.commandCheck.Parameterscheck(false, data[0], n, newDrawing, numberOfLine);
                                numberOfLine = numberOfLine + 20;
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        newDrawing.commandCheck.Parameterscheck(e, newDrawing, n, numberOfLine);
                        numberOfLine = numberOfLine + 20;
                    }
                    if (!newDrawing.error)
                    {
                        Shape triangle = new DrawTriangle(h, b, a);
                        triangle.draw(newDrawing);
                    }
                }

                else if (split[0].Equals("pen"))
                {
                    Color pencolor = Color.FromName(split[1]);
                    if (pencolor.IsKnownColor == false)
                    {
                        commandCheck.Parameterscheck(false, split[1], n, newDrawing, numberOfLine);
                        numberOfLine = numberOfLine + 20;
                    }
                    if (!newDrawing.error)
                    {
                        newDrawing.set_penColor(pencolor);
                    }
                }

                else if (split[0].Equals("fill"))
                {
                    bool valueOn  = split[1].Equals("on");
                    bool valueOff = split[1].Equals("off");

                    if (valueOn == false && valueOff == false)
                    {
                        commandCheck.Parameterscheck(false, split[1], n, newDrawing, numberOfLine);
                        numberOfLine = numberOfLine + 20;
                    }
                    if (!newDrawing.error)
                    {
                        if (valueOn)
                        {
                            newDrawing.fill = true;
                        }
                        else if (valueOff)
                        {
                            newDrawing.fill = false;
                        }
                    }
                }

                else if (split[0].Equals("reset"))
                {
                    newDrawing.reset();
                }

                else if (split[0].Equals("clear"))
                {
                    newDrawing.clear();
                }

                else if (split[0].Equals("exit"))
                {
                    Application.Exit();
                }

                else if (newDrawing.m.existingMethod(method[0]))
                {
                    String[] methodValue   = (newDrawing.m.getMethod(method[0])).Split(',');
                    String   methodCmd     = method[0] + "command";
                    String   methodCommand = newDrawing.m.getMethod(methodCmd);
                    String[] userValue     = method[1].Split(',');
                    int      x             = 0;
                    while (methodValue.Length > x)
                    {
                        String[] valueStore = (methodValue[x] + "=" + userValue[x]).Split(' ');
                        CommandsEntered(valueStore, newDrawing, n);
                        x++;
                    }
                    multiplecommands(methodCommand, newDrawing);
                }

                else if (split[1].Equals("="))
                {
                    try
                    {
                        if (split[3].Equals("+"))
                        {
                            int  variableValue;
                            int  x        = 0;
                            int  y        = 0;
                            bool variable = false;
                            try
                            {
                                if (!int.TryParse(split[2], out x))
                                {
                                    if (!newDrawing.v.exisitingVariable(split[2]))
                                    {
                                        variable = true;
                                    }
                                    else
                                    {
                                        x = newDrawing.v.pullData(split[2]);
                                    }
                                }
                                if (!int.TryParse(split[4], out y))
                                {
                                    if (!newDrawing.v.exisitingVariable(split[4]))
                                    {
                                        variable = true;
                                    }
                                    else
                                    {
                                        y = newDrawing.v.pullData(split[4]);
                                    }
                                }
                                if (variable)
                                {
                                    commandCheck.Parameterscheck(false, split[2], n, newDrawing, numberOfLine);
                                    numberOfLine = numberOfLine + 20;
                                }
                            }
                            catch (Exception e)
                            {
                                commandCheck.Parameterscheck(e, newDrawing, n, numberOfLine);
                                numberOfLine = numberOfLine + 20;
                            }
                            variableValue = x + y;
                            newDrawing.v.UpdateVariable(split[0], variableValue);
                        }

                        if (split[3].Equals("*"))
                        {
                            int  variableValue;
                            int  x        = 0;
                            int  y        = 0;
                            bool variable = false;

                            try
                            {
                                if (!int.TryParse(split[2], out x))
                                {
                                    if (!newDrawing.v.exisitingVariable(split[2]))
                                    {
                                        variable = true;
                                    }
                                    else
                                    {
                                        x = newDrawing.v.pullData(split[2]);
                                    }
                                }
                                if (!int.TryParse(split[4], out y))
                                {
                                    if (!newDrawing.v.exisitingVariable(split[4]))
                                    {
                                        variable = true;
                                    }
                                    else
                                    {
                                        y = newDrawing.v.pullData(split[4]);
                                    }
                                }
                                if (variable)
                                {
                                    commandCheck.Parameterscheck(false, split[2], n, newDrawing, numberOfLine);
                                    numberOfLine = numberOfLine + 20;
                                }
                            }
                            catch (Exception e)
                            {
                                commandCheck.Parameterscheck(e, newDrawing, n, numberOfLine);
                                numberOfLine = numberOfLine + 20;
                            }
                            variableValue = x * y;
                            newDrawing.v.UpdateVariable(split[0], variableValue);
                        }

                        if (split[3].Equals("-"))
                        {
                            int  variableValue;
                            int  x        = 0;
                            int  y        = 0;
                            bool variable = false;

                            try
                            {
                                if (!int.TryParse(split[2], out x))
                                {
                                    if (!newDrawing.v.exisitingVariable(split[2]))
                                    {
                                        variable = true;
                                    }
                                    else
                                    {
                                        x = newDrawing.v.pullData(split[2]);
                                    }
                                }

                                if (!int.TryParse(split[4], out y))
                                {
                                    if (!newDrawing.v.exisitingVariable(split[4]))
                                    {
                                        variable = true;
                                    }
                                    else
                                    {
                                        y = newDrawing.v.pullData(split[4]);
                                    }
                                }

                                if (variable)
                                {
                                    commandCheck.Parameterscheck(false, split[2], n, newDrawing, numberOfLine);
                                    numberOfLine = numberOfLine + 20;
                                }
                            }

                            catch (Exception e)
                            {
                                commandCheck.Parameterscheck(e, newDrawing, n, numberOfLine);
                                numberOfLine = numberOfLine + 20;
                            }
                            variableValue = x - y;
                            newDrawing.v.UpdateVariable(split[0], variableValue);
                        }

                        if (split[3].Equals("/"))
                        {
                            int  variableValue;
                            int  x        = 0;
                            int  y        = 0;
                            bool variable = false;

                            try
                            {
                                if (!int.TryParse(split[2], out x))
                                {
                                    if (!newDrawing.v.exisitingVariable(split[2]))
                                    {
                                        variable = true;
                                    }
                                    else
                                    {
                                        x = newDrawing.v.pullData(split[2]);
                                    }
                                }
                                if (!int.TryParse(split[4], out y))
                                {
                                    if (!newDrawing.v.exisitingVariable(split[4]))
                                    {
                                        variable = true;
                                    }
                                    else
                                    {
                                        y = newDrawing.v.pullData(split[4]);
                                    }
                                }
                                if (variable)
                                {
                                    commandCheck.Parameterscheck(false, split[2], n, newDrawing, numberOfLine);
                                    numberOfLine = numberOfLine + 20;
                                }
                            }
                            catch (Exception e)
                            {
                                commandCheck.Parameterscheck(e, newDrawing, n, numberOfLine);
                                numberOfLine = numberOfLine + 20;
                            }
                            variableValue = x / y;
                            newDrawing.v.UpdateVariable(split[0], variableValue);
                        }
                    }
                    catch
                    {
                        int x = 0;

                        try
                        {
                            bool variable = false;
                            if (!int.TryParse(split[2], out x))
                            {
                                if (!newDrawing.v.exisitingVariable(split[2]))
                                {
                                    variable = true;
                                }
                                else
                                {
                                    x = newDrawing.v.pullData(split[2]);
                                }
                                if (variable)
                                {
                                    newDrawing.commandCheck.Parameterscheck(false, split[2], n, newDrawing, numberOfLine);
                                    numberOfLine = numberOfLine + 20;
                                }
                            }
                        }
                        catch (Exception e)
                        {
                            newDrawing.commandCheck.Parameterscheck(e, newDrawing, n, numberOfLine);
                            numberOfLine = numberOfLine + 20;
                        }
                        if (!newDrawing.error)
                        {
                            if (!newDrawing.v.exisitingVariable(split[0]))
                            {
                                newDrawing.v.storeData(split[0], x);
                            }
                            else
                            {
                                newDrawing.v.UpdateVariable(split[0], x);
                            }
                        }
                    }
                }
                else
                {
                    commandCheck.Commands_Check(newDrawing, n, numberOfLine);
                    numberOfLine = numberOfLine + 20;
                }
            }
            catch
            {
                commandCheck.Commands_Check(newDrawing, n, numberOfLine);
                numberOfLine = numberOfLine + 20;
            }
        }