Exemple #1
0
        // This is a constructor method for the DrawIt class.  Notice that it has the same name ("DrawIt") as the class,
        // and does not have a return type.  That's how we know it's a constructor method.
        public DrawIt()
        {
            // This may look familiar from last week's "create a Windows Forms application" assignment.
            // It is generated by Visual Studio to do the initial setup of the controls on your Windows Form.
            InitializeComponent();
            _actions = new UndoRedoActionSource <IDrawAction>();
            _actions.ActionsChanged += UpdateUi;
            _actions.ActionsChanged += UpdateClearButton;

            _canvasModel = new DrawingModel(CanvasPanel, _actions)
            {
                Background = new DrawBackgroundAction(_BACKGROUND_COLOR)
            };

            // This creates the Pen intance that draws lines on the canvas.
            _drawingPen = new Pen(_COLOR, _LINE_WIDTH);

            // This creates the Pen instance that draws the cursor.
            _cursorPen = new Pen(_CURSOR_COLOR, _LINE_WIDTH);
            //_brush = new SolidBrush(_COLOR);
            _brush = new SolidBrush(_COLOR);


            // This starts us out with a dark gray background on the canvas (so the user can see
            // where to draw).
            Clear();

            // These three lines add "event handlers" to the canvas so that we get told when the user
            // does something with the mouse.  An "event handler" is just a method that knows how to
            // respond to an event (something that happened).  You can watch the extra session from
            // this Thursday if you want to see how to create a method like this.
            CanvasPanel.MouseDown  += HandleMouseDown;
            CanvasPanel.MouseUp    += HandleMouseUp;
            CanvasPanel.MouseMove  += HandleMouseMoved;
            CanvasPanel.MouseLeave += HandleMouseLeave;

            KeyDown += HandleKeyDown;
        }
Exemple #2
0
        // This is a constructor method for the DrawIt class.  Notice that it has the same name ("DrawIt") as the class,
        // and does not have a return type.  That's how we know it's a constructor method.
        public DrawIt()
        {
            // This may look familiar from last week's "create a Windows Forms application" assignment.
            // It is generated by Visual Studio to do the initial setup of the controls on your Windows Form.
            InitializeComponent();
            _actions = new UndoRedoActionSource<IDrawAction>();
            _actions.ActionsChanged += UpdateUi;
            _actions.ActionsChanged += UpdateClearButton;

            _canvasModel = new DrawingModel(CanvasPanel, _actions)
            {
                Background = new DrawBackgroundAction(_BACKGROUND_COLOR)
            };

            // This creates the Pen intance that draws lines on the canvas.
            _drawingPen = new Pen(_COLOR, _LINE_WIDTH);

            // This creates the Pen instance that draws the cursor.
            _cursorPen = new Pen(_CURSOR_COLOR, _LINE_WIDTH);
            //_brush = new SolidBrush(_COLOR);
            _brush = new SolidBrush(_COLOR);

            // This starts us out with a dark gray background on the canvas (so the user can see
            // where to draw).
            Clear();

            // These three lines add "event handlers" to the canvas so that we get told when the user
            // does something with the mouse.  An "event handler" is just a method that knows how to
            // respond to an event (something that happened).  You can watch the extra session from
            // this Thursday if you want to see how to create a method like this.
            CanvasPanel.MouseDown += HandleMouseDown;
            CanvasPanel.MouseUp += HandleMouseUp;
            CanvasPanel.MouseMove += HandleMouseMoved;
            CanvasPanel.MouseLeave += HandleMouseLeave;

            KeyDown += HandleKeyDown;
        }
