Pemrograman GDI+ dengan C# (Part 2)

Graphic Class Method

Draw Method

Digunakan untuk menggambar garis, rectangle, dan ellipse

Drawing Lines

Method DrawLine digunakan untuk menggambar garis yang menghubungkan dua titik.

 

Contoh2_4 Menggambar Garis

private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            Pen redPen = new Pen(Color.Red, 1);
            Pen bluePen = new Pen(Color.Blue, 2);
            Pen greenPen = new Pen(Color.Green, 3);
            Pen blackPen = new Pen(Color.Black, 4);
            // Draw line using float coordinates
            float x1 = 20.0F, y1 = 20.0F;
            float x2 = 200.0F, y2 = 20.0F;
            g.DrawLine(redPen, x1, y1, x2, y2);
            // Draw line using Point structure
            Point pt1 = new Point(20, 20);
            Point pt2 = new Point(20, 200);
            g.DrawLine(greenPen, pt1, pt2);
            // Draw line using PointF structure
            PointF ptf1 = new PointF(20.0F, 20.0F);
            PointF ptf2 = new PointF(200.0F, 200.0F);
            g.DrawLine(bluePen, ptf1, ptf2);
            // Draw line using integer coordinates
            int X1 = 60, Y1 = 40, X2 = 250, Y2 = 100;
            g.DrawLine(blackPen, X1, Y1, X2, Y2);
            // Dispose of objects
            redPen.Dispose();
            bluePen.Dispose();
            greenPen.Dispose();
            blackPen.Dispose();
        }

Contoh2_5 Menggambar Garis yang Terhubung

private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            Pen redPen = new Pen(Color.Red, 1);
            PointF[] ptsArray ={
                new PointF( 20.0F, 20.0F),
                new PointF( 20.0F, 200.0F),
                new PointF(200.0F, 200.0F),
                new PointF(20.0F, 20.0F)};
            g.DrawLines(redPen, ptsArray);
        }

Drawing Rectangle

Bentuk dasar selanjutnya adalah rectangle. Jika anda ingin menggambar rectangle maka yang harus diperhatikan adalah titik awal / starting point, width, dan height-nya.

Contoh2_6 Menggambar Rectangle

 

private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            // Create pens and points
            Pen redPen = new Pen(Color.Red, 1);
            Pen bluePen = new Pen(Color.Blue, 2);
            Pen greenPen = new Pen(Color.Green, 3);
            float x = 5.0F, y = 5.0F;
            float width = 100.0F;
            float height = 200.0F;
            // Create a rectangle
            Rectangle rect = new Rectangle(20, 20, 80, 40);
            // Draw rectangles
            g.DrawRectangle(bluePen,
              x, y, width, height);
            g.DrawRectangle(redPen,
              60, 80, 140, 50);
            g.DrawRectangle(greenPen, rect);
            // Dispose of objects
            redPen.Dispose();
            bluePen.Dispose();
            greenPen.Dispose();
        }

Contoh 2_7 Menggambar Rectangle Berurutan / Series Mode

private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            Pen greenPen = new Pen(Color.Green, 4);
            RectangleF[] rectArray =
            {
                new RectangleF( 5.0F, 5.0F, 100.0F, 200.0F),
                new RectangleF(20.0F, 20.0F, 80.0F, 40.0F),
                new RectangleF(60.0F, 80.0F, 140.0F, 50.0F)
            };
            g.DrawRectangles(greenPen, rectArray);
            greenPen.Dispose();
        } 

Drawing Ellipses dan Circle

Bentuk ellipses adalah bentuk circular didalam bentuk rectangle. Bentuk ellipses mempunyai titik tengah / center point.

image

Contoh2_8 Menggambar Ellipses

 

