Minggu, 29 Maret 2015

Graphics pada Visual Studio

///HELLO\\\

Salam hangat blogger sekalian,
saya disini akan menunjukkan penggunaan graphic pada visual studio
I.Membuat paint kecil kecil''an

1.Buka Visual Studio/SharpDevelop

2.Pilih Windows Form Application

3.lalu buat lah objek berupa panel atau picturebox(dapat diperoleh dari ToolBox)



4.Pada Global,letakkan deklarasi object graphics berikut

 private Graphics Object;

5.Di Event Form load,kita beri instance object-nya. 

Object = CreateGraphics();

NOTE:Untuk class Graphics, kita tidak bisa langsung mendeklarasikan lalu membuat instance-nya. Karena
method CreateGraphics tidak bisa digunakan langsung ketika kita  mendeklarasikan Graphics. 


 6.Gambar akan mulai terbentuk jika kita tekan mouse kiri,maka kita pilih di EVENT di PROPETIES
kilk 2x pd OnMouseDown



lalu tambahkan kodingan ini

if (e.Button == MouseButtons.Left)
{
  Paint = true;
}

NOTE:Variabel Paint adalah Boolean,maka kita harus mendeklarasikan nya terlebih dahulu di variabel Global agar dpt diakses semua

private shouldPaint = false;


7.Lalu jika ingin menggambar saat mouse digerakkan,maka kita harus mengubah event pada panel/picture box. Kita harus ke source code untuk event OnMouseMove.

Klik ganda,lalu masukkan kodingan ini

Object.FillEllipse(new SolidBrush(Color.Black), e.X, e.Y, Diameter, Diameter);

NOTE:Koding di atas adalah untuk menggambar elips dengan menggunakan brush berwarna Hitam, di koordinat event X dan event Y, tinggi elips sebesar Diameter, lebar elips sebesar Diameter (juga lingkaran).

8.Agar kita bisa berhenti menggambar ketika tombol mouse dilepas, kita
harus menambahkan program di event OnMouseUp.Pada EVENT

lalu beri kodingan berikut

void MainFormMouseUp(object sender, MouseEventArgs e)

  Paint = false;
}


9.Start Program/Debug




II.Membuat Paint sederhana dengan informasi panjang dan letak objek

Oke Agan agan sekalian,jika sudah paint kecil-kecilan,sekarang kita Advanced dikit cerita nya,disini saya membuat paint dengan informasi panjang dan letak objek,yang mana kita harus memasukkan kodingan Method Drawline 

objGraphic.DrawLine(new Pen(Color.Black), x1, y1, x2, y2);

NOTE:Object Pen sama seperti SolidBrush, hanya saja Pen menampilkan titik satu piksel saja. Gambar diletakkan di object panel. Agar dapat menggambar di panel, ada sedikit perbedaan untuk membuat instance Graphics:
                    objGraphic = panel1.CreateGraphics();

Karena saya sudah membuat program nya,maka langsung saja saya beri tampilan program saya...





Dapat kita lihat program saya terdiri dari 2 Form yang mana:

1.Form Loading
  • 2 label
  • 1 PictureBox
  • 1 ProgressBar
2.Form Utama
  • 11 Button(6 button warna,5 button pilihan )
  • 2 Groupbox
  • 3 Textbox
  • 3 Label
  • 1 Picture Box
 Dan berikut kodingannya

I.Kodingan untuk Form Loading
 
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 WindowsFormsApplication15
{
    public partial class Form4 : Form
    {
        public Form4()
        {
            InitializeComponent();
        }

        private void progressBar1_Click(object sender, EventArgs e)
        {

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (progressBar1.Value < 100)
            {
                progressBar1.Value += 3;
            }

            else if (progressBar1.Value == 100);
            {
                timer1.Stop();
                Form1 form = new Form1();
                form.Show();
                this.Hide();
//jika progress bar sudah 100,maka form ini hilang dan menuju form 1 
           }
        }

        private void Form4_Load(object sender, EventArgs e)
        {
            timer1.Enabled = true;
        }
    }
}

