Tuesday, May 20, 2014

Intro to Graphics


namespace Intro_To_Graphic
{

    //Drawing Geometrical shapes; lines, rectangles, circles,triangles...
    //.Net provides classes for drawing and filling.
    //Classes needed: Graphics class, Pen class, Brush class, struct Point
    //To draw you need to:
    //      1. Create a graphic object
    //      2. Create a Pen
    //      3. Draw the shape
   //To fill you need to:
    //      1. Create a graphics object
    //      2. Create a Brush
    //      3. Fill the shape

    //Notes: We can use either PictureBox or Panel to draw graphic pictures

    public partial class Form1 : Form
    {
        /*
         * To draw on a picturebox, you need to create 
         * a Bitmap. Do all the drawing on the bitmap
         * then put the bitmap onto the pictureBox
         * ============================================
         * Step 1: is to create a bitmap with same dimension as the picturebox
         * ============================================
         * Step 2: is to create a Graphics object for the bitmap
         * ============================================
         * Step 3: Figure out the points and the color 
         * ============================================
         * Step 4: Draw the shape
         */// ============================================

        //declare a bitmap
        Bitmap bmap;

        //declare a Graphic object
        Graphics g;

        //Random object 

        Random rand = new Random();
        public Form1()
        {
            InitializeComponent();
            bmap = new Bitmap(pictureBox1.Width, pictureBox1.Height);

            g = Graphics.FromImage(bmap);
        }
        //helper methods
        //method to return a random 
        private Point GetRandomPoint(Bitmap bitmap)
        {
            int x = rand.Next(bitmap.Width);
            int y = rand.Next(bitmap.Height);
            Point p = new Point(x, y);
            return p;
        }

        //method to return random color
        private Color GetRandomColor()
        {
            int red = rand.Next(256); //0 - 255)
            int green = rand.Next(256); //0 - 255)
            int blue = rand.Next(256); //0 - 255)

            Color c = Color.FromArgb(red, green, blue);
            return c;
        }

        private void btnDrawLine_Click(object sender, EventArgs e)
        {
            int lineThickness = rand.Next(1, 7);
            Color color = GetRandomColor();
            Pen pen = new Pen(color, lineThickness);
            Point p1 = GetRandomPoint(bmap);
            Point p2 = GetRandomPoint(bmap);
            g.DrawLine(pen, p1, p2);
         
            //place the bmap 
            pictureBox1.Image = bmap;    
        }

        private void btnDrawRectangle_Click(object sender, EventArgs e)
        {
            //need a pen, x, y, widgth, height
            Point p = GetRandomPoint(bmap);
            int x = p.X;
            int y = p.Y;
            //set width, height;
            int width = rand.Next(bmap.Width - x);
            int height = rand.Next(bmap.Height - y);

            //generate a random color
            Color color = GetRandomColor();
         
            //create pen
            int lineThickness = rand.Next(1, 7);
            Pen pen = new Pen(color, lineThickness);
            //draw rectangle
            g.DrawRectangle(pen, x, y, width, height);
            pictureBox1.Image = bmap;
        }

        private void btnDrawEllipse_Click(object sender, EventArgs e)
        {
            Point p = GetRandomPoint(bmap);
            int x = p.X;
            int y = p.Y;

            //define width and height
            int width = rand.Next(bmap.Width - x);
            int height = rand.Next(bmap.Height - y);

            //generate a random color
            Color color = GetRandomColor();

            //create a Pen
            int lineThickness = rand.Next(1, 7);
            Pen pen = new Pen(color, lineThickness);

            g.DrawEllipse(pen, x, y, width, height);
            pictureBox1.Image = bmap;
        }

        private void btnDrawCircle_Click(object sender, EventArgs e)
        {
            Point p = GetRandomPoint(bmap);
            int x = p.X;
            int y = p.Y;

            //define width and height
            int width = rand.Next(bmap.Width - x);
            int height = rand.Next(bmap.Height - y);
            int diameter = Math.Min(width, height);

            //generate a random color
            Color color = GetRandomColor();

            //create a Pen
            int lineThickness = rand.Next(1, 7);
            Pen pen = new Pen(color, lineThickness);

            g.DrawEllipse(pen, x, y, diameter, diameter);
            pictureBox1.Image = bmap;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            bmap = new Bitmap(pictureBox1.Width, pictureBox1.Height);
            g = Graphics.FromImage(bmap);
            pictureBox1.Image = bmap;
        }

        private void btnFillRectangle_Click(object sender, EventArgs e)
        {
            //need a pen, x, y, widgth, height
            Point p = GetRandomPoint(bmap);
            int x = p.X;
            int y = p.Y;
            //set width, height;
            int width = rand.Next(bmap.Width - x);
            int height = rand.Next(bmap.Height - y);

            //create solidBrush
            Color color = GetRandomColor();
            SolidBrush brush = new SolidBrush(color);

            g.FillRectangle(brush, x, y, width, height);
            pictureBox1.Image = bmap;
        }

        private void btnFillEllipse_Click(object sender, EventArgs e)
        {
            Point p = GetRandomPoint(bmap);
            int x = p.X;
            int y = p.Y;

            //define width and height
            int width = rand.Next(bmap.Width - x);
            int height = rand.Next(bmap.Height - y);

            //create solidBrush
            Color color = GetRandomColor();
            SolidBrush brush = new SolidBrush(color);

            g.FillEllipse(brush, x, y, width, height);
            pictureBox1.Image = bmap;
        }

