diff --git a/Paint_2/Form1.Designer.cs b/Paint_2/Form1.Designer.cs index 19e4947..745c93b 100644 --- a/Paint_2/Form1.Designer.cs +++ b/Paint_2/Form1.Designer.cs @@ -221,6 +221,7 @@ this.btnColorHistory4.Size = new System.Drawing.Size(25, 25); this.btnColorHistory4.TabIndex = 4; this.btnColorHistory4.UseVisualStyleBackColor = false; + this.btnColorHistory4.Click += new System.EventHandler(this.BtnColor_Click); // // btnColorHistory3 // @@ -232,6 +233,7 @@ this.btnColorHistory3.Size = new System.Drawing.Size(25, 25); this.btnColorHistory3.TabIndex = 3; this.btnColorHistory3.UseVisualStyleBackColor = false; + this.btnColorHistory3.Click += new System.EventHandler(this.BtnColor_Click); // // btnColorHistory2 // @@ -243,6 +245,7 @@ this.btnColorHistory2.Size = new System.Drawing.Size(25, 25); this.btnColorHistory2.TabIndex = 2; this.btnColorHistory2.UseVisualStyleBackColor = false; + this.btnColorHistory2.Click += new System.EventHandler(this.BtnColor_Click); // // btnColorHistory1 // @@ -254,6 +257,7 @@ this.btnColorHistory1.Size = new System.Drawing.Size(25, 25); this.btnColorHistory1.TabIndex = 0; this.btnColorHistory1.UseVisualStyleBackColor = false; + this.btnColorHistory1.Click += new System.EventHandler(this.BtnColor_Click); // // lblSelectedColor // @@ -388,10 +392,11 @@ this.nupPencilWidth.Size = new System.Drawing.Size(141, 21); this.nupPencilWidth.TabIndex = 17; this.nupPencilWidth.Value = new decimal(new int[] { - 2, + 10, 0, 0, 0}); + this.nupPencilWidth.ValueChanged += new System.EventHandler(this.nupPencilWidth_ValueChanged); // // label8 // @@ -503,6 +508,7 @@ this.btnClear.TabIndex = 27; this.btnClear.Text = "Clear"; this.btnClear.UseVisualStyleBackColor = false; + this.btnClear.Click += new System.EventHandler(this.btnClear_Click); // // btnFill // diff --git a/Paint_2/Form1.cs b/Paint_2/Form1.cs index 69e0589..2ac21e7 100644 --- a/Paint_2/Form1.cs +++ b/Paint_2/Form1.cs @@ -32,12 +32,13 @@ namespace Paint_2 } private Point MousePositionToCanvasPosition() { - return new Point(MousePosition.X - canvas.Location.X,MousePosition.Y - canvas.Location.Y); + //return new Point(MousePosition.X - canvas.Location.X, MousePosition.Y - canvas.Location.Y); + return canvas.PointToClient(MousePosition); } private void canvas_MouseDown(object sender, MouseEventArgs e) { drawing = true; - sketch.StartDrawing(MousePositionToCanvasPosition(), Color.FromArgb(rnd.Next(0,256), rnd.Next(0, 256), rnd.Next(0, 256)), (int)nupPencilWidth.Value); + sketch.StartDrawing(MousePositionToCanvasPosition(), Color.FromArgb(rnd.Next(0, 256), rnd.Next(0, 256), rnd.Next(0, 256)), (int)nupPencilWidth.Value); tmrRefresh.Enabled = true; List colorHistory = sketch.CurrentTool.GetLastColors(4); @@ -50,8 +51,8 @@ namespace Paint_2 { tmrRefresh.Enabled = false; sketch.AddDrawingPoint(MousePositionToCanvasPosition()); - canvas.Image = sketch.Paint(); drawing = false; + RefreshUi(); } private void tmrRefresh_Tick(object sender, EventArgs e) @@ -59,8 +60,39 @@ namespace Paint_2 if (drawing) { sketch.AddDrawingPoint(MousePositionToCanvasPosition()); - canvas.Image = sketch.Paint(); + } + RefreshUi(); + } + private void BtnColor_Click(object sender, EventArgs e) + { + Button button = sender as Button; + sketch.ChangePaintToolColor(button.BackColor); + tmrRefresh_Tick(sender, e); + } + private string ColorToString(Color color) + { + // Expected Hex:FFFFFF R:255 G:255 B:255 + string result = ""; + int toBase = 16; + result += "Hex:" + Convert.ToString(color.R, toBase) + Convert.ToString(color.G, toBase) + Convert.ToString(color.B, toBase) + " "; + result += "R:" + color.R + " G:" + color.G + " B:" + color.B; + return result; + } + private void RefreshUi() + { + canvas.Image = sketch.Paint(); + lblSelectedColor.Text = ColorToString(sketch.CurrentTool.Color); + } + private void btnClear_Click(object sender, EventArgs e) + { + sketch.Clear(); + RefreshUi(); + } + + private void nupPencilWidth_ValueChanged(object sender, EventArgs e) + { + sketch.ChangePaintToolWidth((int)nupPencilWidth.Value); } } } diff --git a/Paint_2/PaintTool.cs b/Paint_2/PaintTool.cs index edeb856..afdc77e 100644 --- a/Paint_2/PaintTool.cs +++ b/Paint_2/PaintTool.cs @@ -15,9 +15,10 @@ namespace Paint_2 List Widths { get; set; } int Width { get; set; } - void Start(Point point,Color color,int width); + void Start(Color color,int width); void Stop(Point point); void Add(Point point); + void Clear(); List GetLastColors(int colorNumber); void Paint(Bitmap canvas); } diff --git a/Paint_2/Pencil.cs b/Paint_2/Pencil.cs index c167922..9e733f8 100644 --- a/Paint_2/Pencil.cs +++ b/Paint_2/Pencil.cs @@ -52,16 +52,19 @@ namespace Paint_2 drawingCounter += 1; } } - public void Start(Point point, Color color, int width) + public void Start(Color color, int width) { Color = color; Width = width; List points = new List(); - points.Add(point); Drawings.Add(points); Colors.Add(Color); Widths.Add(Width); } + public void Clear() + { + Drawings = new List>(); + } public void Stop(Point point) { throw new NotImplementedException(); @@ -72,16 +75,20 @@ namespace Paint_2 if (Colors.Count <= colorNumber) { //We need to fill with black color - for (int i = colorNumber; i >= 0; i--) + for (int i = Colors.Count; i > 0; i--) + { + result.Add(Colors[(Colors.Count) - i]); + } + for (int i = colorNumber - Colors.Count;i > 0; i--) { result.Add(Color.FromArgb(0x00,0x00,0x00)); } } else { - for(int i = colorNumber; i > 0; i--) + for (int i = colorNumber; i > 0; i--) { - result.Add(Colors[(Colors.Count)-i]); + result.Add(Colors[(Colors.Count) - i]); } } return result; diff --git a/Paint_2/Sketch.cs b/Paint_2/Sketch.cs index 14476fe..9024a47 100644 --- a/Paint_2/Sketch.cs +++ b/Paint_2/Sketch.cs @@ -29,7 +29,8 @@ namespace Paint_2 CurrentColor = Color.FromArgb(0xFF, 0xFF, 0xFF); IsDrawing = false; - Drawing = new Bitmap(sketchSize.Width, sketchSize.Height); + SketchSize = sketchSize; + Drawing = new Bitmap(SketchSize.Width, SketchSize.Height); ToolList = toolList; if (toolList[0] != null) { @@ -46,7 +47,16 @@ namespace Paint_2 } public void StartDrawing(Point location,Color color,int width) { - CurrentTool.Start(location,color,width); + CurrentTool.Start(color,width); + CurrentTool.Add(location); + } + public void ChangePaintToolColor(Color color) + { + CurrentTool.Start(color,CurrentTool.Width); + } + public void ChangePaintToolWidth(int width) + { + CurrentTool.Start(CurrentTool.Color,width); } public void StopDrawing(Point location) { @@ -56,6 +66,14 @@ namespace Paint_2 { CurrentTool.Add(location); } + public void Clear() + { + Drawing = new Bitmap(SketchSize.Width, SketchSize.Height); + foreach (PaintTool tool in ToolList) + { + tool.Clear(); + } + } public Bitmap Paint() { foreach (PaintTool tool in ToolList)