II.Kodingan di Form utama
 
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;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;

namespace WindowsFormsApplication15
{
    public partial class Form1 : Form
    {
        private Graphics Paint;
        private double xy;
        private int a = 0, b = 0, colour = 0;
        private int qX, qY, x, y, wX, wY;
        private bool Picture = false, pull = false;


        public Form1()
        {
            InitializeComponent();
        }

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

        private void pictureBox1_Click(object sender, EventArgs e)
        {

        }



        private void button6_Click(object sender, EventArgs e)
        {
            colour = 1;
            button6.FlatStyle = FlatStyle.Popup;
            button7.FlatStyle = FlatStyle.Standard;
            button5.FlatStyle = FlatStyle.Standard; //red
            button8.FlatStyle = FlatStyle.Standard;
            button10.FlatStyle = FlatStyle.Standard;
            button11.FlatStyle = FlatStyle.Standard;

        }

        private void button7_Click(object sender, EventArgs e)
        {
            colour = 2;
            button6.FlatStyle = FlatStyle.Standard;
            button7.FlatStyle = FlatStyle.Popup;
            button5.FlatStyle = FlatStyle.Standard; //green
            button8.FlatStyle = FlatStyle.Standard;
            button10.FlatStyle = FlatStyle.Standard;
            button11.FlatStyle = FlatStyle.Standard;
        }

        private void button5_Click_1(object sender, EventArgs e)
        {
            colour = 3;
            button6.FlatStyle = FlatStyle.Standard;
            button7.FlatStyle = FlatStyle.Standard;
            button5.FlatStyle = FlatStyle.Popup; //blue
            button8.FlatStyle = FlatStyle.Standard;
            button10.FlatStyle = FlatStyle.Standard;
            button11.FlatStyle = FlatStyle.Standard;
        }

        private void button8_Click(object sender, EventArgs e)
        {
            colour = 4;
            button6.FlatStyle = FlatStyle.Standard;
            button7.FlatStyle = FlatStyle.Standard;
            button5.FlatStyle = FlatStyle.Standard;
            button8.FlatStyle = FlatStyle.Popup; //pink
            button10.FlatStyle = FlatStyle.Standard;
            button11.FlatStyle = FlatStyle.Standard;
        }

        private void button10_Click(object sender, EventArgs e)
        {
            colour = 5;
            button6.FlatStyle = FlatStyle.Standard;
            button7.FlatStyle = FlatStyle.Standard;
            button5.FlatStyle = FlatStyle.Standard;
            button8.FlatStyle = FlatStyle.Standard;
            button10.FlatStyle = FlatStyle.Popup; //black
            button11.FlatStyle = FlatStyle.Standard;
        }

        private void button11_Click(object sender, EventArgs e)
        {
            colour = 6;
            button6.FlatStyle = FlatStyle.Standard;
            button7.FlatStyle = FlatStyle.Standard;
            button5.FlatStyle = FlatStyle.Standard;
            button8.FlatStyle = FlatStyle.Standard;
            button10.FlatStyle = FlatStyle.Standard;
            button11.FlatStyle = FlatStyle.Popup; //Yellow
        }

        private void groupBox2_Enter(object sender, EventArgs e)
        {

        }

        private void button5_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            a = 1;
            button1.BackColor = Color.Black; //Line
            button2.BackColor = Color.White;
            button3.BackColor = Color.White;
        }


        private void button2_Click(object sender, EventArgs e)
        {
            a = 2;
            button1.BackColor = Color.White;
            button2.BackColor = Color.Yellow;
            button3.BackColor = Color.White;
            //rectangle
        }



        private void button3_Click(object sender, EventArgs e)
        {
          
            a = 3;
            button1.BackColor = Color.Orange;
            button2.BackColor = Color.White; //ellipse
            button3.BackColor = Color.White;
        }


