Sunday, March 29, 2015

minggu5: Visual studio C# - program Paint

Dasar Teori
Mouse adalah salah satu input komputer yang sangat penting. Mouse
sangat dibutuhkan untuk berinteraksi dengan GUI aplikasi Windows.
Dengan mouse, user dapat mengarahkan kursor, menge-klik maupun
menge-drag sebuah object. Menekan, melepaskan tombol mouse
maupun menggerakkan kursor mouse akan menghasilkan event.
Pada praktikum ini kita mencoba memanfaatkan event-event mouse
tersebut untuk menggambar grafik. Untuk menggambar pada form,
atau object lain sebagai base menggambar, kita akan menggunakan
object Graphics. Di dalam object ini terdapat beberapa method untuk
menggambar text, garis, persegi panjang dan elips.

Paint adalah suatu aplikasi untuk menggambar, dan disini saya akan mencoba untuk membuat aplikasi sederhana seperti layaknya aplikasi paint paint.

tapi sebelum itu, saya akan memulai dengan dengan praktik berikut:
1. buka visual studio
2.pada praktikum ini toolbox yang digunakan tidak ada, karna yang digunakan sekarang adalah hanya event event nya saja, tapi setelah ini kita tetap menggunakan toolbox.
 









3.masukan kode nya

4.lalu running program!


Baiklah setelah selesai praktikum tadi, saya akan mencoba membuat aplikasi paint sederana
1.masih membuka visual studio
2.lalu desain program, toolbox yang digunakan disini yaitu button, groupbox, textbox, panel


3.masukan kodingan nya
ini adalah kode programnya
namespace Program_Paint
{
    public partial class Form1 : Form
    {
        Graphics ObjGraphic;
        private double xy;
        int a = 0, b = 0, warna = 0;
        private Boolean shouldPaint = false;
        int cX, cY, x, y, bX, bY;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            ObjGraphic = panel1.CreateGraphics();
        }

