Home c# Drawing primitives in C #

Drawing primitives in C #

Author

Date

Category

recently began to study C #, and I can not do this task: create a program that allows you to draw graphic format primitive: circle, segment, rectangle, sector, text, ellipse, and the painted sector. The selection of this or that graphic primitive is carried out using the ListBox control. I would be grateful for the help.


Answer 1

only for seed …

Let’s start with abstraction

public enum figuretype
{
  Circle,
  Line
  Rectangle
  Sector
  Text
  Ellipse,
  Shadedsector
}
Interface IFigure.
{
  FigureType Type {Get; }
  STRING NAME {GET; }
  Point FirstPoint {Get; SET; }
  Point SecondPoint {Get; SET; }
}

Next, implement the interface so

class figure: ifigure
{
  // Ctor.
  Public Figure (FigureType FigureType, String Name)
  {
    if (String.ISNULLOREMPTY (Name)) Throw New ArgumentException (Nameof (Name));
    Type = FigureType;
    Name = Name;
  }
  Public FigureType Type {Get; Private SET; }
  Public String Name {Get; Private SET; }
  Public Point FirstPoint {Get; SET; }
  Public Point SecondPoint {Get; SET; }
}

The form itself is such a

Public Partial Class Form1: Form
{
  Private iFigure _selectedfigure;
  Private Bool _isdrawing;
  Public Form1 ()
  {
    Initializecomponent ();
    SETDATATOLISTBOX ();
    _ListBoxFigures.SelectedIndExchanged + = listboxfigures_selectedchanged;
    _panelcanvas.mousedown + = panelcanvas_mousedown;
    _panelcanvas.mouseup + = panelcanvas_mouseup;
    _panelcanvas.paint + = panelcanvas_paint;
    this.centerToScreen ();
    this.text = "facing";
  }
  /// & LT; Summary & GT;
  /// Filling a list
  /// & LT; / Summary & GT;
  Private Void SetDatatolistBox ()
  {
    _ListBoxFigures.Datasource = New List & LT; ifigure & gt;
    {
      New Figure (FigureType.line, "Cut"),
      New Figure (FigureType.Rectangle, "Rectangle"),
      New Figure (FigureType.circle, "Circle"),
    };
    _ListBoxFigures.displayMember = NameOF (ifigure.name);
  }
  /// & LT; Summary & GT;
  /// Processing Event Selection in the list
  /// & LT; / Summary & GT;
  /// & LT; Param Name = "Sender" & gt; & lt; / param & gt;
  /// & LT; Param name = "E" & gt; & lt; / param & gt;
  Private Void ListBoxFigures_SelectedChanged (Object Sender, Eventargs E)
  {
    if (_listboxfigures.selecteditem == null) return;
    _SelectedFigure = _ListBoxFigures.SelectedItem as ifigure;
  }
  /// & LT; Summary & GT;
  /// Processing Event Pressing the left mouse button on the panel
  /// & LT; / Summary & GT;
  /// & LT; Param Name = "Sender" & gt; & lt; / param & gt;
  /// & LT; Param name = "E" & gt; & lt; / param & gt;
  Private Void Panelcanvas_mouseDown (Object Sender, MouseEventArgs E)
  {
    if (_selectedfigure == NULL) RETURN;
    _selectedfigure.firstPoint = New Point (E.X, E.Y);
  }
  /// & LT; Summary & GT;
  /// Processing Event Release Mouse Button On Panel
  /// & LT; / Summary & GT;
  /// & LT; Param Name = "Sender" & gt; & lt; / param & gt;
  /// & LT; Param name = "E" & gt; & lt; / param & gt;
  Private Void Panelcanvas_mouseup (Object Sender, MouseEventArgs E)
  {
    if (_selectedfigure == NULL) RETURN;
    _selectedfigure.secondpoint = new point (E.X, E.Y);
    _ISDRAWING = TRUE;
    _panelcanvas.refresh ();
  }
  /// & LT; Summary & GT;
  /// Processing Event Perverted Panel
  /// & LT; / Summary & GT;
  /// & LT; Param Name = "Sender" & gt; & lt; / param & gt;
  /// & LT; Param name = "E" & gt; & lt; / param & gt;
  Private Void PanelcanVas_Paint (Object Sender, Painteventargs E)
  {
    if (! _isdrawing) Return;
    Switch (_selectedfigure.Type)
    {
      Case figuretype.circle:
        Throw New NotimplementeDextException ();
      Case FigureType.Line:
        Drawline (E.Graphics);
        Break;
      Case FigureType.Rectangle: 
DrawRectangle (E.Graphics);
        Break;
      Case FigureType.Sector:
        Throw New NotimplementeDextException ();
      Case FigureType.Text:
        Throw New NotimplementeDextException ();
      Case FigureType.ellipse:
        Throw New NotimplementeDextException ();
      Case FigureType.ShadedSector:
        Throw New NotimplementeDextException ();
    }
    _Isdrawing = false;
  }
  /// & LT; Summary & GT;
  /// Drawing a rectangle
  /// & LT; / Summary & GT;
  /// & lt; Param name = "Graphics" & gt; & lt; / param & gt;
  PRIVATE VOID DrawRectangle (Graphics Graphics)
  {
    var width = _selectedfigure.secondpoint.x - _selectedfigure.firstpoint.x;
    var height = _selectedfigure.secondpoint.y - _selectedfigure.firstpoint.y;
    VAR Rectangle =.
      new rectangle (_selectedfigure.firstpoint.x, _selectedfigure.firstpoint.y, width, height);
    Pen Blackpen = New Pen (Color.Black, 5);
    Graphics.DRAWRectangle (Blackpen, Rectangle);
    blackpen.dispose ();
  }
  /// & LT; Summary & GT;
  /// Drawing line
  /// & LT; / Summary & GT;
  /// & lt; Param name = "Graphics" & gt; & lt; / param & gt;
  Private Void Drawline (Graphics Graphics)
  {
    Pen Blackpen = New Pen (Color.Black, 5);
    graphics.drawline (BlackPen, _selectedfigure.firstpoint, _selectedfigure.secondpoint);
    blackpen.dispose ();
  }
}

works it so

 example of work

Whole example can be downloaded here

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions