Minggu, 29 Maret 2015

Aplikasi Paint sederhana menggunakan bahasa C#

        Hallo, kali ini saya akan membuat tutorial aplikasi Paint sederhana menggunakan software visual studio 2012 - C# windows form application
Berikut ini adalah tutorialnya ;

1. Create new project


   Next


2.     Mendesign tampilan form yang kita ingin buat


3. Programnya / Isi kodingnya

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 pain6
{
    public partial class Pain : Form
    {
        public Pain()
        {
            InitializeComponent();
        }

        /// <summary>
/// variable global
/// </summary>
int tebal = 1, initialX, initialY; 
Pen p;
Color wrn, wrn1;
private Graphics objGraphic;
private bool shouldPaint = false, warna = false;
Boolean line, rectang, circle, triangle;
double px, py, vector, angle ;

/// <summary>
/// vatiable line
/// </summary> 
private Point preCoor, newCoor;


/// <summary>
/// variable rectang
/// </summary>
int width, height;
        

/// <summary>
/// variable circle
/// </summary> 
int cirW, cirL;


/// <summary>
/// variable triangle
/// </summary>
private List<Point> points = new List<Point>();
Point a, b, c;
Point[] list = new Point[3];


/// <summary>
/// 
/// </summary>
/// <param name=”sender”></param>
/// <param name=”e”></param>

        private void panel1_Paint(object sender, PaintEventArgs e)
        {
            objGraphic = panel1.CreateGraphics();
        }

        private void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {
            tebal = (int)numericUpDown1.Value;
        }

        private void clear_Click(object sender, EventArgs e)
        {
            cleartext(); buttoncolor(); numericUpDown1.Value = 1;
        }

        /// <summary>
/// reset boolean of button
/// </summary>
void reset()
{
    line = false; rectang = false;
    circle = false; triangle = false;
}

/// <summary>
/// fungsi
/// </summary>
///
void colour()
{
    warna = true;
    p = new Pen(wrn);
    if (tebal == 0)
{
        DialogResult box;
        box = MessageBox.Show("Width input cannot 0 pixel", "Error", MessageBoxButtons.RetryCancel);
        if (box == DialogResult.Cancel)
{ this.Dispose(); }
}
}


/// <summary>
/// fungsi rumus-rumus
/// </summary>
void rumusline()
{
    px = newCoor.X; py = newCoor.Y;
    vector = Math.Sqrt((Math.Pow(px, 2)) + (Math.Pow(py, 2)));
    angle = Math.Atan(py / px) * 180 / Math.PI; display(); 
}


void rumusrectang()
{
    px = width; py = height;
    vector = px * py;
    if (rectang == true)
    { angle = 0; } display(); 
}

void rumuscircle()
{
    px = cirW; py = cirL;
    vector = Math.PI * 0.5 * (cirW + cirL);
    if (circle == true)
    { angle = 360; } display();
}

void rumustriangle ()
{
    px = b.X-a.X; py = c.Y;
    vector = 0.5 * px * py;
    if (triangle == true)
    { angle = 180; } display();
}


/// <summary>
/// tampilkan information
/// </summary>
void display()
{
    textBoxX.Text = Convert.ToString(px);
    textBoxY.Text = Convert.ToString(py);
    textBoxL.Text = vector.ToString("#.##");
    textBoxAng.Text = angle.ToString("#,0.00");
}


/// <summary>
/// clear text
/// </summary>
void cleartext()
{
    panel1.Invalidate();
    this.textBoxX.Clear(); this.textBoxY.Clear();
    this.textBoxL.Clear(); this.textBoxAng.Clear();
}


/// <summary>
/// buttoncolor
/// </summary>
void buttoncolor()
{
    Line.BackColor = Color.Snow; Rectang.BackColor = Color.Snow;
    Circle.BackColor = Color.Snow; Triagle.BackColor = Color.Snow;
}


/// <summary>
/// event mouse
/// </summary>
/// <param name=”sender”></param>
/// <param name=”e”></param>
private void panel1_MouseDown(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left && warna == true) 
{
    if (line == true)
{
    shouldPaint = true;
    preCoor = e.Location; newCoor = preCoor;
    panel1.Invalidate();
}

else if (rectang == true)
{
    shouldPaint = true;
    initialX = e.X; initialY = e.Y;
    panel1.Invalidate();
}

else if (circle == true)
{
    shouldPaint = true;
    initialX = e.X; initialY = e.Y;
    panel1.Invalidate(); 

else if (triangle == true)
    shouldPaint = true;
    Point a = e.Location; points.Add(a);
    panel1.Invalidate(); 
}
}
}

private void panel1_MouseMove(object sender, MouseEventArgs e)
{
    if (shouldPaint == true && warna == true) 
{
    if (line == true)
    ControlPaint.DrawReversibleLine(panel1.PointToScreen(preCoor), panel1.PointToScreen(newCoor), wrn1);
    newCoor = new Point(e.X, e.Y);
    ControlPaint.DrawReversibleLine(panel1.PointToScreen(preCoor), panel1.PointToScreen(newCoor), wrn1); 

else if (rectang == true)
    this.Refresh();
    p.Width = tebal;
    width = e.X - initialX;
    height = e.Y - initialY;
    Rectangle rect = new Rectangle(Math.Min(e.X, initialX),
    Math.Min(e.Y, initialY),
    Math.Abs(e.X - initialX),
    Math.Abs(e.Y - initialY));
    objGraphic.DrawRectangle(p, rect);
}

else if (circle == true)
    this.Refresh();
    p.Width = tebal; 
    cirW = Math.Abs(e.X - initialX);
    cirL = Math.Abs(e.Y - initialY);
    Rectangle rec = new Rectangle(Math.Min(e.X, initialX),
    Math.Min(e.Y, initialY),
    Math.Abs(e.X - initialX),
    Math.Abs(e.Y - initialY));
    objGraphic.DrawEllipse(p, rec);
}

else if (triangle == true) 
    Point a = e.Location;
    points.Add(a);
    p.Width = tebal;
    System.Diagnostics.Debug.WriteLine(e.Y);
    a = points[0];
    a.X = (points[0].X + points[points.Count - 1].X) / 2;
    b = points[points.Count - 1];
    b.X = points[0].X;
    c = points[points.Count - 1];
    list[0] = a;
    list[1] = b;
    list[2] = c;
}
}

private void panel1_MouseUp(object sender, MouseEventArgs e)
{
    if (shouldPaint == true && warna == true)
{
        if (line == true)
{
            p.Width = tebal;
            ControlPaint.DrawReversibleLine(panel1.PointToScreen(preCoor), panel1.PointToScreen(newCoor), wrn1);
            objGraphic.DrawLine(p, preCoor, newCoor);
            rumusline(); shouldPaint = false; 
}

else if (rectang == true)
{ rumusrectang(); shouldPaint = false; }

else if (circle == true)
{ rumuscircle(); shouldPaint = false; }

else if (triangle == true)
{
this.Refresh();
objGraphic.DrawPolygon(p, list);
foreach (Point el in points)
System.Diagnostics.Debug.WriteLine(el);
rumustriangle(); shouldPaint = false;
}
}
}


private void panel1_MouseClick(object sender, MouseEventArgs e)
{
    if (line == false && rectang == false && circle == false && triangle == false)
{
        DialogResult box2;
        box2 = MessageBox.Show("Please, Select Tools", "Error", MessageBoxButtons.RetryCancel);
        if (box2 == DialogResult.Cancel)
{ this.Dispose(); } 

if (warna == false)
{
    DialogResult box1;
    box1 = MessageBox.Show("Please, Select Color", "Error", MessageBoxButtons.RetryCancel);
    if (box1 == DialogResult.Cancel)
{ this.Dispose(); }
}

if (tebal == 0)
{
    DialogResult box;
    box = MessageBox.Show("Width input cannot 0 pixel", "Error", MessageBoxButtons.RetryCancel);
    if (box == DialogResult.Cancel)
{ this.Dispose(); }
}
}

/// <summary>
/// warna
/// </summary>
/// <param name=”sender”></param>
/// <param name=”e”></param>

        private void buttonBlack_Click(object sender, EventArgs e)
        {
            buttonBlack.FlatAppearance.BorderColor = System.Drawing.Color.White;
            wrn = Color.Black; wrn1 = Color.Black; colour();
        }

        private void buttonRed_Click(object sender, EventArgs e)
        {
            buttonRed.FlatAppearance.BorderColor = System.Drawing.Color.White;
            wrn = Color.Red; wrn1 = Color.Cyan; colour();
        }

        private void buttonOrange_Click(object sender, EventArgs e)
        {
            buttonOrange.FlatAppearance.BorderColor = System.Drawing.Color.White;
            wrn = Color.Orange; wrn1 = Color.DeepSkyBlue; colour();
        }

        private void buttonYellow_Click(object sender, EventArgs e)
        {
            buttonYellow.FlatAppearance.BorderColor = System.Drawing.Color.White;
            wrn = Color.Yellow; wrn1 = Color.Blue; colour();
        }

        private void buttonGreen_Click(object sender, EventArgs e)
        {
            buttonGreen.FlatAppearance.BorderColor = System.Drawing.Color.White;
            wrn = Color.Green; wrn1 = Color.Green; colour();
        }

        private void buttonBlue_Click(object sender, EventArgs e)
        {
            buttonBlue.FlatAppearance.BorderColor = System.Drawing.Color.White;
            wrn = Color.Blue; wrn1 = Color.Yellow; colour();
        }

        private void buttonPurple_Click(object sender, EventArgs e)
        {
            buttonPurple.FlatAppearance.BorderColor = System.Drawing.Color.White;
            wrn = Color.Purple; wrn1 = Color.Purple; colour();
        }

        private void buttonMagenta_Click(object sender, EventArgs e)
        {
            buttonMagenta.FlatAppearance.BorderColor = System.Drawing.Color.White;
            wrn = Color.Magenta; wrn1 = Color.Lime; colour();
        }

/// <summary>
/// exit button
/// </summary>
/// <param name=”sender”></param>
/// <param name=”e”></param>

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    if (e.CloseReason == CloseReason.UserClosing)
{
        DialogResult result = MessageBox.Show("Do you really want to exit?", "Warning", MessageBoxButtons.YesNo);
        if (result == DialogResult.Yes)
{ Environment.Exit(0); }
else
{ e.Cancel = true; }
}
else
{ e.Cancel = true; }
}

/// <summary>
/// button shape
/// </summary>
/// <param name=”sender”></param>
/// <param name=”e”></param>

        private void Line_Click(object sender, EventArgs e)
        {
            reset(); cleartext(); line = true;
            buttoncolor(); Line.BackColor = Color.LightCyan;
        }

        private void Rectang_Click(object sender, EventArgs e)
        {
            reset(); cleartext(); rectang = true;
            buttoncolor(); Rectang.BackColor = Color.LightCyan;
        }

        private void Circle_Click(object sender, EventArgs e)
        {
            reset(); cleartext(); circle = true;
            buttoncolor(); Circle.BackColor = Color.LightCyan;
        }

        private void Triagle_Click(object sender, EventArgs e)
        {
            reset(); cleartext(); triangle = true;
            buttoncolor(); Triagle.BackColor = Color.LightCyan;
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            cleartext(); buttoncolor(); numericUpDown1.Value = 1;
        }

        private void Pain_Load(object sender, EventArgs e)
        {
            
        }
    }

     4.     Jalankan aplikasi paint sederhananya


Berikut ini tutorial youtubenya :


Tidak ada komentar:

Posting Komentar