/// <summary>
        /// when mouse was presses the create on of the following shapes depending
        /// on what the user desided to draw.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void frmSETPaint_MouseDown(object sender, MouseEventArgs e)
        {
            /* checks to see which of the options was clicked.*/
            if(tsbLine.Checked == true)
            {
               currentDrawObject = new Line(currentLineColour, currentLineThickness);
            }
            else if(tsbRectangle.Checked == true)
            {
               currentDrawObject = new RectangleShape(currentLineColour, currentFillColour, currentLineThickness);
            }
            else if(tsbEllipse.Checked == true)
            {
               currentDrawObject = new Ellipse(currentLineColour, currentFillColour, currentLineThickness);
            }

            if (currentDrawObject != null)
            {
               currentDrawObject.startPoint = e.Location;
               currentDrawObject.endPoint = e.Location;
            }
        }
示例#2
0
        /// <summary>
        /// Opening a new file ensure object passed is new and using a try statement
        /// we will try to open the file. If the test pass then it will try to read the file 
        /// and put the data from file into data members ( DtawObjects class).
        /// </summary>
        /// <param name="filename"></param>
        /// <param name="drawObjectHistory"></param>
        /// <returns>status</returns>
        public static bool OpenSETPaintFile(string filename, List<DrawObject> drawObjectHistory)
        {
            BinaryReader openFile;
             byte tempByte = 0;
             DrawObject drawObject = new DrawObject();
             bool status = true;

             drawObjectHistory.Clear();   /*opening a new file ensure object passed is new*/

             try
             {
            openFile = new BinaryReader(File.Open(filename, FileMode.Open, FileAccess.Read));
             }
             catch (Exception)
             {
            return false;
             }

             try
             {
            if(openFile.ReadByte() != 58) /*read and test file magic number*/
            {
               return false;
            }
            /*loop and check if its a line /Rectangle or ellipse and put the values into
              data members.*/
            while (true)
            {
               tempByte = openFile.ReadByte();
               if(tempByte == (byte)DrawObjectType.Line)
               {
                  drawObject = new Line();
               }
               else if(tempByte == (byte)DrawObjectType.Rectangle)
               {
                  drawObject = new RectangleShape();
               }
               else if(tempByte == (byte)DrawObjectType.Ellipse)
               {
                  drawObject = new Ellipse();
               }

               drawObject.startPoint = new Point(openFile.ReadInt32(), openFile.ReadInt32());
               drawObject.endPoint = new Point(openFile.ReadInt32(), openFile.ReadInt32());
               drawObject.fillColour = Color.FromArgb(openFile.ReadInt32());
               drawObject.lineColour = Color.FromArgb(openFile.ReadInt32());
               drawObject.lineThickness = openFile.ReadInt32();
               drawObjectHistory.Add(drawObject);
            }
             }
             catch (EndOfStreamException)
             {
            status = true;
             }
             catch (Exception)
             {
            status = false;
             }
             finally
             {
            openFile.Close();
             }

             return status;
        }
        /// <summary>
        /// When the mouse was released, the program can go ahead and add the object to the list .
        /// Also there is no need for cast because its already taken care by the compiler.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void frmSETPaint_MouseUp(object sender, MouseEventArgs e)
        {
            if(currentDrawObject != null)
               {
              if(currentDrawObject.startPoint != currentDrawObject.endPoint)
              {
                  drawObjectHistory.Add(currentDrawObject);
              }

              currentDrawObject = null;
               }

            this.Refresh();
        }