-------------------------------------------父类形状类-------------------------------------------
1 using System; 2 using System.Collections.Generic; 3 using System.Drawing; 4 using System.Linq; 5 using System.Text; 6 using System.Threading.Tasks; 7 8 namespace J4.entity 9 { 10 public class Shape 11 { 12 public virtual void Draw(Graphics g, Pen p) 13 { 14 } 15 } 16 }
-------------------------------------------子类圆形类-------------------------------------------
1 using System; 2 using System.Collections.Generic; 3 using System.Drawing; 4 using System.Linq; 5 using System.Text; 6 using System.Threading.Tasks; 7 8 namespace J4.entity 9 { 10 public class Circle:Shape 11 { 12 public override void Draw(Graphics g, Pen p) 13 { 14 g.DrawEllipse(p, 75,150,100,100); 15 } 16 17 18 } 19 }
-------------------------------------------子类矩形类-------------------------------------------
1 using System; 2 using System.Collections.Generic; 3 using System.Drawing; 4 using System.Linq; 5 using System.Text; 6 using System.Threading.Tasks; 7 8 namespace J4.entity 9 { 10 public class Rect:Shape 11 { 12 public override void Draw(Graphics g, Pen p) 13 { 14 Rectangle rec = new Rectangle(20, 100, 200, 200); 15 g.DrawRectangle(p, rec); 16 } 17 } 18 }
-------------------------------------------主窗体类-------------------------------------------
1 using J4.entity; 2 using System; 3 using System.Collections.Generic; 4 using System.ComponentModel; 5 using System.Data; 6 using System.Drawing; 7 using System.Linq; 8 using System.Text; 9 using System.Threading.Tasks; 10 using System.Windows.Forms; 11 12 namespace J4 13 { 14 public partial class frmMain : Form 15 { 16 public frmMain() 17 { 18 InitializeComponent(); 19 } 20 21 private void btnDraw_Click(object sender, EventArgs e) 22 { 23 //Graphics g = CreateGraphics(); 24 ////(颜色,厚度) 25 //Pen p = new Pen(Color.Red,3); 26 ////(X轴坐标,Y轴坐标,宽,高) 27 //Rectangle rec = new Rectangle(50,100,200,200); 28 ////矩形 29 //g.DrawRectangle(p,rec); 30 ////圆形 31 //g.DrawEllipse(p,50,100,100,100); 32 33 34 35 Shape shape = new Shape(); 36 Graphics g = this.panel1.CreateGraphics(); 37 //默认红色 38 Pen p = new Pen(Color.Red, 3); 39 40 //判断选中的是什么形状 41 if (rbtnCircle.Checked == true) 42 { 43 shape = new Circle(); 44 } 45 else 46 { 47 shape = new Rect(); 48 } 49 //判断选中的是什么颜色 50 if (rbtnBlue.Checked == true) 51 { 52 p = new Pen(Color.Blue, 3); 53 } 54 else if (rbtnYellow.Checked == true) 55 { 56 p = new Pen(Color.Yellow, 3); 57 } 58 //调用方法 59 shape.Draw(g,p); 60 61 } 62 63 private void panel1_Paint(object sender, PaintEventArgs e) 64 { 65 } 66 } 67 }