Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Drawing;
- using System.Windows.Forms;
- namespace Canvas
- {
- internal enum Tool
- {
- Brush = 0,
- Line = 1,
- Circles = 2,
- Eraser = 3
- }
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- private Color BrushColor = Color.Black;
- private Tool CurrentTool = Tool.Brush;
- private Graphics Canvas;
- private bool IsDrawing = false;
- private Point Point1;
- private Point Point2;
- private void InitializeCanvas()
- {
- Canvas = canvas.CreateGraphics();
- }
- private void SetAppState(string state)
- {
- statusBar.Text = state;
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- SetAppState("Ready");
- UpdateColor();
- InitializeCanvas();
- SetCursor(Cursors.Cross);
- }
- private void UpdateColor()
- {
- colorIndicator.BackColor = BrushColor;
- }
- private void SetColor(Color color)
- {
- this.BrushColor = color;
- UpdateColor();
- }
- private void colorPickerButton_Click(object sender, EventArgs e)
- {
- DialogResult result = colorDialog1.ShowDialog();
- if (result == DialogResult.OK)
- {
- SetColor(colorDialog1.Color);
- }
- }
- private void brushToolButton_CheckedChanged(object sender, EventArgs e)
- {
- CurrentTool = Tool.Brush;
- SetCursor(Cursors.Cross);
- }
- private void canvas_MouseDown(object sender, MouseEventArgs e)
- {
- Point1 = new Point(e.X, e.Y);
- IsDrawing = true;
- SetAppState("Drawing");
- }
- private void DrawLine(Point pt1, Point pt2)
- {
- DrawLine(pt1, pt2, GetLineSz());
- }
- private void DrawLine(Point pt1, Point pt2, int sz)
- {
- Pen pen = new Pen(new SolidBrush(CurrentTool != Tool.Eraser ? BrushColor : Color.White), sz);
- Point[] points = { pt1, pt2 };
- int tensity = GetInt(tensityTb.Text);
- Canvas.DrawCurve(pen, points, tensity);
- }
- private void canvas_MouseUp(object sender, MouseEventArgs e)
- {
- if (CurrentTool == Tool.Line)
- {
- Point2 = new Point(e.X, e.Y);
- DrawLine(Point1, Point2);
- };
- IsDrawing = false;
- SetAppState("Ready");
- }
- private void DrawCircle(Point pt, int sz)
- {
- Pen pen = new Pen(BrushColor);
- Canvas.DrawEllipse(pen, pt.X, pt.Y, sz, sz);
- }
- private void canvas_MouseMove(object sender, MouseEventArgs e)
- {
- if(IsDrawing)
- {
- if (CurrentTool == Tool.Brush || CurrentTool == Tool.Eraser)
- {
- Point2 = new Point(e.X, e.Y);
- DrawLine(Point1, Point2);
- Point1 = Point2;
- }
- else if (CurrentTool == Tool.Circles)
- {
- Point pt = new Point((new Random()).Next(10, 500), (new Random()).Next(10, 500));
- DrawCircle(pt, (new Random()).Next(10, 5000));
- }
- }
- }
- private int GetInt(string str)
- {
- try
- {
- return Int32.Parse(str);
- }
- catch (FormatException)
- {
- return 0;
- }
- }
- private int GetLineSz()
- {
- return GetInt(brushSz.Text);
- }
- private void ClearCanvas()
- {
- canvas.Image = null;
- }
- private void clearCanvasButton_Click(object sender, EventArgs e)
- {
- ClearCanvas();
- }
- private void SetCursor(Cursor cursor)
- {
- canvas.Cursor = cursor;
- }
- private void rubberToolButton_CheckedChanged(object sender, EventArgs e)
- {
- CurrentTool = Tool.Eraser;
- SetCursor(Cursors.Hand);
- }
- private void lineToolButton_CheckedChanged(object sender, EventArgs e)
- {
- CurrentTool = Tool.Line;
- }
- private void circlesToolButton_CheckedChanged(object sender, EventArgs e)
- {
- CurrentTool = Tool.Circles;
- SetCursor(Cursors.WaitCursor);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement