C# :: Aufgabe #221
1 Lösung

Zeichenprogramm / Paint für Arme
Anfänger - C#
von Exception
- 15.07.2018 um 08:17 Uhr
Hallo zusammen,
heute wird ein Grafikprogramm (Windows Forms) erstellt, mit dem man verschiedene geometrische Formen zeichnen kann.
Zunächst genügt es, die Formen "Ellipse", "Rechteck" und "Linie" zu implementieren.
Alle diese Formen haben folgende Gemeinsamkeiten:
- eine Position (Point, linke obere Ecke)
- eine Größe (Size)
- Rahmenfarbe / Linienfarbe (Color)
Das heißt, dass alle Formen drei gemeinsame Eigenschaften haben.
Für die GUI bzw. das Zeichnen auf dem Bildschrim soll in Form1 eine PictureBox angelegt werden - die Leinwand.
Zusätzlich eine ComboBox für die geometrische Form und eine für die Farbe.
Zusätzlich muss jede abgeleitete Klasse eine eigene Implementierung der Methode
C#-Code
besitzen (näheres siehe weiter unten).
Klassen:
Erstelle nun folgende Klassen:
- CsGeoForm (Basisklasse)
- CsRectangle, CsEllipse, CsLine (abgeleitete Klasse)
Zum Instanziieren müssen immer alle drei Eigenschaften (Position, Größe, Farbe) den (öffentlichen) Kontstruktoren übergeben werden.
Nachdem alle vier Klassen implementiert wurden wird nun die oben angesprochene Draw Methode implementiert.
Dies ist praktisch ein Zweizeiler.
Es wird zuerst ein Pen angelegt, der die entsprechende Farbe hat.
Anschließend wird die entsprechende Methode aus der Graphics Klasse verwendet.
Und fertig ist die Methode.
Wieder zurück zu Form1:
- Die PictureBox sollte die Eigenschaft BorderStyle auf "Fixed Single" gestellt werden.
- Nun brauchen wir eine Methode "getColor()", diese liefert später anhand der Farbauswahl der ComboBox die entsprechende systemdefinierte Farbe.
- Die Picturebox braucht nun ein Event - MouseClick.
Für die Implementierung des Events "MouseClick":
- zuerst muss geprüft werden, ob die linke oder rechte Maustaste gedrückt wurde.
- Linksklick: Startkoordinaten werden festgelegt.
- Rechtsklick: Höhe und Breite wird festgelegt und anschließend wird eine Instanz dder entsprechenden geometrischen Form erstellt.
Zum Schluss ist die geometrische Form durch einen Aufruf der Draw Methode zu zeichnen.
Hierfür ist ein Graphics-Objekt erforderlich
-> picturebox1.CreateGraphics()
--> Achtung: Muss später wieder mit Dispose() freigegeben werden!
Probleme, die es noch behoben werden müssen:
1)
Sollte das Fenster minimiert werden, so sind alle gezeichneten Rechtecke, etc. weg.
Das darf nicht sein!
Tipp: Weiteres Event -> "Paint"
2)
Evtl ist es während dem Testen bereits aufgefallen: Das Rechteck wird bei negativer Höhe/Breite nicht durch die Methode DrawRectangle gezeichnet!
Das sollte so auch nicht sein...
Tipp: Im Konstruktor von CsRectangle prüfen ob Höhe / Breite kleiner 0 ist.
Nice 2 Have / Erweiterungen:
- Weitere Formen
- ...
Viel Spaß :)
heute wird ein Grafikprogramm (Windows Forms) erstellt, mit dem man verschiedene geometrische Formen zeichnen kann.
Zunächst genügt es, die Formen "Ellipse", "Rechteck" und "Linie" zu implementieren.
Alle diese Formen haben folgende Gemeinsamkeiten:
- eine Position (Point, linke obere Ecke)
- eine Größe (Size)
- Rahmenfarbe / Linienfarbe (Color)
Das heißt, dass alle Formen drei gemeinsame Eigenschaften haben.
Für die GUI bzw. das Zeichnen auf dem Bildschrim soll in Form1 eine PictureBox angelegt werden - die Leinwand.
Zusätzlich eine ComboBox für die geometrische Form und eine für die Farbe.
Zusätzlich muss jede abgeleitete Klasse eine eigene Implementierung der Methode

