Click here to Skip to main content
15,897,291 members
Articles / Programming Languages / C#

2D Poligon Drawer-Animator

Rate me:
Please Sign up or sign in to vote.
4.57/5 (21 votes)
1 Aug 2004CPOL2 min read 105K   4.7K   52  
Draw poligons, manipulate them, and animate them.
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace _2dGrph
{



	/// <summary>
	/// Summary description for WinForm1.
	/// </summary>
	public class brushfrm : System.Windows.Forms.Form
	{
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.IContainer components;
		private System.Windows.Forms.Panel panel1;
		SelectRect s_r;
		//test
		GraphicsPath g_p;
		//GrpLst g_l;
		public Status status;
		private System.Windows.Forms.ColorDialog colorDialog1;
		private System.Windows.Forms.Button button4;
		private System.Windows.Forms.ImageList imageList1;
		private System.Windows.Forms.ToolBar toolBar1;
		private System.Windows.Forms.ToolBarButton toolBarButton1;
		private System.Windows.Forms.Button button1;

		public brushfrm()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			s_r = new SelectRect(this);
			//test
			g_p = new GraphicsPath();
			//g_l = new GrpLst();
			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose (bool disposing)
		{
			if (disposing)
			{
				if (components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose(disposing);
		}

		private void redraw()
		{
			Rectangle rect;
			Graphics g = panel1.CreateGraphics();


			//Do Double Buffering
			Bitmap offScreenBmp;
			Graphics offScreenDC;
			offScreenBmp = new Bitmap(panel1.Width, panel1.Height);
			offScreenDC = Graphics.FromImage(offScreenBmp);

			offScreenDC.Clear(panel1.BackColor);
			//offScreenDC.SmoothingMode=SmoothingMode.AntiAlias;


		/*	foreach (Wall w in cart.walls)
			{
				//myGraphics.DrawRectangle(new Pen(Color.Black, 1), w.X, w.Y, w.Wid, w.Hei	);
				rect = new Rectangle(w.X,w.Y,w.Wid,w.Hei);
				offScreenDC.FillRectangle(Brushes.Goldenrod,rect);

				//GraphicsPath path=new GraphicsPath();
				//path.AddEllipse(rect);
				//PathGradientBrush pgbrush= new PathGradientBrush(path);
				//pgbrush.CenterPoint=new Point((rect.Right- rect.Left) /3+rect.Left,(rect.Bottom - rect.Top) /3+rect.Top);
				//pgbrush.CenterColor=Color.White;
				//pgbrush.SurroundColors=new Color[] { Color.Red };
				//offScreenDC.FillRectangle(pgbrush,rect);
				//offScreenDC.DrawEllipse(new Pen(Color.Red),rect);

			}*/

			/*
			foreach (Node p in cart.ai_nodes)
			{
				rect = new Rectangle(p.P.X,p.P.Y,1,1);
				offScreenDC.FillRectangle(Brushes.Red,rect);
				offScreenDC.DrawEllipse(new Pen(Color.Black, 1),p.P.X-3,p.P.Y-3,6,6);
			}
			*/

			/*rect = new Rectangle(P1.X,P1.Y,2,2);
			offScreenDC.FillRectangle(Brushes.Blue,rect);
			offScreenDC.DrawEllipse(new Pen(Color.Blue, 1),P1.X-3,P1.Y-3,6,6);
			rect = new Rectangle(P2.X,P2.Y,2,2);
			offScreenDC.DrawEllipse(new Pen(Color.Red, 1),P2.X-3,P2.Y-3,6,6);
			offScreenDC.FillRectangle(Brushes.Blue,rect);
			*/

			if (s_r.visible)
			{
				// offScreenDC.DrawRectangle(new Pen(Color.Black, 1),s_r.p1.X,s_r.p1.Y,s_r.p2.X - s_r.p1.X,s_r.p2.Y - s_r.p1.Y);
				offScreenDC.DrawLine(new Pen(Color.Black, 1),s_r.p1.X,s_r.p1.Y,s_r.p1.X,s_r.p2.Y);
				offScreenDC.DrawLine(new Pen(Color.Black, 1),s_r.p1.X,s_r.p2.Y,s_r.p2.X,s_r.p2.Y);
				offScreenDC.DrawLine(new Pen(Color.Black, 1),s_r.p2.X,s_r.p2.Y,s_r.p2.X,s_r.p1.Y);
				offScreenDC.DrawLine(new Pen(Color.Black, 1),s_r.p2.X,s_r.p1.Y,s_r.p1.X,s_r.p1.Y);
			}

			//test
			offScreenDC.DrawPath(new Pen(colorDialog1.Color, 1),g_p);


			g.DrawImageUnscaled(offScreenBmp, 0, 0);
			}






		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.components = new System.ComponentModel.Container();
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(brushfrm));
			this.panel1 = new System.Windows.Forms.Panel();
			this.colorDialog1 = new System.Windows.Forms.ColorDialog();
			this.button4 = new System.Windows.Forms.Button();
			this.imageList1 = new System.Windows.Forms.ImageList(this.components);
			this.toolBar1 = new System.Windows.Forms.ToolBar();
			this.toolBarButton1 = new System.Windows.Forms.ToolBarButton();
			this.button1 = new System.Windows.Forms.Button();
			this.SuspendLayout();
			// 
			// panel1
			// 
			this.panel1.BackColor = System.Drawing.SystemColors.Window;
			this.panel1.Location = new System.Drawing.Point(8, 40);
			this.panel1.Name = "panel1";
			this.panel1.Size = new System.Drawing.Size(120, 112);
			this.panel1.TabIndex = 0;
			this.panel1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.panel1_MouseUp);
			this.panel1.Paint += new System.Windows.Forms.PaintEventHandler(this.panel1_Paint);
			this.panel1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.panel1_MouseMove);
			this.panel1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.panel1_MouseDown);
			// 
			// colorDialog1
			// 
			this.colorDialog1.FullOpen = true;
			this.colorDialog1.ShowHelp = true;
			// 
			// button4
			// 
			this.button4.Location = new System.Drawing.Point(88, 160);
			this.button4.Name = "button4";
			this.button4.Size = new System.Drawing.Size(40, 24);
			this.button4.TabIndex = 4;
			this.button4.Text = "color";
			this.button4.Click += new System.EventHandler(this.button4_Click);
			// 
			// imageList1
			// 
			this.imageList1.ImageSize = new System.Drawing.Size(16, 16);
			this.imageList1.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList1.ImageStream")));
			this.imageList1.TransparentColor = System.Drawing.Color.Transparent;
			// 
			// toolBar1
			// 
			this.toolBar1.Buttons.AddRange(new System.Windows.Forms.ToolBarButton[] {
						this.toolBarButton1});
			this.toolBar1.DropDownArrows = true;
			this.toolBar1.ImageList = this.imageList1;
			this.toolBar1.Location = new System.Drawing.Point(0, 0);
			this.toolBar1.Name = "toolBar1";
			this.toolBar1.ShowToolTips = true;
			this.toolBar1.Size = new System.Drawing.Size(136, 28);
			this.toolBar1.TabIndex = 5;
			this.toolBar1.ButtonClick += new System.Windows.Forms.ToolBarButtonClickEventHandler(this.toolBar1_ButtonClick);
			// 
			// toolBarButton1
			// 
			this.toolBarButton1.ImageIndex = 0;
			this.toolBarButton1.Tag = "0";
			this.toolBarButton1.ToolTipText = "Close figure";
			// 
			// button1
			// 
			this.button1.Location = new System.Drawing.Point(16, 160);
			this.button1.Name = "button1";
			this.button1.Size = new System.Drawing.Size(64, 24);
			this.button1.TabIndex = 6;
			this.button1.Text = "Set brush";
			this.button1.Click += new System.EventHandler(this.button1_Click1);
			// 
			// brushfrm
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.BackColor = System.Drawing.Color.RosyBrown;
			this.ClientSize = new System.Drawing.Size(136, 192);
			this.Controls.Add(this.button1);
			this.Controls.Add(this.toolBar1);
			this.Controls.Add(this.button4);
			this.Controls.Add(this.panel1);
			this.Name = "brushfrm";
			this.Text = "Brush";
			this.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.canvas_KeyPress);
			this.Load += new System.EventHandler(this.canvas_Load);
			this.ResumeLayout(false);
		}
		#endregion

		public void OnselectRect(object sender, selectRectEventArgs e)
		{

			//Add code to draw select rect
		}


		private void canvas_Load(object sender, System.EventArgs e)
		{

		}

		private void panel1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
		{
			s_r.setRect(e.X,e.Y);


			switch (status.Mode)
			{
			case Action.Select:
				{
				g_p.AddLine(s_r.p1,s_r.p2);
				break;
				}
			case Action.Rect:
				{
				g_p.AddRectangle( new Rectangle(s_r.p1.X,s_r.p1.Y,s_r.p2.X-s_r.p1.X ,s_r.p2.Y-s_r.p1.Y));
				break;
				}
			case Action.Ellip:
				{
				g_p.AddEllipse(new Rectangle(s_r.p1.X,s_r.p1.Y,s_r.p2.X-s_r.p1.X ,s_r.p2.Y-s_r.p1.Y));
				break;
				}
			}

			s_r.undoRect();     //stop drawing the rectangle

			redraw();
		}

		private void panel1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
		{
				   if (s_r.drawing)
					{
						s_r.p2.X = e.X;
						s_r.p2.Y = e.Y;
						redraw();
					}

		}

		private void panel1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
		{
			s_r.resetRect(e.X,e.Y);
		}
		
		private void panel1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
		{
			redraw();
		}

		private void button1_Click(object sender, System.EventArgs e)
		{
			//g_p.CloseFigure();
			redraw();
		}
		
		private void button2_Click(object sender, System.EventArgs e)
		{
			Matrix translateMatrix = new Matrix();
			translateMatrix.Translate(5, 0);
			//g_p.Transform(translateMatrix);
			redraw();

		}
		
		private void button3_Click(object sender, System.EventArgs e)
		{
			Matrix rotateMatrix = new Matrix();
			rotateMatrix.Rotate(45);
			//g_p.Transform(rotateMatrix);
			redraw();

		}
		
		private void button4_Click(object sender, System.EventArgs e)
		{
			colorDialog1.ShowDialog();
			button4.BackColor = colorDialog1.Color;
		}

		private void canvas_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
		{

		}
		
		private void toolBar1_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
		{
			if (e.Button.Tag == "0")
				g_p.CloseFigure();
				redraw();
		}
		
		private void button1_Click1(object sender, System.EventArgs e)
		{
		PathGradientBrush pgbrush= new PathGradientBrush(g_p);
		RectangleF rectf = g_p.GetBounds();
		Rectangle rect = new Rectangle();
		rect.X = (int)rectf.X;
		rect.Y = (int)rectf.Y;
		rect.Width = (int)rectf.Width;
		rect.Height = (int)rectf.Height;

		pgbrush.CenterPoint=new Point((rect.Right- rect.Left) /3+rect.Left,(rect.Bottom - rect.Top) /3+rect.Top);
		//pgbrush.CenterColor=Color.White;
		pgbrush.CenterColor=Color.Black;
		pgbrush.SurroundColors=new Color[] { colorDialog1.Color };
		//offScreenDC.FillRectangle(pgbrush,rect);
		status.pgbrush =pgbrush;
		redraw();
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Italy Italy
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions