You are on page 1of 23

1.

POLYGON // ,

CLASS POLYGON
public class Polygon
{
public List<Point> Points;
public bool IsClosed;
public Color Color;
public Polygon(Color color)
{
Points = new List<Point>();
Color = color;
IsClosed = false;
}
public void AddPoint(Point point)
{
Points.Add(point);
}
public void Draw(Graphics g)
{
if (Points.Count > 1)
{
Brush brush = new SolidBrush(Color);
Pen pen = new Pen(brush);
if (IsClosed)
{
g.FillPolygon(brush, Points.ToArray());
}
else
{
g.DrawLines(pen, Points.ToArray());
}
brush.Dispose();
pen.Dispose();
}

public void Move(int dx, int dy)


{
for(int i = 0; i < Points.Count; i++)
{
Points[i] = new Point(Points[i].X + dx, Points[i].Y + dy);
}
}
}
POLIGONDOC
public class PolygonDoc

{
public List<Polygon> Polygons { get; set; }
[NonSerialized]
public bool IsChanged;
public PolygonDoc()
{
Polygons = new List<Polygon>();
IsChanged = false;
}
public void AddPolygon(Polygon polygon)
{
Polygons.Add(polygon);
IsChanged = true;
}
public void Draw(Graphics g)
{
foreach (Polygon polygon in Polygons)
{
polygon.Draw(g);
}
}
public void Move(int dx, int dy)
{
foreach (Polygon polygon in Polygons)
{
polygon.Move(dx, dy);
}
}
}
FORM1
public partial class Form1 : Form
{
private Color currentColor;
private PolygonDoc polygonDoc;
private Point startPoint;
private Point previousPoint;
private Point currentPoint;
private Polygon currentPolygon;
private double distance;
private bool drawSquare;
private string FileName;
public Form1()
{
InitializeComponent();
newDoc();
this.DoubleBuffered = true;

currentColor = Color.Blue;
}
private void Form1_MouseClick(object sender, MouseEventArgs e)
{
if (startPoint.IsEmpty)
{
startPoint = e.Location;
currentPolygon = new Polygon(currentColor);
currentPolygon.AddPoint(startPoint);
previousPoint = startPoint;
}
else
{
if (drawSquare)
{
currentPolygon.IsClosed = true;
startPoint = Point.Empty;
previousPoint = Point.Empty;
drawSquare = false;
if (currentPolygon.Points.Count >= 3)
{
polygonDoc.AddPolygon(currentPolygon);
setTitle();
}
}
else
{
currentPolygon.AddPoint(e.Location);
previousPoint = e.Location;
}
}
Invalidate(true);
}
private double distanceSquared(Point p1, Point p2)
{
return (p1.X - p2.X) * (p1.X - p2.X) + (p1.Y - p2.Y) * (p1.Y - p2.Y);
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.Clear(Color.White);
if (currentPolygon != null)
{
currentPolygon.Draw(e.Graphics);
}
polygonDoc.Draw(e.Graphics);
Pen squarePen = new Pen(Color.Black, 2);
squarePen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
Pen linePen = new Pen(Color.Black, 1);
linePen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
if (drawSquare)
{
e.Graphics.DrawRectangle(squarePen, startPoint.X - 5, startPoint.Y - 5, 10, 10);

}
if (!startPoint.IsEmpty)
{
e.Graphics.DrawLine(linePen, previousPoint, currentPoint);
}
squarePen.Dispose();
linePen.Dispose();
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (!startPoint.IsEmpty)
{
distance = distanceSquared(e.Location, startPoint);
drawSquare = (distance <= 25);
}
currentPoint = e.Location;
Invalidate(true);
}
private void colorToolStripMenuItem_Click(object sender, EventArgs e)
{
ColorDialog colorDialog = new ColorDialog();
if (colorDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
currentColor = colorDialog.Color;
}
}
private void statusStrip1_Paint(object sender, PaintEventArgs e)
{
toolStripTotalPolygons.Text = string.Format("# Polygons: {0}",
polygonDoc.Polygons.Count);
toolStripCurrentPosition.Text = string.Format("{0}, {1}", currentPoint.X,
currentPoint.Y);
}
private void saveFile()
{
if (FileName == "Untitled")
{
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "Polygons doc file (*.pgn)|*.pgn";
saveFileDialog.Title = "Save polygons doc";
saveFileDialog.FileName = FileName;
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
FileName = saveFileDialog.FileName;
}
}
if (FileName != null)
{
using (FileStream fileStream = new FileStream(FileName, FileMode.Create))
{
IFormatter formatter = new BinaryFormatter();

formatter.Serialize(fileStream, polygonDoc);
}
polygonDoc.IsChanged = false;
setTitle();

}
}
private void openFile()
{
askToSave();
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "Polygons doc file (*.pgn)|*.pgn";
openFileDialog.Title = "Open polygons doc file";
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
FileName = openFileDialog.FileName;
try
{
using (FileStream fileStream = new FileStream(FileName, FileMode.Open))
{
IFormatter formater = new BinaryFormatter();
polygonDoc = (PolygonDoc)formater.Deserialize(fileStream);
}
setTitle();
}
catch (Exception ex)
{
MessageBox.Show("Could not read file: " + FileName);
FileName = null;
return;
}
Invalidate(true);
}
}
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
openFile();
}
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
saveFile();
}
private void newDoc()
{
askToSave();
FileName = "Untitled";
polygonDoc = new PolygonDoc();
currentPolygon = null;
startPoint = Point.Empty;
previousPoint = Point.Empty;
setTitle();
}
private void newToolStripMenuItem_Click(object sender, EventArgs e)

