A
A
Alertoso2022-03-15 19:25:22
OOP
Alertoso, 2022-03-15 19:25:22

How can I make the shape classes not contain a draw method?

There is a class hierarchy (6 figures), I can’t figure out how to make the figure class not contain a drawing method, I create a sheet of figures and then, when it is necessary to draw, I get the necessary constructor by index and call the Draw method, but how to do so that shape classes don't contain a draw method?

public MainWindow()
        {
            InitializeComponent();
            brushStroke = new SolidColorBrush(Colors.Black);
            brushFill = new SolidColorBrush(Colors.White);
            widthStroke = 7;
            pointsList = new PointCollection();
            countPoints = 2;
            txtCountPoints.DataContext = this;

            ObservableCollection<string> comboItems = new ObservableCollection<string>();
            cmbShapes.ItemsSource = comboItems;
            comboItems.Add("Линия");
            comboItems.Add("Ломаная");
            comboItems.Add("Прямоугольник");
            comboItems.Add("Эллипс");
            comboItems.Add("Треугольник");
            comboItems.Add("Многоугольник");

            listShapes = new NewShapeList();
            listShapesTypes = new List<Type>();
            listShapesTypes.Add(typeof(NewLine));
            listShapesTypes.Add(typeof(NewPolyline));
            listShapesTypes.Add(typeof(NewRectangle));
            listShapesTypes.Add(typeof(NewEllipse));
            listShapesTypes.Add(typeof(NewTriangle));
            listShapesTypes.Add(typeof(NewPolygon));

            slidStrWidth.Value = widthStroke;
            rectStrokeColor.Fill = brushStroke;
            rectFillColor.Fill = brushFill;
        }

        private void btnStrokeColor_Click(object sender, RoutedEventArgs e)
        {
            ColorDialog colorPicker = new ColorDialog();
            if (colorPicker.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                System.Drawing.Color color = colorPicker.Color;
                SolidColorBrush brushColor = new SolidColorBrush(Color.FromArgb(color.A, color.R, color.G, color.B));
                brushStroke = brushColor;
                rectStrokeColor.Fill = brushColor;
            }
        }

        private void btnFillColor_Click(object sender, RoutedEventArgs e)
        {
            ColorDialog colorPicker = new ColorDialog();
            if (colorPicker.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                System.Drawing.Color color = colorPicker.Color;
                SolidColorBrush brushColor = new SolidColorBrush(Color.FromArgb(color.A, color.R, color.G, color.B));
                brushFill = brushColor;
                rectFillColor.Fill = brushColor;
            }
        }

        private void canvasField_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            pointsList.Add(e.GetPosition(canvasField));
            if (pointsList.Count == CountPoints)
            {
                Type shapeType = listShapesTypes[cmbShapes.SelectedIndex];
                ConstructorInfo constructorInfo = shapeType.GetConstructor(new Type[] { typeof(double), typeof(SolidColorBrush), 
                                                                                            typeof(SolidColorBrush), typeof(PointCollection) });
                object objShape = constructorInfo.Invoke(new object[] { widthStroke, brushStroke, brushFill, pointsList });
                MethodInfo methodInfo = shapeType.GetMethod("Draw");
                object magicValue = methodInfo.Invoke(objShape, new object[] { canvasField });
                Convert.ChangeType(objShape, shapeType);
                listShapes.Shapes.Add((NewShape) objShape);
                pointsList.Clear();
            }
        }

Shape class example
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;

namespace LR4_OOP
{
    public class NewLine : NewShape
    {
        public NewLine(double strokeWidth, SolidColorBrush strokeColor, SolidColorBrush fillColor, PointCollection points) : base(strokeWidth, strokeColor, fillColor, points)
        {
        }

        public override void Draw(Canvas canvas)
        {
            Line line = new Line();
            line.X1 = Points[0].X;
            line.Y1 = Points[0].Y;
            line.X2 = Points[1].X;
            line.Y2 = Points[1].Y;
            line.Stroke = StrokeBrush;
            line.Fill = FillBrush;
            line.StrokeThickness = StrokeWidth;
            canvas.Children.Add(line);
        }
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Korotenko, 2022-03-16
@firedragon

create a render class, let it do the rendering. And let the figures remain figures and carry only POCO data
https://habr.com/ru/post/268371/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question