برای مشاهده مفیدترین ارسال در این موضوع اینجا کلیک کنید

نمایش نتایج: از شماره 1 تا 1 از مجموع 1
  1. #1
    مدیر بازنشسته
    تاریخ عضویت
    2011 June
    محل سکونت
    گرگان
    ارسال ها
    1,170
    تشکر
    62
    تشکر شده 1,587 بار در 809 پست
    نوشته های وبلاگ
    49


    2 امتياز مثبت از 2 راي
    آيا اين پست براي شما سودمند بود؟ بله | خیر

    Post paint_نقاشی با شی گرایی

    سلام
    مشکلات موجود برای این برنامه:
    قبل از این یه پروژه انجام دادم و تکرارها رو دیدم
    برای همین رفتم کلاسها رو به ترتیب زیر نوشتم


    ترتیب:
    clsLine
    clsRectangle
    clsEllipse




    مشکل:


    کد تکراری در دو کلاس:
    Abstraction


    ClsLine:
    using System;
    using System.Drawing;


    namespace winPaint
    {

    class clsLine
    {
    //must be property, وقت نداشتم فیلد کردمشون
    //p1.x<=p2.x , p1.y<=p2.y
    public Point p1, p2;
    public Pen pen;
    public Graphics boom;
    //------


    public clsLine(Point p1, Point p2, Pen pen, Graphics boom)
    {
    this.p1 = p1;
    this.p2 = p2;
    this.pen = pen;
    this.boom = boom;


    }
    public void Draw()
    {
    //چون در این فضا نمی بیند، به سطح کلاس می رود و آنها را پیدا می کند
    boom.DrawLine(pen, p1, p2);
    }
    public void Save()
    {
    clsDB db1 = new clsDB("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=dbpaint.accdb");
    db1.Perform(string.Format("INSERT INTO tblLine(x1,y1,x2,y2,color) VALUES({0},{1},{2},{3},'{4}')",p1.X,p1.Y,p2.X,p2.Y,pen.Color));
    }
    }
    }





    ClsEllipse:
    using System;
    using System.Drawing;


    namespace winPaint
    {
    class clsEllipse
    {
    //must be property, وقت نداشتم فیلد کردمشون
    //p1.x<=p2.x , p1.y<=p2.y
    public Point p1, p2;
    public Pen pen;
    public Graphics boom;
    //------
    public clsEllipse(Point p1, Point p2, Pen pen, Graphics boom)
    {
    this.p1 = p1;
    this.p2 = p2;
    this.pen = pen;
    this.boom = boom;


    }
    public void Draw()
    {
    //چون در این فضا نمی بیند، به سطح کلاس می رود و آنها را پیدا می کند

    boom.DrawEllipse(pen, p1.X,p1.Y, p2.X- p1.X, p2.Y-p1.Y);
    }
    public void Save()
    {
    clsDB db1 = new clsDB("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=dbpaint.accdb");
    db1.Perform(string.Format("INSERT INTO tblEllipse(x1,y1,x2,y2,color) VALUES({0},{1},{2},{3},'{4}')", p1.X, p1.Y, p2.X, p2.Y, pen.Color));
    }




    }
    }





    ClsRectangle:

    using System;
    using System.Drawing;


    namespace winPaint
    {
    class clsRectangle
    {
    //must be property, وقت نداشتم فیلد کردمشون
    //p1.x<=p2.x , p1.y<=p2.y
    public Point p1, p2;
    public Pen pen;
    public Graphics boom;
    //------
    public clsRectangle(Point p1, Point p2, Pen pen, Graphics boom)
    {
    this.p1 = p1;
    this.p2 = p2;
    this.pen = pen;
    this.boom = boom;


    }
    public void Draw()
    {
    //چون در این فضا نمی بیند، به سطح کلاس می رود و آنها را پیدا می کند

    boom.DrawRectangle(pen, p1.X,p1.Y, p2.X- p1.X, p2.Y-p1.Y);
    }
    public void Save()
    {
    clsDB db1 = new clsDB("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=dbpaint.accdb");
    db1.Perform(string.Format("INSERT INTO tblRectangle(x1,y1,x2,y2,color) VALUES({0},{1},{2},{3},'{4}')", p1.X, p1.Y, p2.X, p2.Y, pen.Color));
    }




    }
    }





    اینم یه کلاس برای یک شکل الکیه:

    using System;
    using System.Drawing;
    using System.Data.OleDb;
    using System.Collections.Generic;


    namespace WindowsApplication1
    {
    #region Enum ShapeType
    public enum ShapeType
    {
    Line, Rectangle, Cube
    }
    #endregion


    #region clsShape




    public class clsShape
    {
    public static int ShapeCount;


    protected static OleDbConnection conn;
    protected static OleDbCommand cmd;


    static clsShape()
    {
    ShapeCount = 0;
    conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=dbpaint.accdb");
    cmd = new OleDbCommand("", conn);
    conn.Open();
    }


    public clsShape(Point p1, Point p2, Pen thePen, Graphics collaj)
    {
    this.Collaj = collaj;
    this.thePen = thePen;
    this.P1 = p1;
    this.P2 = p2;


    ShapeCount++;
    }


    ~clsShape()
    {
    ShapeCount--;
    }


    private Pen p;


    public Pen thePen
    {
    get { return p; }
    set { p = value; }
    }



    private Graphics g;


    public Graphics Collaj
    {
    get { return g; }
    set { g = value; }
    }


    private Point p1;


    public Point P1
    {
    get { return p1; }
    set { p1 = value; }
    }


    private Point p2;


    public Point P2
    {
    get { return p2; }
    set { p2 = value; }
    }


    private ShapeType shType;


    protected ShapeType ShType
    {
    get { return shType; }
    set { shType = value; }
    }



    public virtual void Draw(){}


    public virtual bool SaveToDataBase(int paintID)
    {
    throw new Exception("shape");
    cmd.CommandText = string.Format("INSERT INTO tblpaint(x1,y1,x2,y2,color,shType,paintID) VALUES({0},{1},{2},{3},{4},{5},{6})",P1.X,p1.Y,P2.X,P2.Y,thePen.Color.ToArgb(),ShType.ToString(),paintID);
    int i=cmd.ExecuteNonQuery();
    return (i > 0);
    }


    public virtual bool LoadFromDataBase(int shapeID)
    {
    return true;
    }


    }
    #endregion


    #region clsLine


    //ye mesal as class va verasat biarid ba toozih
    public sealed class clsLine : clsShape
    {
    public static int lineCount = 0;


    public clsLine(Point p1,Point p2, Pen thePen, Graphics collaj):base(p1,p2,thePen, collaj)
    {
    ShType = ShapeType.Line;
    lineCount++;
    }


    ~clsLine()
    {
    lineCount--;
    }


    public override void Draw()
    {
    Collaj.DrawLine(thePen, P1, P2);
    }



    }
    #endregion


    #region clsRectangle


    public class clsRectangle : clsShape
    {
    public static int rectCount = 0;


    public clsRectangle(Point p1, Point p2, Pen thePen, Graphics collaj)
    : base(p1, p2, thePen, collaj)
    {
    ShType = ShapeType.Rectangle;
    rectCount++;
    }


    ~clsRectangle() {
    rectCount--;
    }


    public override void Draw()
    {
    Point p=new Point();
    p.X = Math.Min(P1.X, P2.X);
    p.Y = Math.Min(P1.Y, P2.Y);
    Collaj.DrawRectangle(thePen, p.X, p.Y, Math.Abs(P2.X - P1.X), Math.Abs(P2.Y - P1.Y));
    }


    }
    #endregion


    #region clsCube
    public class clsCube : clsRectangle
    {
    public clsCube(Point p1, Point p2, Pen thePen, Graphics collaj):base(p1, p2, thePen, collaj)
    {
    ShType = ShapeType.Cube;
    }
    public override bool LoadFromDataBase(int shapeID)
    {
    return base.LoadFromDataBase(shapeID);

    }
    public override bool SaveToDataBase(int paintID)
    {
    //throw new Exception("cube");
    return true;
    }
    }
    #endregion


    #region clsPaint
    public class clsPaint
    {


    private int paintID;


    public int PaintID
    {
    get { return paintID; }
    set { paintID = value; }
    }


    private string paintName;


    public string PaintName
    {
    get { return paintName; }
    set { paintName = value; }
    }


    private Graphics g;


    public Graphics Collaj
    {
    get { return g; }
    set { g = value; }
    }

    List<clsShape> shapes = new List<clsShape>();

    public void AddShape(clsShape sh)
    {


    }
    public void RemoveShape(clsShape sh)
    {


    }
    public bool SaveShapesToDataBase()
    {
    foreach (clsShape sh in shapes)
    {
    sh.SaveToDataBase(this.PaintID);
    }
    return true;
    }
    public void LoadShapesFromDataBase()
    {
    //int i=0;
    //shapes = new List<clsShape>();
    //cmd.CommandText = "SELECT * FROM tblpaint";
    //OleDbDataReader rdr;
    //rdr = cmd.ExecuteReader();
    //while (rdr.Read())
    //{
    // p1.X = Convert.ToInt32(rdr["x1"]);
    // p1.Y = Convert.ToInt32(rdr["y1"]);
    // p2.X = Convert.ToInt32(rdr["x2"]);
    // p2.Y = Convert.ToInt32(rdr["y2"]);
    // thePen = new Pen(Color.FromArgb(Convert.ToInt32(rdr["color"])));
    // ShType = (ShapeType)Enum.Parse(typeof(ShapeType), Convert.ToString(rdr["shType"]));
    // //switch(shty
    // //shapes[i]=;
    //}
    //rdr.Close();
    //return b;




    //foreach (clsShape sh in shapes)
    //{
    // sh.LoadFromDataBase(this.PaintID);
    //}

    //return true;
    }
    }
    #endregion
    }



    اینم فرمای مربوطه:

    فرم اول:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;


    namespace winPaint
    {
    public partial class Form0 : Form
    {
    public Form0()
    {
    InitializeComponent();
    }
    Graphics g;
    private void Form0_Paint(object sender, PaintEventArgs e)
    {
    Pen p = new Pen(Color.Green);
    Point p1 = new Point(20, 40);
    Point p2 = new Point(110, 90);
    g.DrawLine(p, p1, p2);
    g.DrawRectangle(p, p1.X, p1.Y, p2.X - p1.X, p2.Y - p1.Y);
    g.DrawEllipse(p, p1.X, p1.Y, p2.X - p1.X, p2.Y - p1.Y);
    }


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




    فرم دوم:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;


    namespace winPaint
    {
    public partial class Form1 : Form
    {
    Graphics gpanel1;
    public Form1()
    {
    InitializeComponent();
    }


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

    }


    private void DrawPicture()
    {
    Pen p = new Pen(Color.Red);
    Point p1 = new Point(20, 30);
    Point p2 = new Point(50, 60);


    clsLine l1 = new clsLine(p1,p2,p,gpanel1);
    l1.Draw();
    //l1.Save();


    clsRectangle r1 = new clsRectangle(p1, p2, p, gpanel1);
    r1.Draw();
    r1.Save();


    clsEllipse e1 = new clsEllipse(p1, p2, p, gpanel1);
    e1.Draw();
    e1.Save();
    }


    private void Form1_Paint(object sender, PaintEventArgs e)
    {
    DrawPicture();


    }
    }
    }






    موفق باشید..

    موضوعات مشابه:
    فایل های پیوست شده
    • نوع فایل: zip winPaint.zip (82.5 کیلو بایت,  این فایل 13 بار دانلود شده است)
    آرامش محصول تفکر نیست! آرامش هنر نیندیشیدن به انبوه مسائلیست که ارزش فکر کردن ندارد...

 

 

کاربران برچسب خورده در این موضوع

علاقه مندی ها (Bookmarks)

علاقه مندی ها (Bookmarks)

مجوز های ارسال و ویرایش

  • شما نمیتوانید موضوع جدیدی ارسال کنید
  • شما امکان ارسال پاسخ را ندارید
  • شما نمیتوانید فایل پیوست کنید.
  • شما نمیتوانید پست های خود را ویرایش کنید
  •  


Powered by vBulletin
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.6.0
Persian Language By Ustmb.ir
این انجمن کاملا مستقل بوده و هیچ ارتباطی با دانشگاه علوم و فنون مازندران و مسئولان آن ندارد..این انجمن و تمامی محتوای تولید شده در آن توسط دانشجویان فعلی و فارغ التحصیل ادوار گذشته این دانشگاه برای استفاده دانشجویان جدید این دانشگاه و جامعه دانشگاهی کشور فراهم شده است.لطفا برای اطلاعات بیشتر در رابطه با ماهیت انجمن با مدیریت انجمن ارتباط برقرار کنید
ساعت 04:51 PM بر حسب GMT +4 می باشد.