{
newDoc();
Invalidate(true);
}
private void newToolStripButton_Click(object sender, EventArgs e)
{
newDoc();
Invalidate(true);
}
private void openToolStripButton_Click(object sender, EventArgs e)
{
openFile();
}
private void saveToolStripButton_Click(object sender, EventArgs e)
{
saveFile();
}
private void askToSave()
{
if (polygonDoc != null && polygonDoc.IsChanged)
{
DialogResult result = MessageBox.Show("Save document changes?", "Save
document?", MessageBoxButtons.YesNo);
if (result == System.Windows.Forms.DialogResult.Yes)
{
saveFile();
}
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
askToSave();
}
private void setTitle()
{
this.Text = string.Format("Draw Polygons - {0}{1}", FileName,
polygonDoc.IsChanged ? "*" : "");
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Down)
{
if (polygonDoc != null)
{
polygonDoc.Move(0, 5);
}
}
if (e.KeyCode == Keys.Up)

{
if (polygonDoc != null)
{
polygonDoc.Move(0, -5);
}
}
if (e.KeyCode == Keys.Right)
{
if (polygonDoc != null)
{
polygonDoc.Move(5, 0);
}
}
if (e.KeyCode == Keys.Left)
{
if (polygonDoc != null)
{
polygonDoc.Move(-5, 0);
}
}
Invalidate(true);
}

2. TICKING PIES // ,

CLASS PIE
public class Pie
{
public static int radius = 25;
public Color color;
public Point point;
public int Tick;
public Pie(Color c, Point p)
{
color = c;
point = p;
Tick = 0;
}
public void Draw(Graphics g) {
Brush b = new SolidBrush(color);
g.FillPie(b, point.X - radius, point.Y - radius, 2*radius, 2*radius, 0, (int) (4-Tick) * 90);
CLASS PIEDOC
public class PiesDoc
{
public List<Pie> pies;
public PiesDoc()

{
}

pies = new List<Pie>();

public void AddPie(Pie p) {


pies.Add(p);
}
public void Draw(Graphics g) {
foreach (Pie p in pies)
{
p.Draw(g);
}
}
public void TickPies() {
for (int i = pies.Count - 1; i >= 0; i--)
{
pies[i].Tick++;
if (pies[i].Tick == 4)
{
pies.RemoveAt(i);
}
}
}
}
FORM
public partial class Form1 : Form
{
Color currentColor;
PiesDoc piesDocument;
Random random;
string FileName;
public Form1()
{
InitializeComponent();
newDocument();
this.DoubleBuffered = true;
}
public void newDocument() {

currentColor = Color.Blue;
piesDocument = new PiesDoc();
random = new Random();

private void Form1_Paint(object sender, PaintEventArgs e)


{
e.Graphics.Clear(Color.White);
piesDocument.Draw(e.Graphics);

}
private void timer1_Tick(object sender, EventArgs e)
{

piesDocument.TickPies();
Invalidate(true);

private void Form1_MouseClick(object sender, MouseEventArgs e)


{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
piesDocument.AddPie(new Pie(currentColor, e.Location));
Invalidate(true);
}
}
private void btn_Click(object sender, EventArgs e)
{
if (timer1.Enabled)
{
timer1.Enabled = false;
btn.Text = "";
}
else
{
timer1.Enabled = true;
btn.Text = "";
}
}
void saveFile() {
if (FileName == null)
{
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "Pies doc file (*.pie)|*.pie";
sfd.Title = "Save pies doc";
sfd.FileName = FileName;
if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
FileName = sfd.FileName;
}
}
if (FileName != null)
{
using (FileStream fs = new FileStream(FileName, FileMode.Create))
{
IFormatter formatter = new BinaryFormatter();
formatter.Serialize(fs, piesDocument);
}
}
}

void openFile() {
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "Pies doc file (*.pie)|*.pie";
ofd.Title = "Open pies document file.";
if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
FileName = ofd.FileName;
try
{
using (FileStream fs = new FileStream(FileName, FileMode.Open))
{
IFormatter formatter = new BinaryFormatter();
piesDocument = (PiesDoc)formatter.Deserialize(fs);
}
}
catch (Exception)
{
MessageBox.Show("Could not read file: " + FileName);
FileName = null;
return;
}
Invalidate(true);
statusStrip1.Invalidate(true);
}
}
private void newToolStripMenuItem_Click(object sender, EventArgs e)
{
newDocument();
Invalidate(true);
}
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
openFile();
}
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
saveFile();
}
private void newToolStripButton_Click(object sender, EventArgs e)
{
newDocument();
Invalidate(true);
}
private void openToolStripButton_Click(object sender, EventArgs e)
{

10

openFile();
}
private void saveToolStripButton_Click(object sender, EventArgs e)
{
saveFile();
}
private void statusStrip1_Paint(object sender, PaintEventArgs e)
{
status.Text = String.Format("# Pies: " + piesDocument.pies.Count);
}
private void colorToolStripMenuItem_Click(object sender, EventArgs e)
{
ColorDialog cd = new ColorDialog();
if (cd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
currentColor = cd.Color;
}
}

3.COLORRECTANGLES //

Class Rectangle
public class Rectangle
{
public int Height;
public int Width;
public Color Color;
public Point Point;
public bool isSelected;
public Rectangle(Point p, int w, int h, Color c)
{
Point = p;
Width = w;
Height = h;
Color = c;
isSelected = false;
}

public void Draw(Graphics g) {


Brush b = new SolidBrush(Color);
if (isSelected)
{
Pen p = new Pen(Color.Red, 2);
g.DrawRectangle(p, Point.X, Point.Y, Width, Height);
p.Dispose();
}

11

g.FillRectangle(b, Point.X, Point.Y, Width, Height);


b.Dispose();

public void Select(Point p) {


if (p.X >= Point.X && p.X <= Point.X + Width && p.Y >= Point.Y && p.Y <= Point.Y +
Height)

{
}

isSelected = !isSelected;

}
}

Class RectangleDoc
public class RectangleDoc
{
public List<Rectangle> Rectangles;
public RectangleDoc()
{
Rectangles = new List<Rectangle>();
}
public void Draw(Graphics g) {
foreach (Rectangle r in Rectangles)
{
r.Draw(g);
}
}
public void Add(Rectangle r) {
Rectangles.Add(r);
}
public void Select(Point p) {
foreach (Rectangle r in Rectangles)
{
r.Select(p);
}
}
public void DeleteSelected() {
for (int i = Rectangles.Count - 1; i >= 0; i--)
{
if (Rectangles[i].isSelected)
{
Rectangles.RemoveAt(i);

12

}
}

public void DeleteAll() {


Rectangles.Clear();
}
}
FORM
public partial class Form1 : Form
{
public
public
public
public
public
public
public

string FileName;
RectangleDoc rectDoc;
Point prevPoint;
Point currPoint;
int x, y;
int Height, Width;
Color CurrColor;

public Form1()
{
InitializeComponent();
newDoc();
this.DoubleBuffered = true;
CurrColor = Color.Pink;
}
public void newDoc() {
rectDoc = new RectangleDoc();
prevPoint = Point.Empty;
currPoint = Point.Empty;
FileName = "Untitled";
}

public void saveFile() {


if (FileName == "Untitled")
{
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "Rectanges doc file (*.rct)|*.rct";
saveFileDialog.Title = "Save rectanges doc";
saveFileDialog.FileName = FileName;
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
FileName = saveFileDialog.FileName;
}
}
if (FileName != null)
{
using (FileStream fileStream = new FileStream(FileName, FileMode.Create))

13

{
IFormatter formatter = new BinaryFormatter();
formatter.Serialize(fileStream, rectDoc);
}

}
public void openFile() {
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "Rectanges doc file (*.rct)|*.rct";
openFileDialog.Title = "Open rectanges doc file";
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
FileName = openFileDialog.FileName;
try
{
using (FileStream fileStream = new FileStream(FileName, FileMode.Open))
{
IFormatter formater = new BinaryFormatter();
rectDoc = (RectangleDoc)formater.Deserialize(fileStream);
}
}
catch (Exception ex)
{
MessageBox.Show("Could not read file: " + FileName);
FileName = null;
return;
}
Invalidate(true);
}
}
private void Form1_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
// pri prviot klik
if (prevPoint.IsEmpty)
{
prevPoint = e.Location;
}
else // pri vtoriot klik
{
rectDoc.Add(new Rectangle(new Point(x,y), Width, Height, CurrColor));
prevPoint = Point.Empty;
currPoint = Point.Empty;
}
Invalidate(true); // so serijalizacija treba da se zachuva samo oboenite
pravoagolnici
}
if (e.Button == System.Windows.Forms.MouseButtons.Right)

14

{
rectDoc.Select(currPoint);
Invalidate(); // da se precrta so crvenata linija koja go obikoluva pravoagolnikot.
}

private void Form1_MouseMove(object sender, MouseEventArgs e)


{
// za crtanje na isprekinatata linija
currPoint = e.Location;
x = prevPoint.X;
y = prevPoint.Y;
if (x > currPoint.X) x = currPoint.X;
if (y > currPoint.Y) y = currPoint.Y;
Width = Math.Abs(prevPoint.X - currPoint.X);
Height = Math.Abs(prevPoint.Y - currPoint.Y);
Invalidate();
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.Clear(Color.White);
rectDoc.Draw(e.Graphics);
if (!prevPoint.IsEmpty)
{
Pen p = new Pen(Color.Black, 1);
p.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
e.Graphics.DrawRectangle(p, x, y, Width, Height);
p.Dispose();
}
}
private void newToolStripMenuItem_Click(object sender, EventArgs e)
{
rectDoc.DeleteAll();
}
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
openFile();
}
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
saveFile();
}
private void newToolStripButton_Click(object sender, EventArgs e)
{
rectDoc.DeleteAll();
}
private void openToolStripButton_Click(object sender, EventArgs e)
{
openFile();

15

}
private void saveToolStripButton_Click(object sender, EventArgs e)
{
saveFile();
}
private void colorToolStripMenuItem_Click(object sender, EventArgs e)
{
ColorDialog colorDialog = new ColorDialog();
if (colorDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
CurrColor = colorDialog.Color;
}
}
private void toolStripStatusLabel1_Paint(object sender, PaintEventArgs e)
{
label.Text = "Rectangles: " + rectDoc.Rectangles.Count;
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Delete)
{
rectDoc.DeleteSelected();
}
}

