Hello ENGINEER, Welcome to Electrical's Corner

Maret 29, 2015

Tutorial Membuat Aplikasi "Paint" Menggunakan Visual Studio 2012 (C#)

Tutor : Program Aplikasi "Paint" di VS 2012 (c#)

Assalamualaikum Wr.Wb

hai teman-teman, masih semangat kan yah belajar programnya???????????? ayo belajar lagi!!! sekarang nih lebih greget programnya. temen-temen pasti tau kan aplikasi paint?? ituloh yang ada si start-accesories-paint :D paint itu secara umum adalah tempat uuntuk membuat gambar. jadi, kalau diprogram namanya "Graphic" nah, luas banget kan program kan yah??? ayo kita belajar!!!
kalau gitu, begini langkah-langkah membuat aplikasinya :) pastinya buka ms. visual studio 2012 dulu yaa dan jangan lupa beri nama. pilih yg windows form guys!.

1. Klik Toolbox dan pilih tools nya, kemudian susun seperti ini.
x adalah garis horizontal dan y adalah garis vertikal,
Panjang adalah panjang garis yang telah kita buat.
untuk tulisan X, Y, PANJANG laras pakai label
untuk kotak warna merah, kuning, biru, laras memakai button dan mengganti backcolour nya di properties.
untuk kotak berupa gambar garis, elips dan kotak laras memakai button dan mengubah backimage nya.

2. jika sudah disusun semua, masukkan program ini:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace tugaaas_5
{
    public partial class Form1 : Form
    {
        private Graphics laras  ;
        private double nilaixy;
        private int a = 0, b = 0, warna = 0;
        private int cX, cY, x, y, bX, bY;
        private bool picture = false, tarik = false;
        public Form1()
        {
            InitializeComponent();
        }

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

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

        private void button7_Click(object sender, EventArgs e)
        {
            this.Refresh();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            warna = 1;
            button1.FlatStyle = FlatStyle.Popup;
            button3.FlatStyle = FlatStyle.Standard;
            button2.FlatStyle = FlatStyle.Standard;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            warna = 2;
            button1.FlatStyle = FlatStyle.Standard;
            button3.FlatStyle = FlatStyle.Popup;
            button2.FlatStyle = FlatStyle.Standard;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            warna = 3;
            button1.FlatStyle = FlatStyle.Standard;
            button3.FlatStyle = FlatStyle.Standard;
            button2.FlatStyle = FlatStyle.Popup;
        }

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

                if (a == 1)
                {
                    if (warna == 1)
                    {
                        laras.DrawLine(new Pen(Color.Red), cX, cY, e.X, e.Y);
                    }
                    else if (warna == 2)
                    {
                        laras.DrawLine(new Pen(Color.Blue), cX, cY, e.X, e.Y);
                    }
                    else if (warna == 3)
                    {
                        laras.DrawLine(new Pen(Color.Yellow), cX, cY, e.X, e.Y);
                    }
                    else { MessageBox.Show("PILIH WARNA"); }
                }

                else if (a == 2)
                {
                    if (warna == 1)
                    {
                        laras.DrawRectangle(new Pen(Color.Red), cX, cY, e.X, e.Y);
                    }
                    else if (warna == 2)
                    {
                        laras.DrawRectangle(new Pen(Color.Blue), cX, cY, e.X, e.Y);
                    }
                    else if (warna == 3)
                    {
                        laras.DrawRectangle(new Pen(Color.Yellow), cX, cY, e.X, e.Y);
                    }

                    else { MessageBox.Show("PILIH WARNA"); }

                }

                else if (a == 3)
                {
                    if (warna == 1)
                    {
                        laras.DrawEllipse(new Pen(Color.Red), cX, cY, e.X, e.Y);
                    }
                    else if (warna == 2)
                    {
                        laras.DrawEllipse(new Pen(Color.Blue), cX, cY, e.X, e.Y);
                    }
                    else if (warna == 3)
                    {
                        laras.DrawEllipse(new Pen(Color.Yellow), cX, cY, e.X, e.Y);
                    }

                    else { MessageBox.Show("PILIH WARNA"); }
                }
                else { MessageBox.Show("PILIH BENTUKNYA"); }
            }
        }

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

        private void button4_Click(object sender, EventArgs e)
        {
            
            a = 1;
            button4.BackColor = Color.Orange;
            button6.BackColor = Color.White;
            button5.BackColor = Color.White;
        }

        private void button5_Click(object sender, EventArgs e)
        {
            
            a = 3;
            button4.BackColor = Color.Orange;
            button6.BackColor = Color.White;
            button5.BackColor = Color.White;
        }

        private void button6_Click(object sender, EventArgs e)
        {
            
            a = 2;
            button4.BackColor = Color.White;
            button6.BackColor = Color.Orange;
            button5.BackColor = Color.White;
        }
    }
}

3. running kan programnya :)

jika kita belum memilih bentuk tapi sudah memilih warna, akan muncul messege box seperti ini :

jika belum memilih warna tapi sudah memilih bentuk, maka akan muncul seperti ini:

sekian tutorial dari ayas,,, semoga bermanfaat :)

wassalamualaikum wr.wb




Tidak ada komentar:

Posting Komentar