Exemple #3
0
        // This is a constructor method for the DrawIt class.  Notice that it has the same name ("DrawIt") as the class,
        // and does not have a return type.  That's how we know it's a constructor method.
        public DrawIt()
        {
            // This may look familiar from last week's "create a Windows Forms application" assignment.
            // It is generated by Visual Studio to do the initial setup of the controls on your Windows Form.
            InitializeComponent();

            // This line creates a SimpleDrawActionSource(), which is a class that very minimally
            // implements IUndoRedoActionSource with an action type of IDrawAction.
            // This won't support Undo or Redo, so the buttons will always be disabled, but it
            // will allow us to use the interface (since it has been implemented), and later we can
            // replace it with a more advanced implementation.
            // _actions = new SimpleActionSource<IDrawAction>();

            // This line creates a BrokenDrawActionSource(), with the behavior
            // we saw at the beginning of class (in DrawIt 1.0).  It appears to
            // allow Undo and Redo, but if you Undo some actions and then
            // start drawing more circles, it redraws the undone actions
            // before it starts drawing the new circles.
            // _actions = new BrokenActionSource<IDrawAction>();

            // Homework: Create an actual UndoRedoDrawActionSource() and make sure
            // that it has the proper Undo() and Redo() semantics.
            _actions = new UndoRedoActionSource <IDrawAction>();

            // This line says, "I want to listen to ActionsChanged events from this IActionSource.
            // Every time the ActionsChanged event happens, I want my UpdateUi() method to be called.
            //
            // Note the use of "+=" instead of "=".  That's how delegates are assigned to events.  Instead
            // of using "=", which would imply that only one delegate could listen to an event, we use "+=",
            // which basically says "Add my UpdateUi method as a listener to this event, along with any other
            // listeners who are listening to this event."  This is a good thing, because while we're listening
            // to the ActionsChanged event to update our UI, the DrawingModel will be listening to the same
            // ActionsChanged event to know when to redraw.
            //
            // Note also that I did *NOT* put parentheses after the "UpdateUi" in the line below.  Parentheses
            // indicate a method call -- that I'm actually invoking the method.  In this line, I'm NOT invoking
            // UpdateUi.  Instead, I'm talking about it.  I'm actually giving the event a reference to the method,
            // rather than calling the method.  I'm treating "UpdateUi" as if it were just another value, to be
            // assigned.  This is one of the great things about C#: a method can be treated as just another value,
            // assigned to a variable, passed around to another method as a parameter, etc.  It's a very powerful
            // and advanced language feature, which we're using here to set up our "UpdateUi" method as a listener
            // ("delegate") on the ActionsChanged event of our actions model.
            _actions.ActionsChanged += UpdateUi;

            // This adds ANOTHER event handler ("event listener") on the ActionsChanged
            // event of the _actions object.  When the _actions object fires its
            // ActionsChanged event, it will also call UpdateClearButton()
            _actions.ActionsChanged += UpdateClearButton;

            // This line creates a new DrawingModel which wraps around and encapsulates
            // the CanvasPanel, and then stores that DrawingModel into the instance variable
            // _canvasModel.
            //
            // Since _canvasModel is an "instance variable" (declared as part of the DrawIt
            // class, above), all of the "instance methods" here in DrawIt (also declared as
            // part of the DrawIt class) will be able to see it.  Anything that wants to draw
            // something on the canvas can say _canvasModel.DrawLine() or _canvasModel.DrawCircle() or...
            //
            // I have implemented versions of HandleMouseDown() and HandleMouseMoved() below
            // that draw simple circles on the canvas, and a circular cursor to show where
            // the next circle will be drawn, to illustrate how to use the DrawingModel.
            _canvasModel = new DrawingModel(CanvasPanel, _actions)
            {
                Background = new DrawBackgroundAction(_BACKGROUND_COLOR)
            };

            // This creates the Pen intance that draws lines on the canvas.
            _drawingPen = new Pen(_COLOR, _LINE_WIDTH);

            // This creates the Pen instance that draws the cursor.
            _cursorPen = new Pen(_CURSOR_COLOR, _LINE_WIDTH);

            // This starts us out with a dark gray background on the canvas (so the user can see
            // where to draw).
            Clear();

            // These three lines add "event handlers" to the canvas so that we get told when the user
            // does something with the mouse.  An "event handler" is just a method that knows how to
            // respond to an event (something that happened).  You can watch the extra session from
            // this Thursday if you want to see how to create a method like this.
            CanvasPanel.MouseDown  += HandleMouseDown;
            CanvasPanel.MouseUp    += HandleMouseUp;
            CanvasPanel.MouseMove  += HandleMouseMoved;
            CanvasPanel.MouseLeave += HandleMouseLeave;

            KeyDown += HandleKeyDown;
        }
