Есть иерархия классов(6 фигур), не могу придумать, как сделать так, чтобы класс фигуры не содержал метод рисования, я создаю лист из фигур и потом уже, когда необходимо отрисовать, получаю нужный конструктор по индексу и вызываю метод Draw, но как сделать так, чтобы классы фигур не содержали метод рисования?
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();
}
}
Пример класса фигуры
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);
}
}
}