        private void button12_Click(object sender, EventArgs e)
        {
            MessageBox.Show("SEE YA!");
            this.Close();
        }

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                Picture = true;
                b++;
            }
            qX = e.X;
            qY = e.Y;
            pull = true;
        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
         

            textBox1.Text = Convert.ToString(wX);
            textBox2.Text = Convert.ToString(wY);
            xy = Math.Sqrt((wX * wX) + (wY * wY));
            textBox3.Text = Convert.ToString(xy);
        }

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



        private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
        {
            if (Picture == true)
            {
                x = e.X;
                y = e.Y;
                wX = e.X - qX;
                wY = qY - e.Y;

                if (a == 1)
                {
                    if (colour == 1)
                    {
                        Paint.DrawLine(new Pen(Color.Red), qX, qY, e.X, e.Y);
                    }
                    else if (colour == 2)
                    {
                        Paint.DrawLine(new Pen(Color.Blue), qX, qY, e.X, e.Y);
                    }
                    else if (colour == 3)
                    {
                        Paint.DrawLine(new Pen(Color.Green), qX, qY, e.X, e.Y);
                    }
                    else if (colour == 4)
                    {
                        Paint.DrawLine(new Pen(Color.Pink), qX, qY, e.X, e.Y);
                    }
                    else if (colour == 5)
                    {
                        Paint.DrawLine(new Pen(Color.Black), qX, qY, e.X, e.Y);
                    }
                    else if (colour == 6)
                    {
                        Paint.DrawLine(new Pen(Color.Yellow), qX, qY, e.X, e.Y);
                    }
                    else { MessageBox.Show("Choose your color!!"); }
                }

                else if (a == 2)
                {
                    if (colour == 1)
                    {
                        Paint.DrawRectangle(new Pen(Color.Red), qX, qY, e.X, e.Y);
                    }
                    else if (colour == 2)
                    {
                        Paint.DrawRectangle(new Pen(Color.Blue), qX, qY, e.X, e.Y);
                    }
                    else if (colour == 3)
                    {
                        Paint.DrawRectangle(new Pen(Color.Green), qX, qY, e.X, e.Y);
                    }
                    else if (colour == 4)
                    {
                        Paint.DrawRectangle(new Pen(Color.Pink), qX, qY, e.X, e.Y);
                    }
                    else if (colour == 5)
                    {
                        Paint.DrawRectangle(new Pen(Color.Black), qX, qY, e.X, e.Y);
                    }
                    else if (colour == 6)
                    {
                        Paint.DrawRectangle(new Pen(Color.Yellow), qX, qY, e.X, e.Y);
                    }
                }
                else if (a == 3)
                {
                    if (colour == 1)
                    {
                        Paint.DrawEllipse(new Pen(Color.Red), qX, qY, e.X, e.Y);
                    }
                    else if (colour == 2)
                    {
                        Paint.DrawEllipse(new Pen(Color.Blue), qX, qY, e.X, e.Y);
                    }
                    else if (colour == 3)
                    {
                        Paint.DrawEllipse(new Pen(Color.Green), qX, qY, e.X, e.Y);
                    }
                    else if (colour == 4)
                    {
                        Paint.DrawEllipse(new Pen(Color.Pink), qX, qY, e.X, e.Y);
                    }
                    else if (colour == 5)
                    {
                        Paint.DrawEllipse(new Pen(Color.Black), qX, qY, e.X, e.Y);
                    }
                    else if (colour == 6)
                    {
                        Paint.DrawEllipse(new Pen(Color.Yellow), qX, qY, e.X, e.Y);
                    }
                }
            }
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }
    }
}


Ini adalah video penjelasan dari saya
 

Baiklah mungkin sekian dari saya,maaf jika kurang jelas,typo dsb 
Terima Kasih

+++SALAM KOMPAK!!!+++

1 komentar:

  1. Casino near you, Palm Springs, CA - Mapyro
    The casino at Harrah's Southern California is 통영 출장안마 a 상주 출장마사지 three-level hotel 충주 출장샵 with a 충청북도 출장안마 casino and 통영 출장안마 a restaurant. The casino has 645 rooms in all of its

    BalasHapus