void Draw(Graphics g) { ... }
besitzen (näheres siehe weiter unten).
Klassen:
Erstelle nun folgende Klassen:
- CsGeoForm (Basisklasse)
- CsRectangle, CsEllipse, CsLine (abgeleitete Klasse)
Zum Instanziieren müssen immer alle drei Eigenschaften (Position, Größe, Farbe) den (öffentlichen) Kontstruktoren übergeben werden.
Nachdem alle vier Klassen implementiert wurden wird nun die oben angesprochene Draw Methode implementiert.
Dies ist praktisch ein Zweizeiler.
Es wird zuerst ein Pen angelegt, der die entsprechende Farbe hat.
Anschließend wird die entsprechende Methode aus der Graphics Klasse verwendet.
Und fertig ist die Methode.
Wieder zurück zu Form1:
- Die PictureBox sollte die Eigenschaft BorderStyle auf "Fixed Single" gestellt werden.
- Nun brauchen wir eine Methode "getColor()", diese liefert später anhand der Farbauswahl der ComboBox die entsprechende systemdefinierte Farbe.
- Die Picturebox braucht nun ein Event - MouseClick.
Für die Implementierung des Events "MouseClick":
- zuerst muss geprüft werden, ob die linke oder rechte Maustaste gedrückt wurde.
- Linksklick: Startkoordinaten werden festgelegt.
- Rechtsklick: Höhe und Breite wird festgelegt und anschließend wird eine Instanz dder entsprechenden geometrischen Form erstellt.
Zum Schluss ist die geometrische Form durch einen Aufruf der Draw Methode zu zeichnen.
Hierfür ist ein Graphics-Objekt erforderlich
-> picturebox1.CreateGraphics()
--> Achtung: Muss später wieder mit Dispose() freigegeben werden!
Probleme, die es noch behoben werden müssen:
1)
Sollte das Fenster minimiert werden, so sind alle gezeichneten Rechtecke, etc. weg.
Das darf nicht sein!
Tipp: Weiteres Event -> "Paint"
2)
Evtl ist es während dem Testen bereits aufgefallen: Das Rechteck wird bei negativer Höhe/Breite nicht durch die Methode DrawRectangle gezeichnet!
Das sollte so auch nicht sein...
Tipp: Im Konstruktor von CsRectangle prüfen ob Höhe / Breite kleiner 0 ist.
Nice 2 Have / Erweiterungen:
- Weitere Formen
- ...
Viel Spaß :)
Lösungen:
Form1 Designer
C#-Code
Form1
C#-Code
PBForm
C#-Code
Vector2
C#-Code
PBCircle
C#-Code
PBLine
C#-Code
PBRectangle
C#-Code