        private void panel1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                shouldPaint = true;
                b++;
                cX = e.X;
                cY = e.Y;
            }
        }

        private void panel1_MouseMove(object sender, MouseEventArgs e)
        {
            textBox1.Text = Convert.ToString(bX);
            textBox2.Text = Convert.ToString(bY);
            xy = Math.Sqrt((bX * bX) + (bY * bY));
            textBox3.Text = Convert.ToString(xy);
        }

        private void panel1_MouseClick(object sender, MouseEventArgs e)
        {
            if (shouldPaint == true)
            {
                x = e.X;
                y = e.Y;
                bX = e.X - cX;
                bY = cY - e.Y;

                if (a == 1)
                {
                    if (warna == 1)
                    {
                        ObjGraphic.DrawLine(new Pen(Color.Black), cX, cY, e.X, e.Y);
                    }
                    else if (warna == 2)
                    {
                        ObjGraphic.DrawLine(new Pen(Color.Red), cX, cY, e.X, e.Y);
                    }
                    else if (warna == 3)
                    {
                        ObjGraphic.DrawLine(new Pen(Color.Blue), cX, cY, e.X, e.Y);
                    }
                    else if (warna == 4)
                    {
                        ObjGraphic.DrawLine(new Pen(Color.Yellow), cX, cY, e.X, e.Y);
                    }
                    else if (warna == 5)
                    {
                        ObjGraphic.DrawLine(new Pen(Color.Purple), cX, cY, e.X, e.Y);
                    }
                    else if (warna == 6)
                    {
                        ObjGraphic.DrawLine(new Pen(Color.Gray), cX, cY, e.X, e.Y);
                    }
                    else if (warna == 7)
                    {
                        ObjGraphic.DrawLine(new Pen(Color.Green), cX, cY, e.X, e.Y);
                    }
                    else if (warna == 8)
                    {
                        ObjGraphic.DrawLine(new Pen(Color.Pink), cX, cY, e.X, e.Y);
                    }
                    else if (warna == 9)
                    {
                        ObjGraphic.DrawLine(new Pen(Color.Orange), cX, cY, e.X, e.Y);
                    }
                    else if (warna == 10)
                    {
                        ObjGraphic.DrawLine(new Pen(Color.Aqua), cX, cY, e.X, e.Y);
                    }
                    else { MessageBox.Show("Choose Colour!"); }
                }

                else if (a == 2)
                {
                    if (warna == 1)
                    {
                        ObjGraphic.DrawRectangle(new Pen(Color.Black), cX, cY, e.X, e.Y);
                    }
                    else if (warna == 2)
                    {
                        ObjGraphic.DrawRectangle(new Pen(Color.Red), cX, cY, e.X, e.Y);
                    }
                    else if (warna == 3)
                    {
                        ObjGraphic.DrawRectangle(new Pen(Color.Blue), cX, cY, e.X, e.Y);
                    }
                    else if (warna == 4)
                    {
                        ObjGraphic.DrawRectangle(new Pen(Color.Yellow), cX, cY, e.X, e.Y);
                    }
                    else if (warna == 5)
                    {
                        ObjGraphic.DrawRectangle(new Pen(Color.Purple), cX, cY, e.X, e.Y);
                    }
                    else if (warna == 6)
                    {
                        ObjGraphic.DrawRectangle(new Pen(Color.Gray), cX, cY, e.X, e.Y);
                    }
                    else if (warna == 7)
                    {
                        ObjGraphic.DrawRectangle(new Pen(Color.Green), cX, cY, e.X, e.Y);
                    }
                    else if (warna == 8)
                    {
                        ObjGraphic.DrawRectangle(new Pen(Color.Pink), cX, cY, e.X, e.Y);
                    }
                    else if (warna == 9)
                    {
                        ObjGraphic.DrawRectangle(new Pen(Color.Orange), cX, cY, e.X, e.Y);
                    }
                    else if (warna == 10)
                    {
                        ObjGraphic.DrawRectangle(new Pen(Color.Aqua), cX, cY, e.X, e.Y);
                    }
                    else { MessageBox.Show("Choose Colour!"); }

                }

                else if (a == 3)
                {
                    if (warna == 1)
                    {
                        ObjGraphic.DrawEllipse(new Pen(Color.Black), cX, cY, e.X, e.Y);
                    }
                    else if (warna == 2)
                    {
                        ObjGraphic.DrawEllipse(new Pen(Color.Red), cX, cY, e.X, e.Y);
                    }
                    else if (warna == 3)
                    {
                        ObjGraphic.DrawEllipse(new Pen(Color.Blue), cX, cY, e.X, e.Y);
                    }
                    else if (warna == 4)
                    {
                        ObjGraphic.DrawEllipse(new Pen(Color.Yellow), cX, cY, e.X, e.Y);
                    }
                    else if (warna == 5)
                    {
                        ObjGraphic.DrawEllipse(new Pen(Color.Purple), cX, cY, e.X, e.Y);
                    }
                    else if (warna == 6)
                    {
                        ObjGraphic.DrawEllipse(new Pen(Color.Gray), cX, cY, e.X, e.Y);
                    }
                    else if (warna == 7)
                    {
                        ObjGraphic.DrawEllipse(new Pen(Color.Green), cX, cY, e.X, e.Y);
                    }
                    else if (warna == 8)
                    {
                        ObjGraphic.DrawEllipse(new Pen(Color.Pink), cX, cY, e.X, e.Y);
                    }
                    else if (warna == 9)
                    {
                        ObjGraphic.DrawEllipse(new Pen(Color.Orange), cX, cY, e.X, e.Y);
                    }
                    else if (warna == 10)
                    {
                        ObjGraphic.DrawEllipse(new Pen(Color.Aqua), cX, cY, e.X, e.Y);
                    }

                    else { MessageBox.Show("Choose Colour!"); }
                }
                else { MessageBox.Show("Choose Object!"); }
            }
        }

        private void black_Click(object sender, EventArgs e)
        {
            warna = 1;
            black.FlatStyle = FlatStyle.Popup;
        }

        private void red_Click(object sender, EventArgs e)
        {
            warna = 2;
            red.FlatStyle = FlatStyle.Popup;
        }

        private void kotak_Click_1(object sender, EventArgs e)
        {
            a = 2;
            garis.BackColor = Color.White;
            kotak.BackColor = Color.Orange;
            elips.BackColor = Color.White;
        }

        private void elips_Click(object sender, EventArgs e)
        {
            a = 3;
            garis.BackColor = Color.White;
            kotak.BackColor = Color.White;
            elips.BackColor = Color.Orange;
        }

        private void garis_Click_1(object sender, EventArgs e)
        {
            a = 1;
            garis.BackColor = Color.Orange;
            kotak.BackColor = Color.White;
            elips.BackColor = Color.White;
        }

        private void green_Click(object sender, EventArgs e)
        {
            warna = 7;
            green.FlatStyle = FlatStyle.Popup;
        }

        private void gray_Click(object sender, EventArgs e)
        {
            warna = 6;
            gray.FlatStyle = FlatStyle.Popup;
        }

        private void pink_Click(object sender, EventArgs e)
        {
            warna = 8;
            pink.FlatStyle = FlatStyle.Popup;
        }

        private void orange_Click(object sender, EventArgs e)
        {
            warna = 9;
            orange.FlatStyle = FlatStyle.Popup;
        }

        private void blue_Click(object sender, EventArgs e)
        {
            warna = 3;
            blue.FlatStyle = FlatStyle.Popup;
        }

        private void purple_Click(object sender, EventArgs e)
        {
            warna = 5;
            purple.FlatStyle = FlatStyle.Popup;
        }

        private void yellow_Click(object sender, EventArgs e)
        {
            warna = 4;
            yellow.FlatStyle = FlatStyle.Popup;
        }

        private void aqua_Click(object sender, EventArgs e)
        {
            warna = 10;
            aqua.FlatStyle = FlatStyle.Popup;
        }

    }
}

4.lalu run programnya

baiklah saya rasa cukup sekian, apabila ada kesalahan saya mohon maaf. semoga bermanfaat gan!!

No comments:

Post a Comment