private void canvasPanel_MouseDown(object sender, MouseEventArgs e) { prevMouseLoc = new Point(e.X, e.Y); tempShape = new Shape(toolstrip_combobox.SelectedIndex, fillType, prevMouseLoc, prevMouseLoc, fillColor, lineColor, dashStyle, lineWidth); toolstrip_undo.Visible = true; undoToolStripMenuItem.Enabled = true; toolstrip_redo.Visible = false; redoToolStripMenuItem.Enabled = false; redoShapes = new List<Shape>(); unsavedChanges = true; }
private void canvasPanel_MouseUp(object sender, MouseEventArgs e) { if (prevMouseLoc.X != e.X && prevMouseLoc.Y != e.Y && tempShape != null){ shapes.Add(tempShape); tempShape = null; unsavedChanges = true; } }
private void drawShape(Graphics e, Shape shape) { Action drawRectangle = () => { Point[] points = new Point[4]; points[0] = new Point(shape.startPoint.X, shape.startPoint.Y); points[1] = new Point(shape.endPoint.X, shape.startPoint.Y); points[2] = new Point(shape.endPoint.X, shape.endPoint.Y); points[3] = new Point(shape.startPoint.X, shape.endPoint.Y); if (shape.getFill() == 1) { e.FillPolygon(shape.getSolidBrush(), points); } e.DrawPolygon(shape.getPen(), points); }; Action drawEllipse = () => { Point endPoint = new Point(shape.endPoint.X - shape.startPoint.X, shape.endPoint.Y - shape.startPoint.Y); if (shape.getFill() == 1) { e.FillEllipse(shape.getSolidBrush(), shape.startPoint.X, shape.startPoint.Y, endPoint.X, endPoint.Y); } e.DrawEllipse(shape.getPen(), shape.startPoint.X, shape.startPoint.Y, endPoint.X, endPoint.Y); }; Action drawRightTriangle = () => { Point[] points = new Point[3]; points[0] = new Point(shape.startPoint.X, shape.startPoint.Y); points[1] = new Point(shape.startPoint.X, shape.endPoint.Y); points[2] = new Point(shape.endPoint.X, shape.endPoint.Y); if (shape.getFill() == 1) { e.FillPolygon(shape.getSolidBrush(), points); } e.DrawPolygon(shape.getPen(), points); }; Action drawDiamond = () => { Point[] points = new Point[4]; points[0] = new Point((shape.startPoint.X + shape.endPoint.X) / 2, shape.startPoint.Y); points[1] = new Point(shape.endPoint.X, (shape.startPoint.Y + shape.endPoint.Y) / 2); points[2] = new Point((shape.startPoint.X + shape.endPoint.X) / 2, shape.endPoint.Y); points[3] = new Point(shape.startPoint.X, (shape.startPoint.Y + shape.endPoint.Y) / 2); if (shape.getFill() == 1) { e.FillPolygon(shape.getSolidBrush(), points); } e.DrawPolygon(shape.getPen(), points); }; Action drawIsocelesTriangle = () => { Point[] points = new Point[3]; points[0] = new Point((shape.startPoint.X + shape.endPoint.X) / 2, shape.startPoint.Y); points[1] = new Point(shape.startPoint.X, shape.endPoint.Y); points[2] = new Point(shape.endPoint.X, shape.endPoint.Y); if (shape.getFill() == 1) { e.FillPolygon(shape.getSolidBrush(), points); } e.DrawPolygon(shape.getPen(), points); }; Action drawHexagon = () => { Point[] points = new Point[6]; Point c = new Point(shape.startPoint.X + ((shape.endPoint.X - shape.startPoint.X) / 2), shape.startPoint.Y + ((shape.endPoint.Y - shape.startPoint.Y) / 2)); double a = Math.PI * 2 / 6; double rotate = Math.PI * 2 / 12; double r = Math.Abs(shape.endPoint.X - shape.startPoint.X) / 2; points[0] = new Point(c.X + (int)(Math.Sin(a * 0 + rotate) * r), c.Y + (int)(Math.Cos(a * 0 + rotate) * r)); points[1] = new Point(c.X + (int)(Math.Sin(a * 1 + rotate) * r), c.Y + (int)(Math.Cos(a * 1 + rotate) * r)); points[2] = new Point(c.X + (int)(Math.Sin(a * 2 + rotate) * r), c.Y + (int)(Math.Cos(a * 2 + rotate) * r)); points[3] = new Point(c.X + (int)(Math.Sin(a * 3 + rotate) * r), c.Y + (int)(Math.Cos(a * 3 + rotate) * r)); points[4] = new Point(c.X + (int)(Math.Sin(a * 4 + rotate) * r), c.Y + (int)(Math.Cos(a * 4 + rotate) * r)); points[5] = new Point(c.X + (int)(Math.Sin(a * 5 + rotate) * r), c.Y + (int)(Math.Cos(a * 5 + rotate) * r)); if (shape.getFill() == 1) { e.FillPolygon(shape.getSolidBrush(), points); } e.DrawPolygon(shape.getPen(), points); }; Action drawPentagon = () => { Point[] points = new Point[5]; Point c = new Point(shape.startPoint.X + ((shape.endPoint.X - shape.startPoint.X) / 2), shape.startPoint.Y + ((shape.endPoint.Y - shape.startPoint.Y) / 2)); double a = Math.PI * 2 / 5; double rotate = Math.PI * 2 / 10; double r = Math.Abs(shape.endPoint.X - shape.startPoint.X) / 2; points[0] = new Point(c.X + (int)(Math.Sin(a * 0 + rotate) * r), c.Y + (int)(Math.Cos(a * 0 + rotate) * r)); points[1] = new Point(c.X + (int)(Math.Sin(a * 1 + rotate) * r), c.Y + (int)(Math.Cos(a * 1 + rotate) * r)); points[2] = new Point(c.X + (int)(Math.Sin(a * 2 + rotate) * r), c.Y + (int)(Math.Cos(a * 2 + rotate) * r)); points[3] = new Point(c.X + (int)(Math.Sin(a * 3 + rotate) * r), c.Y + (int)(Math.Cos(a * 3 + rotate) * r)); points[4] = new Point(c.X + (int)(Math.Sin(a * 4 + rotate) * r), c.Y + (int)(Math.Cos(a * 4 + rotate) * r)); if (shape.getFill() == 1) { e.FillPolygon(shape.getSolidBrush(), points); } e.DrawPolygon(shape.getPen(), points); }; switch (shape.getType()) { case 0: drawRectangle(); break; case 1: drawEllipse(); break; case 2: drawRightTriangle(); break; case 3: drawDiamond(); break; case 4: drawIsocelesTriangle(); break; case 5: drawHexagon(); break; case 6: drawPentagon(); break; } }