4.ISKRSHENA LINIJA
CLASS LINE
public class Line
{
public Point Start { get; set; }
public Point End { get; set; }
public int Width { get; set; }
public Line(Point start, Point end, int width)
{
Start = start;
End = end;
Width = width;
}

16

public void Draw(Graphics g)


{
Pen pen = new Pen(Color.Black, Width);
g.DrawLine(pen, Start, End);
pen.Dispose();
}}}
CLASS LINEDOC
public class LinesDoc
{
public List<Line> Lines { get; set; }

public LinesDoc()
{
Lines = new List<Line>();
}
public void AddLine(Point start, Point end, int width)
{
Line line = new Line(start, end, width);
Lines.Add(line);
}
public void DrawLines(Graphics g)
{
foreach (Line line in Lines)
{
line.Draw(g);
}}}

FORM1
public partial class Form1 : Form
{

17

private Point previusPoint;


private Point currentPoint;
private bool hasPosioner;

private Pen dashedPen;


private Pen solidPen;
private LinesDoc linesDoc;
private int width;
private static readonly int THIN_LINE = 1;
private static readonly int MEDIUM_LINE = 2;
private static readonly int THICK_LINE = 3;
private string FileName;
public Form1()
{
InitializeComponent();
currentPoint = new Point();
dashedPen = new Pen(Color.Black, 1);
dashedPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
solidPen = new Pen(Color.Black, 3);
DoubleBuffered = true;
hasPosioner = true;
linesDoc = new LinesDoc();
width = 3;
}
private void Form1_MouseClick(object sender, MouseEventArgs e)
{
if (previusPoint.IsEmpty)
{
previusPoint = new Point(e.X, e.Y);
}

18

else
{
Point current = new Point(e.X, e.Y);
linesDoc.AddLine(previusPoint, current, width);
previusPoint = current;
Invalidate(true);
}
}

private void Form1_MouseMove(object sender, MouseEventArgs e)


{
currentPoint.X = e.X;
currentPoint.Y = e.Y;
Invalidate(true);
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.Clear(Color.White);
if (hasPosioner)
{
e.Graphics.DrawLine(dashedPen, currentPoint.X, 0, currentPoint.X, Height);
e.Graphics.DrawLine(dashedPen, 0, currentPoint.Y, Width, currentPoint.Y);
}
linesDoc.DrawLines(e.Graphics);
}

private void positionerToolStripMenuItem_Click(object sender, EventArgs e)


{
positionerToolStripMenuItem.Checked = !positionerToolStripMenuItem.Checked;
hasPosioner = !hasPosioner;

19

private void mediumToolStripMenuItem_Click(object sender, EventArgs e)


{
width = MEDIUM_LINE;
}

private void thinToolStripMenuItem_Click(object sender, EventArgs e)


{
width = THIN_LINE;
}

private void tickToolStripMenuItem_Click(object sender, EventArgs e)


{
width = THICK_LINE;
}

private void statusStrip1_Paint(object sender, PaintEventArgs e)


{
toolStripStatusNumberOfLines.Text = string.Format("Total lines: {0}",
linesDoc.Lines.Count);
toolStripStatusPosition.Text = string.Format("{0}, {1}", currentPoint.X,
currentPoint.Y);
}

private void saveFile()


{
if (FileName == null)
{
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "Lines doc file (*.lin)|*.lin";

20

saveFileDialog.Title = "Save lines doc";


if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
FileName = saveFileDialog.FileName;
}
}
if (FileName != null)
{
using (FileStream fileStream = new FileStream(FileName, FileMode.Create))
{
IFormatter formatter = new BinaryFormatter();
formatter.Serialize(fileStream, linesDoc);
}
}
}
private void openFile()
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "Lines doc file (*.lin)|*.lin";
openFileDialog.Title = "Open lines file";
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
FileName = openFileDialog.FileName;
try
{
using (FileStream fileStream = new FileStream(FileName, FileMode.Open))
{
IFormatter formater = new BinaryFormatter();
linesDoc = (LinesDoc)formater.Deserialize(fileStream);
}

21

}
catch (Exception ex)
{
MessageBox.Show("Could not read file: " + FileName);
FileName = null;
return;
}
Invalidate(true);
}}
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
saveFile();
}
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
openFile();
}

private void saveToolStripButton_Click(object sender, EventArgs e)


{
saveFile();
}
private void openToolStripButton_Click(object sender, EventArgs e)
{
openFile();
}
private void newToolStripMenuItem_Click(object sender, EventArgs e)
{
linesDoc = new LinesDoc();
Invalidate(true);

22

}
private void newToolStripButton_Click(object sender, EventArgs e)
{
linesDoc = new LinesDoc();
Invalidate(true);
}
}
}

23