        private void btnfillCircle_Click(object sender, EventArgs e)
        {
            Point p = GetRandomPoint(bmap);
            int x = p.X;
            int y = p.Y;

            //define width and height
            int width = rand.Next(bmap.Width - x);
            int height = rand.Next(bmap.Height - y);
            int diameter = Math.Min(width, height);

            //create solidBrush
            Color color = GetRandomColor();
            SolidBrush brush = new SolidBrush(color);

            g.FillEllipse(brush, x, y, diameter, diameter);
            pictureBox1.Image = bmap;
        }



       int angle = 0;
        Point p1;
        Point p2;
        int r;
        Pen pen;

        private void Form1_Load(object sender, EventArgs e)
        {
            p1 = new Point(bmap.Width / 2, bmap.Height / 2);
            r = Math.Min(bmap.Width / 2, bmap.Height / 2);
            p2 = new Point(bmap.Width, bmap.Height / 2);
            pen = new Pen(Color.White);
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            g.DrawLine(pen, p1, p2);
            pictureBox1.Image = bmap;
            angle++;
            if (angle > 360)
            {
                angle = 0;
                pen = new Pen(GetRandomColor());
            }
            p2.X = (int)(r * Math.Cos(Math.PI * angle / 180));
            p2.Y = (int)(r * Math.Sin(Math.PI * angle / 180));

        }

        private void btnRotateline_Click(object sender, EventArgs e)
        {
            g.DrawLine(pen, p1, p2);
            pictureBox1.Image = bmap;
            angle++;
            if (angle > 360)
                angle = 0;
            p2.X = (int)(r * Math.Cos(Math.PI * angle / 180));
            p2.Y = (int)(r * Math.Sin(Math.PI * angle / 180));
        }

        private void btnDrawPie_Click(object sender, EventArgs e)
        {
            Point p = GetRandomPoint(bmap);
            int x = p.X;
            int y = p.Y;

            //define width and height
            int width = 1+rand.Next(10, bmap.Width - x);
            int height = 1+rand.Next(10, bmap.Height - y);
            int diameter = Math.Min(width, height);

            //create solidBrush
            Color color = GetRandomColor();
            Pen pen = new Pen(color, rand.Next(1, 5));

            int startAngle = rand.Next(91);
            int sweepAngle = rand.Next(25, 150);

            //draw pie 
            g.DrawPie(pen, x, y, diameter, diameter, startAngle, sweepAngle);
            pictureBox1.Image = bmap;

            //Readmore about angle on //http://msdn.microsoft.com/en-us/library/k0s75s5z(v=vs.110).aspx
        }

        private void btnFillPie_Click(object sender, EventArgs e)
        {
            Point p = GetRandomPoint(bmap);
         
            int x = p.X;
            int y = p.Y;

            //define width and height
            int width = 1+rand.Next(bmap.Width - x);
            int height = 1+rand.Next(bmap.Height - y);
            int diameter = Math.Min(width, height);

            //create solidBrush
            Color color = GetRandomColor();
            SolidBrush brush = new SolidBrush(color);

            int startAngle = rand.Next(-91, 91);
            int sweepAngle = rand.Next(25, 150);

            //draw pie 
            //g.DrawPie(pen, x, y, diameter, diameter, startAngle, sweepAngle);
            g.FillPie(brush, x, y, diameter, diameter, startAngle, sweepAngle);
            pictureBox1.Image = bmap;
        }

        private void btnDrawSixPieChart_Click(object sender, EventArgs e)
        {
            //Lab Assignment to do:
            //Draw a pie chart with 6 pies
            //Each with different color

            Point p = GetRandomPoint(bmap);
            int x = p.X;
            int y = p.Y;

            //define width and height
            int width = 100;
            int height = width;
            Rectangle rect = new Rectangle(x, y, width, height);
            //int diameter = Math.Min(width, height);

            SolidBrush brush = new SolidBrush(Color.AliceBlue);
            int startAngle = 90;
            int sweepAngle = 60;
            g.FillPie(brush, rect, startAngle, sweepAngle);

            brush = new SolidBrush(Color.Green);
            startAngle += sweepAngle;
            sweepAngle = 60;
            g.FillPie(brush, rect, startAngle, sweepAngle);

            brush = new SolidBrush(Color.Yellow);
            startAngle += sweepAngle;
            sweepAngle = 60;
            g.FillPie(brush, rect, startAngle, sweepAngle);

            brush = new SolidBrush(Color.Blue);
            startAngle += sweepAngle;
            sweepAngle = 60;
            g.FillPie(brush, rect, startAngle, sweepAngle);

            brush = new SolidBrush(Color.Red);
            startAngle += sweepAngle;
            sweepAngle = 60;
            g.FillPie(brush, rect, startAngle, sweepAngle);

            brush = new SolidBrush(Color.Orange);
            startAngle += sweepAngle;
            sweepAngle = 60;
            g.FillPie(brush, rect, startAngle, sweepAngle);

            pictureBox1.Image = bmap;
        }
    }
}

No comments:

Post a Comment