private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            // Create pens
            Pen redPen = new Pen(Color.Red, 6);
            Pen bluePen = new Pen(Color.Blue, 4);
            Pen greenPen = new Pen(Color.Green, 2);
            // Create a rectangle
            Rectangle rect =
              new Rectangle(80, 80, 50, 50);
            // Draw ellipses
            g.DrawEllipse(greenPen,
              100.0F, 100.0F, 10.0F, 10.0F);
            g.DrawEllipse(redPen, rect);
            g.DrawEllipse(bluePen, 60, 60, 90, 90);
            g.DrawEllipse(greenPen,
              40.0F, 40.0F, 130.0F, 130.0F);
            // Dispose of objects
            redPen.Dispose();
            greenPen.Dispose();
            bluePen.Dispose();
        }

Drawing Text

Digunakan untuk menggambar text string dalam graphic surfaces

Contoh2_9 Menggambar String

private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            // Create brushes
            SolidBrush blueBrush = new SolidBrush(Color.Blue);
            SolidBrush redBrush = new SolidBrush(Color.Red);
            SolidBrush greenBrush = new SolidBrush(Color.Green);
            // Create a rectangle
            Rectangle rect = new Rectangle(20, 20, 200, 100);
            // The text to be drawn
            String drawString = "Hello GDI+ World!";
            // Create a Font object
            Font drawFont = new Font("Verdana", 14);
            float x = 100.0F;
            float y = 100.0F;
            // String format
            StringFormat drawFormat = new StringFormat();
            // Set string format flag to direction vertical,
            // which draws text vertically
            drawFormat.FormatFlags =
              StringFormatFlags.DirectionVertical;
            // Draw string
            
            g.DrawString("Drawing text",
              new Font("Tahoma", 14), greenBrush, rect);
            g.DrawString(drawString,
              new Font("Arial", 12), redBrush, 120, 140);
            g.DrawString(drawString, drawFont,
              blueBrush, x, y, drawFormat);
            // Dispose of objects
            blueBrush.Dispose();
            redBrush.Dispose();
            greenBrush.Dispose();
            drawFont.Dispose();
        }

Drawing Arc

Arc adalah bagian dari ellipse, yang dibentuk dari area rectangle, start angle, dan sweep angle. Cara penggunaan Arc adalah sebagai berikut:

Contoh2_11 Menggambar Arc

Buat design form sebagai berikut:

image

Buat variabel awal untuk deklarasi awal start dan sweep angle

private void Form1_Paint(object sender, PaintEventArgs e)
    {
        Graphics g = e.Graphics;
        Pen redPen = new Pen(Color.Red, 3);
        Rectangle rect = new Rectangle(20, 20, 200, 200);
        g.DrawArc(redPen, rect, startAngle, sweepAngle);
        redPen.Dispose();
    }
    private void btnReset_Click(object sender, EventArgs e)
    {
        startAngle =(float)Convert.ToDouble(txtStart.Text);
        sweepAngle =(float)Convert.ToDouble(txtSweep.Text);
        Invalidate();
    }

Drawing Curves

Curve terdiri dari dua macam yaitu open dan close curve

image

Contoh2_12 Drawing Curves

private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            // Create a pen
            Pen bluePen = new Pen(Color.Blue, 1);
            // Create an array of points
            PointF pt1 = new PointF(40.0F, 50.0F);
            PointF pt2 = new PointF(50.0F, 75.0F);
            PointF pt3 = new PointF(100.0F, 115.0F);
            PointF pt4 = new PointF(200.0F, 180.0F);
            PointF pt5 = new PointF(200.0F, 90.0F);
            PointF[] ptsArray =
            {
                pt1, pt2, pt3, pt4, pt5
            };
            // Draw curve
            g.DrawCurve(bluePen, ptsArray);
            // Dispose of object
            bluePen.Dispose();
        }

Contoh2_13 Drawing Curves dengan Tension