Exemple #4
0
        // This is a constructor method for the DrawIt class.  Notice that it has the same name ("DrawIt") as the class,
        // and does not have a return type.  That's how we know it's a constructor method.
        public DrawIt()
        {
            // This may look familiar from last week's "create a Windows Forms application" assignment.
            // It is generated by Visual Studio to do the initial setup of the controls on your Windows Form.
            InitializeComponent();

            // This line creates a SimpleDrawActionSource(), which is a class that very minimally
            // implements IUndoRedoActionSource with an action type of IDrawAction.
            // This won't support Undo or Redo, so the buttons will always be disabled, but it
            // will allow us to use the interface (since it has been implemented), and later we can
            // replace it with a more advanced implementation.
            _actions = new SimpleActionSource<IDrawAction>();

            // This line creates a BrokenDrawActionSource(), with the behavior
            // we saw at the beginning of class (in DrawIt 1.0).  It appears to
            // allow Undo and Redo, but if you Undo some actions and then
            // start drawing more circles, it redraws the undone actions
            // before it starts drawing the new circles.
            //_actions = new BrokenActionSource<IDrawAction>();

            // Homework: Create an actual UndoRedoDrawActionSource() and make sure
            // that it has the proper Undo() and Redo() semantics.
            //_actions = new UndoRedoDrawActionSource();
            _actions= new UndoRedoActionSource<IDrawAction>();

            // This line says, "I want to listen to ActionsChanged events from this IActionSource.
            // Every time the ActionsChanged event happens, I want my UpdateUi() method to be called.
            //
            // Note the use of "+=" instead of "=".  That's how delegates are assigned to events.  Instead
            // of using "=", which would imply that only one delegate could listen to an event, we use "+=",
            // which basically says "Add my UpdateUi method as a listener to this event, along with any other
            // listeners who are listening to this event."  This is a good thing, because while we're listening
            // to the ActionsChanged event to update our UI, the DrawingModel will be listening to the same
            // ActionsChanged event to know when to redraw.
            //
            // Note also that I did *NOT* put parentheses after the "UpdateUi" in the line below.  Parentheses
            // indicate a method call -- that I'm actually invoking the method.  In this line, I'm NOT invoking
            // UpdateUi.  Instead, I'm talking about it.  I'm actually giving the event a reference to the method,
            // rather than calling the method.  I'm treating "UpdateUi" as if it were just another value, to be
            // assigned.  This is one of the great things about C#: a method can be treated as just another value,
            // assigned to a variable, passed around to another method as a parameter, etc.  It's a very powerful
            // and advanced language feature, which we're using here to set up our "UpdateUi" method as a listener
            // ("delegate") on the ActionsChanged event of our actions model.
            _actions.ActionsChanged += UpdateUi;

            // This adds ANOTHER event handler ("event listener") on the ActionsChanged
            // event of the _actions object.  When the _actions object fires its
            // ActionsChanged event, it will also call UpdateClearButton()
            _actions.ActionsChanged += UpdateClearButton;

            // This line creates a new DrawingModel which wraps around and encapsulates
            // the CanvasPanel, and then stores that DrawingModel into the instance variable
            // _canvasModel.
            //
            // Since _canvasModel is an "instance variable" (declared as part of the DrawIt
            // class, above), all of the "instance methods" here in DrawIt (also declared as
            // part of the DrawIt class) will be able to see it.  Anything that wants to draw
            // something on the canvas can say _canvasModel.DrawLine() or _canvasModel.DrawCircle() or...
            //
            // I have implemented versions of HandleMouseDown() and HandleMouseMoved() below
            // that draw simple circles on the canvas, and a circular cursor to show where
            // the next circle will be drawn, to illustrate how to use the DrawingModel.
            _canvasModel = new DrawingModel(CanvasPanel, _actions)
            {
                Background = new DrawBackgroundAction(_BACKGROUND_COLOR)
            };

            // This creates the Pen intance that draws lines on the canvas.
            _pen = new Pen(_COLOR, _LINE_WIDTH);

            // This creates the Pen instance that draws the cursor.
            _cursorPen = new Pen(_CURSOR_COLOR, _LINE_WIDTH);

            // This starts us out with a dark gray background on the canvas (so the user can see
            // where to draw).
            Clear();

            // These three lines add "event handlers" to the canvas so that we get told when the user
            // does something with the mouse.  An "event handler" is just a method that knows how to
            // respond to an event (something that happened).  You can watch the extra session from
            // this Thursday if you want to see how to create a method like this.
            CanvasPanel.MouseDown += HandleMouseDown;
            CanvasPanel.MouseUp += HandleMouseUp;
            CanvasPanel.MouseMove += HandleMouseMoved;
            CanvasPanel.MouseLeave += HandleMouseLeave;
        }