namespace SelfMadePaint { partial class Form1 { /// <summary> /// Erforderliche Designervariable. /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// Verwendete Ressourcen bereinigen. /// </summary> /// <param name="disposing">True, wenn verwaltete Ressourcen gelöscht werden sollen; andernfalls False.</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Vom Windows Form-Designer generierter Code /// <summary> /// Erforderliche Methode für die Designerunterstützung. /// Der Inhalt der Methode darf nicht mit dem Code-Editor geändert werden. /// </summary> private void InitializeComponent() { this.RedTb = new System.Windows.Forms.TextBox(); this.GreenTb = new System.Windows.Forms.TextBox(); this.BlueTb = new System.Windows.Forms.TextBox(); this.vis_LbRed = new System.Windows.Forms.Label(); this.vis_GreenLb = new System.Windows.Forms.Label(); this.vis_BlueLb = new System.Windows.Forms.Label(); this.vis_ColorGb = new System.Windows.Forms.GroupBox(); this.vis_FormGb = new System.Windows.Forms.GroupBox(); this.FormsCb = new System.Windows.Forms.ComboBox(); this.FillCbox = new System.Windows.Forms.CheckBox(); this.LiveCbox = new System.Windows.Forms.CheckBox(); this.ClearBtn = new System.Windows.Forms.Button(); this.vis_ToolboxGb = new System.Windows.Forms.GroupBox(); this.FillPnl = new System.Windows.Forms.Panel(); this.CursorPnl = new System.Windows.Forms.Panel(); this.ColorPb = new System.Windows.Forms.PictureBox(); this.DrawBoxPb = new System.Windows.Forms.PictureBox(); this.vis_ColorGb.SuspendLayout(); this.vis_FormGb.SuspendLayout(); this.vis_ToolboxGb.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.ColorPb)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.DrawBoxPb)).BeginInit(); this.SuspendLayout(); // // RedTb // this.RedTb.Location = new System.Drawing.Point(47, 13); this.RedTb.Name = "RedTb"; this.RedTb.Size = new System.Drawing.Size(42, 20); this.RedTb.TabIndex = 1; this.RedTb.Text = "0"; this.RedTb.TextChanged += new System.EventHandler(this.TbColorChanged); this.RedTb.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.TbOnlyNumbers); // // GreenTb // this.GreenTb.Location = new System.Drawing.Point(47, 39); this.GreenTb.Name = "GreenTb"; this.GreenTb.Size = new System.Drawing.Size(42, 20); this.GreenTb.TabIndex = 2; this.GreenTb.Text = "0"; this.GreenTb.TextChanged += new System.EventHandler(this.TbColorChanged); this.GreenTb.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.TbOnlyNumbers); // // BlueTb // this.BlueTb.Location = new System.Drawing.Point(47, 65); this.BlueTb.Name = "BlueTb"; this.BlueTb.Size = new System.Drawing.Size(42, 20); this.BlueTb.TabIndex = 3; this.BlueTb.Text = "0"; this.BlueTb.TextChanged += new System.EventHandler(this.TbColorChanged); this.BlueTb.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.TbOnlyNumbers); // // vis_LbRed // this.vis_LbRed.AutoSize = true; this.vis_LbRed.Location = new System.Drawing.Point(6, 16); this.vis_LbRed.Name = "vis_LbRed"; this.vis_LbRed.Size = new System.Drawing.Size(27, 13); this.vis_LbRed.TabIndex = 4; this.vis_LbRed.Text = "Red"; // // vis_GreenLb // this.vis_GreenLb.AutoSize = true; this.vis_GreenLb.Location = new System.Drawing.Point(6, 42); this.vis_GreenLb.Name = "vis_GreenLb"; this.vis_GreenLb.Size = new System.Drawing.Size(36, 13); this.vis_GreenLb.TabIndex = 5; this.vis_GreenLb.Text = "Green"; // // vis_BlueLb // this.vis_BlueLb.AutoSize = true; this.vis_BlueLb.Location = new System.Drawing.Point(6, 68); this.vis_BlueLb.Name = "vis_BlueLb"; this.vis_BlueLb.Size = new System.Drawing.Size(28, 13); this.vis_BlueLb.TabIndex = 6; this.vis_BlueLb.Text = "Blue"; // // vis_ColorGb // this.vis_ColorGb.Controls.Add(this.ColorPb); this.vis_ColorGb.Controls.Add(this.vis_LbRed); this.vis_ColorGb.Controls.Add(this.vis_BlueLb); this.vis_ColorGb.Controls.Add(this.RedTb); this.vis_ColorGb.Controls.Add(this.vis_GreenLb); this.vis_ColorGb.Controls.Add(this.GreenTb); this.vis_ColorGb.Controls.Add(this.BlueTb); this.vis_ColorGb.Location = new System.Drawing.Point(648, 12); this.vis_ColorGb.Name = "vis_ColorGb"; this.vis_ColorGb.Size = new System.Drawing.Size(100, 121); this.vis_ColorGb.TabIndex = 7; this.vis_ColorGb.TabStop = false; this.vis_ColorGb.Text = "Color"; // // vis_FormGb // this.vis_FormGb.Controls.Add(this.FillCbox); this.vis_FormGb.Controls.Add(this.FormsCb); this.vis_FormGb.Location = new System.Drawing.Point(648, 139); this.vis_FormGb.Name = "vis_FormGb"; this.vis_FormGb.Size = new System.Drawing.Size(100, 76); this.vis_FormGb.TabIndex = 8; this.vis_FormGb.TabStop = false; this.vis_FormGb.Text = "Form"; // // FormsCb // this.FormsCb.Location = new System.Drawing.Point(6, 19); this.FormsCb.Name = "FormsCb"; this.FormsCb.Size = new System.Drawing.Size(88, 21); this.FormsCb.TabIndex = 0; this.FormsCb.SelectedIndexChanged += new System.EventHandler(this.FormsCb_SelectedIndexChanged); // // FillCbox // this.FillCbox.AutoSize = true; this.FillCbox.Location = new System.Drawing.Point(7, 46); this.FillCbox.Name = "FillCbox"; this.FillCbox.Size = new System.Drawing.Size(38, 17); this.FillCbox.TabIndex = 1; this.FillCbox.Text = "Fill"; this.FillCbox.UseVisualStyleBackColor = true; // // LiveCbox // this.LiveCbox.AutoSize = true; this.LiveCbox.Checked = true; this.LiveCbox.CheckState = System.Windows.Forms.CheckState.Checked; this.LiveCbox.Location = new System.Drawing.Point(428, 395); this.LiveCbox.Name = "LiveCbox"; this.LiveCbox.Size = new System.Drawing.Size(46, 17); this.LiveCbox.TabIndex = 9; this.LiveCbox.Text = "Live"; this.LiveCbox.UseVisualStyleBackColor = true; this.LiveCbox.CheckedChanged += new System.EventHandler(this.LiveCbox_CheckedChanged); // // ClearBtn // this.ClearBtn.Location = new System.Drawing.Point(648, 221); this.ClearBtn.Name = "ClearBtn"; this.ClearBtn.Size = new System.Drawing.Size(75, 23); this.ClearBtn.TabIndex = 10; this.ClearBtn.Text = "Clear"; this.ClearBtn.UseVisualStyleBackColor = true; this.ClearBtn.Click += new System.EventHandler(this.ClearBtn_Click); // // vis_ToolboxGb // this.vis_ToolboxGb.Controls.Add(this.FillPnl); this.vis_ToolboxGb.Controls.Add(this.CursorPnl); this.vis_ToolboxGb.Location = new System.Drawing.Point(442, 12); this.vis_ToolboxGb.Name = "vis_ToolboxGb"; this.vis_ToolboxGb.Size = new System.Drawing.Size(200, 55); this.vis_ToolboxGb.TabIndex = 11; this.vis_ToolboxGb.TabStop = false; this.vis_ToolboxGb.Text = "Toolbox"; // // FillPnl // this.FillPnl.BackgroundImage = global::SelfMadePaint.Properties.Resources.fill; this.FillPnl.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; this.FillPnl.Location = new System.Drawing.Point(47, 16); this.FillPnl.Name = "FillPnl"; this.FillPnl.Size = new System.Drawing.Size(25, 25); this.FillPnl.TabIndex = 1; this.FillPnl.Click += new System.EventHandler(this.FillPnl_Click); // // CursorPnl // this.CursorPnl.BackgroundImage = global::SelfMadePaint.Properties.Resources.cursor; this.CursorPnl.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; this.CursorPnl.Location = new System.Drawing.Point(6, 16); this.CursorPnl.Name = "CursorPnl"; this.CursorPnl.Size = new System.Drawing.Size(25, 25); this.CursorPnl.TabIndex = 0; this.CursorPnl.Click += new System.EventHandler(this.CursorPnl_Click); // // ColorPb // this.ColorPb.Location = new System.Drawing.Point(9, 91); this.ColorPb.Name = "ColorPb"; this.ColorPb.Size = new System.Drawing.Size(80, 24); this.ColorPb.TabIndex = 7; this.ColorPb.TabStop = false; // // DrawBoxPb // this.DrawBoxPb.BackColor = System.Drawing.Color.White; this.DrawBoxPb.Location = new System.Drawing.Point(12, 12); this.DrawBoxPb.Name = "DrawBoxPb"; this.DrawBoxPb.Size = new System.Drawing.Size(400, 400); this.DrawBoxPb.TabIndex = 0; this.DrawBoxPb.TabStop = false; this.DrawBoxPb.Paint += new System.Windows.Forms.PaintEventHandler(this.DrawBoxPb_Paint); this.DrawBoxPb.MouseDown += new System.Windows.Forms.MouseEventHandler(this.DrawBoxPb_MouseDown); this.DrawBoxPb.MouseMove += new System.Windows.Forms.MouseEventHandler(this.DrawBoxPb_MouseMove); this.DrawBoxPb.MouseUp += new System.Windows.Forms.MouseEventHandler(this.DrawBoxPb_MouseUp); // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(760, 426); this.Controls.Add(this.vis_ToolboxGb); this.Controls.Add(this.ClearBtn); this.Controls.Add(this.LiveCbox); this.Controls.Add(this.vis_FormGb); this.Controls.Add(this.vis_ColorGb); this.Controls.Add(this.DrawBoxPb); this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle; this.Name = "Form1"; this.Text = "Self Made Paint"; this.vis_ColorGb.ResumeLayout(false); this.vis_ColorGb.PerformLayout(); this.vis_FormGb.ResumeLayout(false); this.vis_FormGb.PerformLayout(); this.vis_ToolboxGb.ResumeLayout(false); ((System.ComponentModel.ISupportInitialize)(this.ColorPb)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.DrawBoxPb)).EndInit(); this.ResumeLayout(false); this.PerformLayout(); } #endregion private System.Windows.Forms.PictureBox DrawBoxPb; private System.Windows.Forms.TextBox RedTb; private System.Windows.Forms.TextBox GreenTb; private System.Windows.Forms.TextBox BlueTb; private System.Windows.Forms.Label vis_LbRed; private System.Windows.Forms.Label vis_GreenLb; private System.Windows.Forms.Label vis_BlueLb; private System.Windows.Forms.GroupBox vis_ColorGb; private System.Windows.Forms.PictureBox ColorPb; private System.Windows.Forms.GroupBox vis_FormGb; private System.Windows.Forms.CheckBox FillCbox; private System.Windows.Forms.ComboBox FormsCb; private System.Windows.Forms.CheckBox LiveCbox; private System.Windows.Forms.Button ClearBtn; private System.Windows.Forms.GroupBox vis_ToolboxGb; private System.Windows.Forms.Panel FillPnl; private System.Windows.Forms.Panel CursorPnl; } }
Form1

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace SelfMadePaint { public partial class Form1 : Form { private List<PBForm> DrawForms = new List<PBForm>(); private Color SelectedColor; private PBForm SelectedForm; private Vector2 FirstPosition; private bool Live; private bool Dragging; private bool Fill; public Form1() { InitializeComponent(); InitOwnComps(); } private void InitOwnComps() { DrawBoxPb.Image = new Bitmap(DrawBoxPb.Width, DrawBoxPb.Height); RedTb.Text = "180"; GreenTb.Text = "70"; BlueTb.Text = "0"; FormsCb.Items.Add(new PBLine()); FormsCb.Items.Add(new PBRectangle()); FormsCb.Items.Add(new PBCircle()); FormsCb.SelectedIndex = 0; Live = LiveCbox.Checked; CursorPnl.BorderStyle = BorderStyle.Fixed3D; FillPnl.BorderStyle = BorderStyle.None; } private void SetImage(Bitmap bmp) { if (DrawBoxPb.Image != null && DrawBoxPb.Image != bmp) DrawBoxPb.Image.Dispose(); DrawBoxPb.Image = bmp; } private void DrawBoxPb_Paint(object sender, PaintEventArgs e) { Bitmap bmp = new Bitmap(DrawBoxPb.Width, DrawBoxPb.Height); Graphics g = Graphics.FromImage(bmp); foreach (PBForm el in DrawForms) { if (el.FirstPosition != null && el.SecondPosition != null) el.Draw(g); } SetImage(bmp); } private void TbOnlyNumbers(object sender, KeyPressEventArgs e) { if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && (e.KeyChar != '.')) { e.Handled = true; } // only allow one decimal point if ((e.KeyChar == '.') && ((sender as TextBox).Text.IndexOf('.') > -1)) { e.Handled = true; } } private void TbColorChanged(object sender, EventArgs e) { if ((sender as TextBox).Text.Length <= 0) (sender as TextBox).Text = "0"; int r = Convert.ToInt32(RedTb.Text) > 255 ? 255 : Convert.ToInt32(RedTb.Text); int g = Convert.ToInt32(GreenTb.Text) > 255 ? 255 : Convert.ToInt32(GreenTb.Text); int b = Convert.ToInt32(BlueTb.Text) > 255 ? 255 : Convert.ToInt32(BlueTb.Text); SelectedColor = Color.FromArgb(r, g, b); ColorPb.BackColor = SelectedColor; } private void FormsCb_SelectedIndexChanged(object sender, EventArgs e) { SelectedForm = (FormsCb.SelectedItem as PBForm).NewInstance(); } private void DrawBoxPb_MouseDown(object sender, MouseEventArgs e) { if (!Fill) { Dragging = true; FirstPosition = (Vector2)DrawBoxPb.PointToClient(Cursor.Position); SelectedForm.FirstPosition = FirstPosition; SelectedForm.Color = SelectedColor; SelectedForm.Fill = FillCbox.Checked; DrawForms.Add(SelectedForm); } else { FillSelectedArea((Vector2)DrawBoxPb.PointToClient(Cursor.Position)); } } private void DrawBoxPb_MouseUp(object sender, MouseEventArgs e) { Dragging = false; SetFormSize(); DrawBoxPb.Refresh(); SelectedForm = (FormsCb.SelectedItem as PBForm).NewInstance(); } private void LiveCbox_CheckedChanged(object sender, EventArgs e) { Live = LiveCbox.Checked; } private void DrawBoxPb_MouseMove(object sender, MouseEventArgs e) { if (Live && Dragging) { SetFormSize(); DrawBoxPb.Refresh(); } } private void SetFormSize() { Vector2 secondPosition = ((Vector2)DrawBoxPb.PointToClient(Cursor.Position)); SelectedForm.SecondPosition = secondPosition; } private void ClearPaintArea() { DrawForms = new List<PBForm>(); DrawBoxPb.Refresh(); } private void ClearBtn_Click(object sender, EventArgs e) { ClearPaintArea(); } private void FillSelectedArea(Vector2 position) { Bitmap bmp = (Bitmap)DrawBoxPb.Image; Color col = bmp.GetPixel((int)position.X, (int)position.Y); List<Vector2> pixels = new List<Vector2>(); List<Vector2> closedPixels = new List<Vector2>(); pixels.Add(position); while (pixels.Count > 0) { Vector2 vec = pixels[0]; bmp.SetPixel((int)vec.X, (int)vec.Y, SelectedColor); DrawForms.Add(new PBRectangle(vec, vec + new Vector2(1, 1), SelectedColor, true)); pixels.Remove(vec); Vector2 l = vec.GetLNeighbor(); Vector2 r = vec.GetRNeighbor(); Vector2 t = vec.GetTNeighbor(); Vector2 b = vec.GetBNeighbor(); if (vec.X > 0 && bmp.GetPixel((int)l.X, (int)l.Y).Equals(col) && !pixels.Contains(l)) { pixels.Add(l); } if (vec.Y > 0 && bmp.GetPixel((int)b.X, (int)b.Y).Equals(col) && !pixels.Contains(b)) { pixels.Add(b); } if (vec.X < bmp.Width-1 && bmp.GetPixel((int)r.X, (int)r.Y).Equals(col) && !pixels.Contains(r)) { pixels.Add(r); } if (vec.Y < bmp.Height-1 && bmp.GetPixel((int)t.X, (int)t.Y).Equals(col) && !pixels.Contains(t)) { pixels.Add(t); } } SetImage(bmp); } private void CursorPnl_Click(object sender, EventArgs e) { Fill = false; CursorPnl.BorderStyle = BorderStyle.Fixed3D; FillPnl.BorderStyle = BorderStyle.None; } private void FillPnl_Click(object sender, EventArgs e) { Fill = true; CursorPnl.BorderStyle = BorderStyle.None; FillPnl.BorderStyle = BorderStyle.Fixed3D; } } }
PBForm

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Drawing; namespace SelfMadePaint { public abstract class PBForm { public abstract Vector2 FirstPosition { get; set; } public abstract Vector2 SecondPosition { get; set; } public abstract Vector2 Size { get; set; } public abstract Boolean RevX { get; set; } public abstract Boolean RevY { get; set; } public abstract Boolean Fill { get; set; } public abstract Color Color { get; set; } public abstract void Draw(Graphics g); public abstract PBForm NewInstance(PBForm form); public abstract PBForm NewInstance(); } }
Vector2

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace SelfMadePaint { public class Vector2 { public float X { get; set; } public float Y { get; set; } public Vector2(float x, float y) { X = x; Y = y; } public Vector2() { X = 0; Y = 0; } public Vector2 NewInstance() { return new Vector2(X, Y); } public static explicit operator Vector2(System.Drawing.Point p) { return new Vector2(p.X, p.Y); } public static explicit operator System.Drawing.Point(Vector2 vec) { return new System.Drawing.Point((int)vec.X, (int)vec.Y); } public static Vector2 operator +(Vector2 vec1, Vector2 vec2) { return new Vector2(vec1.X + vec2.X, vec1.Y + vec2.Y); } public static Vector2 operator -(Vector2 vec1, Vector2 vec2) { return new Vector2(vec1.X - vec2.X, vec1.Y - vec2.Y); } public static Vector2 operator *(Vector2 vec1, Vector2 vec2) { return new Vector2(vec1.X * vec2.X, vec1.Y * vec2.Y); } public static Vector2 operator /(Vector2 vec1, Vector2 vec2) { return new Vector2(vec1.X / vec2.X, vec1.Y / vec2.Y); } public override bool Equals(object obj) { return ((obj as Vector2).X == X && (obj as Vector2).Y == Y); } public Vector2 GetLNeighbor() { return new Vector2(X - 1, Y); } public Vector2 GetRNeighbor() { return new Vector2(X + 1, Y); } public Vector2 GetTNeighbor() { return new Vector2(X, Y + 1); } public Vector2 GetBNeighbor() { return new Vector2(X, Y - 1); } } }
PBCircle

using System; using System.Collections.Generic; using System.Drawing; using System.Linq; using System.Text; namespace SelfMadePaint { public class PBCircle : PBForm { public override Vector2 FirstPosition { get; set; } public override Vector2 SecondPosition { get; set; } public override Vector2 Size { get { return SecondPosition - FirstPosition; } set { } } public override Color Color { get; set; } public override bool Fill { get; set; } public override bool RevX { get; set; } public override bool RevY { get; set; } public PBCircle() { } public PBCircle(Vector2 firstPosition, Vector2 size, Color color, bool fill) { FirstPosition = firstPosition; Size = size; Color = color; Fill = fill; } public override void Draw(Graphics g) { if (Fill) g.FillEllipse(new SolidBrush(Color), FirstPosition.X, FirstPosition.Y, Size.X, Size.Y); else g.DrawEllipse(new Pen(Color), FirstPosition.X, FirstPosition.Y, Size.X, Size.Y); } public override string ToString() { return "Circle"; } public override PBForm NewInstance(PBForm form) { return new PBCircle(form.FirstPosition, form.Size, form.Color, form.Fill); } public override PBForm NewInstance() { return new PBCircle(); } } }
PBLine

using System; using System.Collections.Generic; using System.Drawing; using System.Linq; using System.Text; namespace SelfMadePaint { public class PBLine : PBForm { public override Vector2 FirstPosition { get; set; } public override Vector2 SecondPosition { get; set; } public override Vector2 Size { get; set; } public override Color Color { get; set; } public override bool Fill { get; set; } public override bool RevX { get; set; } public override bool RevY { get; set; } public PBLine() { } public PBLine(Vector2 firstPosition, Vector2 size, Color color) { FirstPosition = firstPosition; Size = size; Color = color; } public override void Draw(Graphics g) { g.DrawLine(new Pen(Color), (Point)FirstPosition, (Point)SecondPosition); } public override string ToString() { return "Line"; } public override PBForm NewInstance(PBForm form) { return new PBLine(form.FirstPosition, form.Size, form.Color); } public override PBForm NewInstance() { return new PBLine(); } } }
PBRectangle

using System; using System.Collections.Generic; using System.Drawing; using System.Linq; using System.Text; namespace SelfMadePaint { public class PBRectangle : PBForm { public override Vector2 FirstPosition { get; set; } public override Vector2 SecondPosition { get; set; } public override Vector2 Size { get; set; } public override Color Color { get; set; } public override bool Fill { get; set; } public override bool RevX { get; set; } public override bool RevY { get; set; } public PBRectangle() { } public PBRectangle(Vector2 firstPosition, Vector2 secondPosition, Color color, bool fill) { FirstPosition = firstPosition; SecondPosition = secondPosition; Color = color; Fill = fill; } public override void Draw(Graphics g) { Vector2 newSize = SecondPosition - FirstPosition; Vector2 newFirstposition = FirstPosition.NewInstance(); if (SecondPosition.X < FirstPosition.X) { newFirstposition.X = SecondPosition.X; newSize.X = Math.Abs(newSize.X); } if (SecondPosition.Y < FirstPosition.Y) { newFirstposition.Y = SecondPosition.Y; newSize.Y = Math.Abs(newSize.Y); } if (Fill) g.FillRectangle(new SolidBrush(Color), newFirstposition.X, newFirstposition.Y, newSize.X, newSize.Y); else g.DrawRectangle(new Pen(Color), newFirstposition.X, newFirstposition.Y, newSize.X, newSize.Y); } public override string ToString() { return "Rectangle"; } public override PBForm NewInstance(PBForm form) { return new PBRectangle(form.FirstPosition, form.Size, form.Color, form.Fill); } public override PBForm NewInstance() { return new PBRectangle(); } } }