private float tension = 0.5F;

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            // Create a pen
            Pen bluePen = new Pen(Color.Blue, 1);
            // Create an array of points
            PointF pt1 = new PointF(40.0F, 50.0F);
            PointF pt2 = new PointF(50.0F, 75.0F);
            PointF pt3 = new PointF(100.0F, 115.0F);
            PointF pt4 = new PointF(200.0F, 180.0F);
            PointF pt5 = new PointF(200.0F, 90.0F);
            PointF[] ptsArray =
            {
                pt1, pt2, pt3, pt4, pt5
            };
            // Draw curve
            g.DrawCurve(bluePen, ptsArray,tension);
            // Dispose of object
            bluePen.Dispose();
        }

        private void btnApply_Click(object sender, EventArgs e)
        {
            tension = (float)Convert.ToDouble(txtTension.Text);
            Invalidate();
        }

Contoh2_14 Drawing Closed Curve

private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            // Create a pen
            Pen bluePen = new Pen(Color.Blue, 1);
            // Create an array of points
            PointF pt1 = new PointF(40.0F, 50.0F);
            PointF pt2 = new PointF(50.0F, 75.0F);
            PointF pt3 = new PointF(100.0F, 115.0F);
            PointF pt4 = new PointF(200.0F, 180.0F);
            PointF pt5 = new PointF(200.0F, 90.0F);
            PointF[] ptsArray =
            {
                pt1, pt2, pt3, pt4, pt5
            };
            // Draw curve
            g.DrawClosedCurve(bluePen, ptsArray);
            // Dispose of object
            bluePen.Dispose();
        }

Drawing Polygon

Polygon adalah bentuk yang terdiri dari tiga atau lebih garis, misal: segitiga, dan kotak.

Contoh2_15 Drawing Polygon

private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            // Create pens
            Pen greenPen = new Pen(Color.Green, 2);
            Pen redPen = new Pen(Color.Red, 2);
            // Create points for polygon
            PointF p1 = new PointF(40.0F, 50.0F);
            PointF p2 = new PointF(60.0F, 70.0F);
            PointF p3 = new PointF(80.0F, 34.0F);
            PointF p4 = new PointF(120.0F, 180.0F);
            PointF p5 = new PointF(200.0F, 150.0F);
            PointF[] ptsArray =
            {
                p1, p2, p3, p4, p5
            };
            // Draw polygon
            g.DrawPolygon(greenPen, ptsArray);
            // Dispose of objects
            greenPen.Dispose();
            redPen.Dispose();
        }

Drawing Graphic Path

Graphic Path menghubungkan beberapa objek drawing seperti line, rectable, circle, dll.

Contoh2_16 Drawing Graphic Path

using System.Drawing;
using System.Drawing.Drawing2D;

private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            // Create a pen
            Pen greenPen = new Pen(Color.Green, 1);
            // Create a graphics path
            GraphicsPath path = new GraphicsPath();
            // Add a line to the path
            path.AddLine(20, 20, 103, 80);
            // Add an ellipse to the path
            path.AddEllipse(100, 50, 100, 100);
            // Add three more lines
            path.AddLine(195, 80, 300, 80);
            path.AddLine(200, 100, 300, 100);
            path.AddLine(195, 120, 300, 120);
            // Create a rectangle and call
            // AddRectangle
            Rectangle rect =
              new Rectangle(50, 150, 300, 50);
            path.AddRectangle(rect);
            // Draw path
            g.DrawPath(greenPen, path);
            // Dispose of object
            greenPen.Dispose();
        }

Drawing Pie Shapes

image

Contoh2_17 Drawing Pie Chart

image

private void btnDraw_Click(object sender, EventArgs e)
        {
            // Create a Graphics object
            Graphics g = this.CreateGraphics();
            g.Clear(this.BackColor);
            // Get the current value of start and sweep
            // angles
            float startAngle =
              (float)Convert.ToDouble(textBox1.Text);
            float sweepAngle =
              (float)Convert.ToDouble(textBox2.Text);
            // Create a pen
            Pen bluePen = new Pen(Color.Blue, 1);
            // Draw pie
            g.DrawPie(bluePen, 20, 20, 100, 100,
              startAngle, sweepAngle);
            // Dispose of objects
            bluePen.Dispose();
            g.Dispose();
        }

 

bersambung Part 3…..

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