
Министерство образования и науки Российской Федерации
Государственное образовательное учреждение
высшего профессионального образования
Владимирский Государственный Университет
Кафедра информационных систем и информационного менеджмента
Лабораторная работа № 3
по дисциплине «Технология Программирования»
Тема: Графический редактор
Выполнил:
студент гр. ИСТ-111
Матафонов Д.И.
Принял:
Гусев Д.И.
Владимир 2012 г.
ВЫПОЛНЕНИЕ РАБОТЫ
1. СИСТЕМНЫЙ АНАЛИЗ.
Цель разработки.
Используя полученные в курсе лекций, практических занятий и лабораторных работ знания и навыки, создать простейший графический редактор для Windows.
1.2 Назначение программы.
Рисование графических примитивов: прямоугольников, эллипсов, прямых. Заливка фигур. Удаление выбранных фигур. Выбор толщины для каждой фигуры.
Методы решения.
При запуске приложения открывается окно графического редактора, в верхней панели меню выбирается фигура. Фигура строится по принципу “нажал, переместил, отпустил”. Чтобы задать толщину фигуры, нужно выбрать в списке нужную фигуру, затем необходимую толщину и нажать кнопку “Задать толщину”. Для того, чтобы задать цвет фигуры, помечаем её в списке и нажимаем кнопку “Задать цвет”. Чтобы удалить фигуру, также выделяем её в списке и выбираем в меню “Удалить фигуру”.
Программирование.
Содержание Model:
Figure.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
namespace GraphicalPaint.Model
{
public abstract class Figure
{
public string FigureName;
public Point Point1{ get; set; }
public Point Point2 { get; set; }
public Pen pen = new Pen(Pens.Black.Brush, 1);
public bool Fill;
public abstract void Draw(Graphics graphics);
}}
Rectangle.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
namespace GraphicalPaint.Model
{
public class Rectangle: Figure
{
public Rectangle ()
{
FigureName = "Прямоугольник";
}
public Point FirstCoord { get; set; }
public Point LastCoord { get; set; }
public override void Draw(Graphics graphics)
{
If (Fill)
{
graphics.FillRectangle(pen.Brush, FirstCoord.X, FirstCoord.Y, LastCoord.X - FirstCoord.X, LastCoord.Y - FirstCoord.Y);
}
else
{
graphics.DrawRectangle(pen, FirstCoord.X, FirstCoord.Y, LastCoord.X - FirstCoord.X, LastCoord.Y - FirstCoord.Y);
}
}
}
}
Line.Cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
namespace GraphicalPaint.Model
{
public class Line:Figure
{
public Line()
{
FigureName = "Линия";
}
public System.Drawing.Point FirstPoint { get; set; }
public System.Drawing.Point LastPoint { get; set; }
public override void Draw(System.Drawing.Graphics graphics)
{
graphics.DrawLine(pen,FirstPoint.X, FirstPoint.Y, LastPoint.X, LastPoint.Y);
}
}
}
Ellipse.Cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
namespace GraphicalPaint.Model
{
class Ellipse: Figure
{
public Ellipse()
{
FigureName = "Эллипс";
}
public Point Center { get; set; }
public Point Radius { get; set; }
public override void Draw(Graphics graphics)
{
If (Fill)
{
graphics.FillEllipse(pen.Brush, Center.X, Center.Y, Radius.X - Center.X, Radius.Y - Center.Y);
}
else
{
graphics.DrawEllipse(pen,Center.X, Center.Y,Radius.X - Center.X, Radius.Y - Center.Y);
}
}
}
}
DrawingFigure.Cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GraphicalPaint.Model
{
public class DrawingFigure
{
private List<Figure> figures = new List<Figure>();
public List<Figure> Figures
{
get
{
return figures;
}
}
public void CreateFigure(int FigureType, System.Drawing.Point Point1, System.Drawing.Point Point2)
{
switch (FigureType)
{
case 0: // Rectangle
Rectangle rect = new Rectangle();
rect.FirstCoord = Point1;
rect.LastCoord = Point2;
figures.Add(rect);
break;
case 1: // Line
Line line = new Line();
line.FirstPoint = Point1;
line.LastPoint = Point2;
figures.Add(line);
break;
case 2: // Ellipse
Ellipse ellipse = new Ellipse();
ellipse.Center = Point1;
ellipse.Radius = Point2;
figures.Add(ellipse);
break;
default: throw new Exception("Figure type is not supported");
}
}
public void DeleteFigures(int index)
{
figures.RemoveAt(index);
}
}
}
Содержание Controller:
FigureController.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using GraphicalPaint.Model;
using System.Windows.Forms;
namespace GraphicalPaint.Controller
{
public class FigureController
{
private Form1 View;
private DrawingFigure drawing;
public int thickness;
public DrawingFigure Drawing
{
get{return drawing;}
}
public FigureController(Form1 View)
{
this.View = View;
this.drawing = new DrawingFigure();
}
public void CreateFigure(int FigureType, Point Point1, Point Point2)
{
drawing.CreateFigure(FigureType, Point1, Point2);
View.RefreshFigures();
}
public void deletefigures(int Index)
{
drawing.DeleteFigures(Index);
View.RefreshFigures();
}
public void GetThickness(RadioButton rb1, RadioButton rb2, RadioButton rb3)
{
if (rb1.Checked == true)
{
thickness = 1;
}
if (rb2.Checked == true)
{
thickness = 3;
}
if (rb3.Checked == true)
{
thickness = 6;
}
}
}
}
Содержание View:
Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using GraphicalPaint.Controller;
using GraphicalPaint.Model;
namespace GraphicalPaint
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
figureController = new FigureController(this);
}
private FigureController figureController;
private Point FirstCoord;
private Point LastCoord;
private int FigureType;
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
label1.Text = " X " + e.X + " Y " + e.Y;
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
FirstCoord = e.Location;
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
LastCoord = e.Location;
figureController.CreateFigure(FigureType, FirstCoord, LastCoord);
}
private void прямоугольникToolStripMenuItem_Click(object sender, EventArgs e)
{
FigureType = 0;
}
private void линияToolStripMenuItem_Click(object sender, EventArgs e)
{
FigureType = 1;
}
private void эллипсToolStripMenuItem_Click(object sender, EventArgs e)
{
FigureType = 2;
}
public void RefreshFigures()
{
Graphics graphics = pictureBox1.CreateGraphics();
graphics.FillRectangle(new SolidBrush(Color.White), 0, 0, pictureBox1.Width, pictureBox1.Height);
listBox1.Items.Clear();
foreach (GraphicalPaint.Model.Figure figure in figureController.Drawing.Figures)
{
figure.Draw(graphics);
listBox1.Items.Add(figure.FigureName);
}
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
RefreshFigures();
}
private void удалитьФигуруToolStripMenuItem_Click(object sender, EventArgs e)
{
figureController.deletefigures(this.listBox1.SelectedIndex);
}
private void ButtonOfColor_Click(object sender, EventArgs e)
{
Figure figure;
figure = figureController.Drawing.Figures[this.listBox1.SelectedIndex];
DialogResult result = colorDialog1.ShowDialog();
figure.pen.Color = colorDialog1.Color;
figure.Fill = this.ButtonOfColor.CanSelect;
if (result != DialogResult.OK)
{
return;
}
RefreshFigures();
}
private void ButtonOfThickness_Click(object sender, EventArgs e)
{
Figure figure;
figure = figureController.Drawing.Figures[this.listBox1.SelectedIndex];
if (radioButton1.Checked) { figure.pen.Width = 1; }
if (radioButton2.Checked) { figure.pen.Width = 3; }
if (radioButton3.Checked) { figure.pen.Width = 6; }
RefreshFigures();
}
}
}