Blog from Saravanan Arumugam

Let us talk about Technologies

Conventional Graphics Implementation in .Net framework 2.0


In today’s world, microsoft .Net support very enhanced UI implementations through WPF. Yet .Net framework 2.0 has given a lot of good classes that support standard graphics operations.
I had to go through the Graphics features provided in framework 2.0, since I am preparing for MCTS exam, Microsoft .Net Framework – Application Development Foundation.
 
I tried to do an animation that would create a standard 4 point polygan, and keep the vertices move so that a random polygon is shown all the time.
I have used a timer to draw the polygon in a picture box. Trigger and Clear buttons are used to Start and Stop the timer respectively. Timer’s Elapsed event calls the method that draws the polygon.
 
The windows application layout looks like this.
 
The code behind this is, (I find it difficult to format the code properly. So leave it with the default format that the site does.)

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using System.Drawing.Drawing2D;

 

namespace Graphics_Example

{

    public partial class Graphics_Animation : Form

    {

        System.Timers.Timer t;

        PolygonApex[] pa;

 

        public Graphics_Animation()

        {

            InitializeComponent();

        }

 

        private void Graphics_Animation_Load(object sender, EventArgs e)

        {

            t = new System.Timers.Timer();

            t.Elapsed += new System.Timers.ElapsedEventHandler(t_Elapsed);

 

            pa = new PolygonApex[4];

            pa[0] = new PolygonApex(new Point(50, 1100), -1, 1,

                     GraphicsPictureBox.ClientRectangle);

            pa[1] = new PolygonApex(new Point(100, 1), -1, -1,

                     GraphicsPictureBox.ClientRectangle);

            pa[2] = new PolygonApex(new Point(120, 10), 1, 1,

                     GraphicsPictureBox.ClientRectangle);

            pa[3] = new PolygonApex(new Point(310, 75), 1, -1,

GraphicsPictureBox.ClientRectangle);

        }

 

        private void TriggerButton_Click(object sender, EventArgs e)

        {

            t.Interval = 10;

            t.Start();

        }

 

        void t_Elapsed(object sender, System.Timers.ElapsedEventArgs e)

        {

            DrawPolygon();

        }

 

        private void DrawPolygon()

        {

            Graphics g = GraphicsPictureBox.CreateGraphics();

            Brush b = new LinearGradientBrush(GraphicsPictureBox.ClientRectangle,

              Color.Green, Color.Orange, LinearGradientMode.ForwardDiagonal);

 

            Pen p = new Pen(b, 2);

 

            List<Point> points = new List<Point>();

 

            foreach (PolygonApex apex in pa)

            {

                apex.MovePoint();

                points.Add(apex.Point);

            }

            this.SuspendLayout();

            g.Clear(GraphicsPictureBox.BackColor);

            g.DrawPolygon(p, points.ToArray());

            this.ResumeLayout();

        }

 

        private class PolygonApex

        {

            public PolygonApex(Point p, int xDirection, int yDirection,

Rectangle boundary)

            {

                this.point = p;

                this.xDirection = xDirection;

                this.yDirection = yDirection;

                this.Boundary = boundary;

            }

 

            private Point point;

            public int xDirection { get; set; }

            public int yDirection { get; set; }

            public Rectangle Boundary { get; set; }

 

            public Point Point

            {

                get { return point; }

                set { point = value; }

            }

 

            public void MovePoint()

            {

                this.point.X += xDirection;

                if (this.point.X > Boundary.Right)

                {

                    this.point.X = Boundary.Right;

                    xDirection *= -1;

                }

                else if (this.point.X < Boundary.Left)

                {

                    this.point.X = Boundary.Left;

                    xDirection *= -1;

                }

 

                this.point.Y += yDirection;

                if (this.point.Y > Boundary.Bottom)

                {

                    this.point.Y = Boundary.Bottom;

                    yDirection *= -1;

                }

                else if (this.point.Y < Boundary.Top)

                {

                    this.point.Y = Boundary.Top;

                    yDirection *= -1;

                }

            }

        }

 

        private void ClearButton_Click(object sender, EventArgs e)

        {

            t.Stop();

            Graphics g = GraphicsPictureBox.CreateGraphics();

            g.Clear(GraphicsPictureBox.BackColor);

        }

 

        private void Graphics_Animation_Deactivate(object sender, EventArgs e)

        {

            t.Stop();

            Graphics g = GraphicsPictureBox.CreateGraphics();

            g.Clear(GraphicsPictureBox.BackColor);

        }

    }

}

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: