Click here to Skip to main content
15,886,110 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 104.9K   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;
using System.IO; 						//streamer io
using System.Runtime.Serialization;     // io
using System.Runtime.Serialization.Formatters.Binary; // io


namespace _2dGrph
{
	/// <summary>
	/// Summary description for WinForm1.
	/// </summary>
	public class animator2 : canvas
	{
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.IContainer components;
		private System.Windows.Forms.Panel panel2;
		private System.Windows.Forms.CheckBox checkBox1;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.Button button5;
		private System.Windows.Forms.Button button3;
		private System.Windows.Forms.NumericUpDown numericUpDown1;
		Anim anim;
		private System.Windows.Forms.ToolBar toolBar2;
		private System.Windows.Forms.ToolBarButton toolBarButton1;
		private System.Windows.Forms.ImageList imageList2;
		private System.Windows.Forms.ToolBarButton toolBarButton2;

		public animator2()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();
			anim = new Anim((GrpLst)g_l);
			//
			// 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);
		}

		#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(animator2));
			this.panel2 = new System.Windows.Forms.Panel();
			this.checkBox1 = new System.Windows.Forms.CheckBox();
			this.label3 = new System.Windows.Forms.Label();
			this.button5 = new System.Windows.Forms.Button();
			this.button3 = new System.Windows.Forms.Button();
			this.numericUpDown1 = new System.Windows.Forms.NumericUpDown();
			this.toolBar2 = new System.Windows.Forms.ToolBar();
			this.toolBarButton1 = new System.Windows.Forms.ToolBarButton();
			this.toolBarButton2 = new System.Windows.Forms.ToolBarButton();
			this.imageList2 = new System.Windows.Forms.ImageList(this.components);
			this.panel2.SuspendLayout();
			((System.ComponentModel.ISupportInitialize)(this.numericUpDown1)).BeginInit();
			this.SuspendLayout();
			// 
			// pictureBox1
			// 
			this.pictureBox1.Name = "pictureBox1";
			this.pictureBox1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseUp);
			// 
			// pictureBox2
			// 
			this.pictureBox2.Name = "pictureBox2";
			this.pictureBox2.MouseUp += new System.Windows.Forms.MouseEventHandler(this.pictureBox2_MouseUp);
			// 
			// panel2
			// 
			this.panel2.Controls.Add(this.checkBox1);
			this.panel2.Controls.Add(this.label3);
			this.panel2.Controls.Add(this.button5);
			this.panel2.Controls.Add(this.button3);
			this.panel2.Controls.Add(this.numericUpDown1);
			this.panel2.Location = new System.Drawing.Point(304, 32);
			this.panel2.Name = "panel2";
			this.panel2.Size = new System.Drawing.Size(192, 48);
			this.panel2.TabIndex = 1;
			// 
			// checkBox1
			// 
			this.checkBox1.Location = new System.Drawing.Point(136, 8);
			this.checkBox1.Name = "checkBox1";
			this.checkBox1.Size = new System.Drawing.Size(80, 40);
			this.checkBox1.TabIndex = 28;
			this.checkBox1.Text = "Zoom/Rot all frames";
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(8, 8);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(56, 16);
			this.label3.TabIndex = 27;
			this.label3.Text = "Frame";
			// 
			// button5
			// 
			this.button5.BackColor = System.Drawing.Color.LightGray;
			this.button5.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
			this.button5.Location = new System.Drawing.Point(64, 24);
			this.button5.Name = "button5";
			this.button5.Size = new System.Drawing.Size(64, 20);
			this.button5.TabIndex = 26;
			this.button5.Text = "Animate";
			this.button5.Click += new System.EventHandler(this.button5_Click);
			// 
			// button3
			// 
			this.button3.BackColor = System.Drawing.Color.LightGray;
			this.button3.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
			this.button3.Location = new System.Drawing.Point(64, 0);
			this.button3.Name = "button3";
			this.button3.Size = new System.Drawing.Size(64, 20);
			this.button3.TabIndex = 25;
			this.button3.Text = "Store";
			this.button3.Click += new System.EventHandler(this.button3_Click);
			// 
			// numericUpDown1
			// 
			this.numericUpDown1.Location = new System.Drawing.Point(0, 24);
			this.numericUpDown1.Name = "numericUpDown1";
			this.numericUpDown1.ReadOnly = true;
			this.numericUpDown1.Size = new System.Drawing.Size(64, 20);
			this.numericUpDown1.TabIndex = 24;
			this.numericUpDown1.Click += new System.EventHandler(this.numericUpDown1_Click);
			// 
			// toolBar2
			// 
			this.toolBar2.Anchor = System.Windows.Forms.AnchorStyles.Top;
			this.toolBar2.Buttons.AddRange(new System.Windows.Forms.ToolBarButton[] {
						this.toolBarButton1,
						this.toolBarButton2});
			this.toolBar2.ButtonSize = new System.Drawing.Size(22, 22);
			this.toolBar2.Cursor = System.Windows.Forms.Cursors.Hand;
			this.toolBar2.Dock = System.Windows.Forms.DockStyle.None;
			this.toolBar2.DropDownArrows = true;
			this.toolBar2.ImageList = this.imageList2;
			this.toolBar2.Location = new System.Drawing.Point(376, 0);
			this.toolBar2.Name = "toolBar2";
			this.toolBar2.ShowToolTips = true;
			this.toolBar2.Size = new System.Drawing.Size(48, 28);
			this.toolBar2.TabIndex = 20;
			this.toolBar2.ButtonClick += new System.Windows.Forms.ToolBarButtonClickEventHandler(this.toolBar2_ButtonClick);
			// 
			// toolBarButton1
			// 
			this.toolBarButton1.ImageIndex = 1;
			this.toolBarButton1.Tag = "0";
			this.toolBarButton1.ToolTipText = "Load animation";
			// 
			// toolBarButton2
			// 
			this.toolBarButton2.ImageIndex = 0;
			this.toolBarButton2.Tag = "1";
			this.toolBarButton2.ToolTipText = "Save animation";
			// 
			// imageList2
			// 
			this.imageList2.ImageSize = new System.Drawing.Size(16, 16);
			this.imageList2.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList2.ImageStream")));
			this.imageList2.TransparentColor = System.Drawing.Color.Transparent;
			// 
			// animator2
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(544, 360);
			this.Controls.Add(this.toolBar2);
			this.Controls.Add(this.panel2);
			this.Name = "animator2";
			this.Text = "aniamtor2";
			this.Load += new System.EventHandler(this.animator2_Load);
			this.Controls.SetChildIndex(this.pictureBox1, 0);
			this.Controls.SetChildIndex(this.pictureBox2, 0);
			this.Controls.SetChildIndex(this.panel2, 0);
			this.Controls.SetChildIndex(this.toolBar2, 0);
			this.panel2.ResumeLayout(false);
			((System.ComponentModel.ISupportInitialize)(this.numericUpDown1)).EndInit();
			this.ResumeLayout(false);
		}
		#endregion
		
		private void animator2_Load(object sender, System.EventArgs e)
		{
			
		}
		
		private void button3_Click(object sender, System.EventArgs e)
		{
				  GrpLst a = new GrpLst();
				  //a.list = (ArrayList)g_l.list.Clone(); do not works, it copies the references
				  a.list = (ArrayList)g_l.CopyList();
				  anim.setFrame((int)numericUpDown1.Value,a);
				  if (numericUpDown1.Value <numericUpDown1.Maximum)
				  {
					  numericUpDown1.Value++;
					  getFrame();
				  }

		}
		
		private void button5_Click(object sender, System.EventArgs e)
		{
			animate anime = new animate();
			anime.MdiParent = this.MdiParent;
			anime.anim = this.anim;
			if (this.loadImage)
			{
				anime.bkImg = this.loadImg;
				anime.loadImage = true;
				anime.visibleImage = this.visibleImage;
			}
			anime.Show();

		}

		private void numericUpDown1_Click(object sender, System.EventArgs e)
		{
			getFrame();
		}


		#region Fileacces
		public new bool  Loader()
		{
			try
			{
				Stream StreamRead;
				OpenFileDialog DialogueCharger = new OpenFileDialog();
				DialogueCharger.DefaultExt = "anim";
				DialogueCharger.Title = "Lave animation";
				DialogueCharger.Filter = "animation files (*.anim)|*.anim|All files (*.*)|*.*" ;
				if( DialogueCharger.ShowDialog() == DialogResult.OK )
				{
					if( (StreamRead = DialogueCharger.OpenFile())!= null )
					{
						BinaryFormatter BinaryRead = new BinaryFormatter();
						anim = (Anim) BinaryRead.Deserialize(StreamRead);
						g_l = (ExtGrpLst) anim.Ori;
						StreamRead.Close();
						return true;
					}
				}
			}
			catch {}
			return false;
		}

		public new bool  Saver()
		{
			try
			{
				Stream StreamWrite;
				SaveFileDialog DialogueSauver = new SaveFileDialog();
				DialogueSauver.DefaultExt = "anim";
				DialogueSauver.Title = "Save animation";
				DialogueSauver.Filter = "animation files (*.anim)|*.anim|All files (*.*)|*.*" ;
				if( DialogueSauver.ShowDialog() == DialogResult.OK )
				{
					if( (StreamWrite = DialogueSauver.OpenFile()) != null )
					{
						BinaryFormatter BinaryWrite = new BinaryFormatter();
						BinaryWrite.Serialize(StreamWrite, anim);
						StreamWrite.Close();
						return true;
					}
				}
			}
			catch {}
			return false;
		}
		#endregion


		private void getFrame()
		{
			GrpLst a;
			a =  anim.getFrame((int) numericUpDown1.Value);
			if (!(a==null))
			{
				g_l.Clear();
				//g_l.list = (ArrayList)a.list.Clone();
				g_l.list = a.CopyList();
				redraw();
			}

		}

		private void toolBar2_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
		{
			if (e.Button.Tag.Equals("1"))//save
			{
				if (Saver()) {		   }
			}
			if (e.Button.Tag.Equals("0"))//load
			{
				if (Loader()) {		  }
			}

		}

		private new void pictureBox2_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
		{
			moving_g_l.Clear();
			startSize = false;
			//Point c = new Point((int) (pictureBox2.Width/2),(int) (pictureBox2.Height/2));
			float dx = (float)(2*sizePoint.X)/pictureBox2.Width;
			float dy = (float)(2*sizePoint.Y)/pictureBox2.Height;
			//
			Matrix myMatrix = new Matrix();
			if (g_l.almostOneSelected) // selected objects
			{
				// zoom//
				myMatrix.Translate(centerPoint.X,centerPoint.Y);
				myMatrix.Scale(dx,dy);
				myMatrix.Translate(-centerPoint.X,-centerPoint.Y);
				// transform code : end
				if (checkBox1.Checked)
				{
					//g_l.TransformaLL(myMatrix);
					anim.allTransform(myMatrix);
					getFrame();
				}
				else
					g_l.Transform(myMatrix);

			}
			myMatrix.Dispose();
			redraw();

		}


		private new void pictureBox1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
		{
			startAngle = false;
			moving_g_l.Clear();
			//
			Point c = new Point((int) (pictureBox1.Width/2),(int) (pictureBox1.Height/2));
			float dx = anglePoint.X-c.X;
			float dy = anglePoint.Y-c.X;
			float b= 0f;
			float alpha= 0f;
			float f= 0f;
			if ((dx>0)&(dy>0))
			{
				b= 90;
				alpha= (float)Math.Abs((Math.Atan((double)(dy/dx)) * (180/Math.PI)));
			}
			else
				if ((dx<=0)&(dy>=0))
				{
					b= 180;
					alpha= (float)Math.Abs((Math.Atan((double)(dx/dy)) * (180/Math.PI)));
				}
				else
					if ((dx<0)&(dy<0))
					{
						b= 270;
						alpha= (float)Math.Abs((Math.Atan((double)(dy/dx)) * (180/Math.PI)));
					}
					else
					{
						b= 0;
						alpha= (float)Math.Abs((Math.Atan((double)(dx/dy)) * (180/Math.PI)));
					}
			f = (b+ alpha);
			//
			Matrix myMatrix = new Matrix();
			if (g_l.almostOneSelected) // selected objects
			{
				// rotate//
				myMatrix.RotateAt(f,centerPoint);
				// transform code : end

				if (checkBox1.Checked)
				{
					//g_l.TransformaLL(myMatrix);
					anim.allTransform(myMatrix);
					getFrame();
				}
				else
					g_l.Transform(myMatrix);
			}
			myMatrix.Dispose();
			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