How to Create Custom Coordinate Graph Surface in C# Graphics

Posted by ေတဇာလင္း Friday 18 September 2015 0 comments

မိတ္ေဆြ၊ ညီအစ္ကို၊ ေမာင္ႏွမအားလံုးပဲ မဂၤလာပါဗ်ာ။ က်ေနာ္တို႔ C# Graphics ေလ့က်င့္ခန္းေလးကို ဆက္ေလ့လာၾကည့္လိုက္ၾကရေအာင္။ အရင္အပတ္ေလ့က်င့္ခန္းမွာေတာ့ က်ေနာ္တို႔ PageUnit / GraphicsUnit အေၾကာင္းေလးကို ေဆြးေႏြးေပးခဲ့ပါတရ္။ ဒီေန႔ေတာ့ ေနာက္တစ္ဆင့္အေနနဲ႔ က်ေနာ္တို႔ X and Y ပါတဲ့ Coordinate Graph တစ္ခုကို Custom အေနနဲ႔ C# Graphics ကို အသံုးျပဳၿပီး ဘရ္လို ဖန္တီး တည္ေဆာက္ႏိုင္မလဲ ဆိုတာေလးနဲ႔ ပတ္သတ္ၿပီး ေဆြးေႏြးေပးသြားပါ့မရ္ဗ်ာ။ Coordinate Systems - C# Graphics Programming Part-2 မွာ ေလ့လာခဲ့ၾကၿပီးျဖစ္တဲ့ Default Coordinate System ကိုပဲ လက္ေတြ႔ ပံုေဖာ္ၾကည့္မွာျဖစ္ပါတရ္။ ေလ့က်င့္ခန္းရဲ႕ ရည္ရြယ္ခ်က္ကေတာ့ Coordinate Graph Surface တစ္ခုကို ဖန္တီးတတ္ရန္နဲ႔ ၄င္း Surface ေပၚမွာ တိက်တဲ့ အတိုင္းအတာနဲ႔ Graphis ေတြကို ဆြဲတတ္ရန္အတြက္ပဲ ျဖစ္ပါတရ္ဗ်ာ။ ဒါအျပင္ သင္ခန္းစာနဲ႔ ဆက္စပ္ၿပီး Graphics ရဲ႕
SmoothingMode Property,
TranslateTransform( ) Method,
AdjustableArrowCap( )Constructor
Clear( )Method,
Pen Object,
Font Object,
DrawLine( )Method,
DrawString( )Method
စတာေလးေတြရဲ႕ အသံုးျပဳပံုေလးေတြကိုပါ ထည့္သြင္းေဆြးေႏြး ေပးထားပါတရ္ဗ်ာ။ စိတ္၀င္စားတဲ့ မိတ္ေဆြ၊ ညီအစ္ကို၊ ေမာင္ႏွမမ်ား က်ေနာ္နဲ႔အတူ ေလ့လာၾကည့္ၾကအံုးစို႔ဗ်ာ။

အဆင့္(၁)။ Drawing အတြက္ using System.Drawing.Drawing2D; ကိုေၾကျငာပါ။

အဆင့္(၂)။ က်ေနာ္တို႔ Form တြင္ Button တစ္ခုနဲ႔ Panel Control တစ္ခုကိုသံုးထားပါတရ္။ Properties ေတြကိုေတာ့ ေအာက္ပါ Table အတိုင္း ျပင္ယူလိုက္ပါဗ်ာ။
 Control       Name        Text            Backgroud Color
Button1       btnCreateCoordinate     Create Coordinate    -
Panel1       pnPlotArea       -      Silver
ဒီေလ့က်င့္ခန္းမွာေတာ့ က်ေနာ္တို႔ အလုပ္ လုပ္ပံုကို အပိုင္း(၃)ပိုင္းခြဲၿပီး ေလ့လာႏိုင္ပါတရ္ဗ်ာ။ ပထမ အပိုင္းက Graphics Object တစ္ခုန႔ဲ ၄င္းနဲ႔ဆက္စပ္ေနတဲ့ Attribute ေတြကို ျပင္ယူတဲ့အပိုင္းျဖစ္ပါတရ္။ ဒုတိယအပိုင္းကေတာ့ X and Y Coordinate Line ႏွစ္ခုနဲ႔ ၄င္းတို႔ရဲ႕ Lable ကိုဆြဲယူတဲ့အပိုင္းျဖစ္ၿပီး တတိယအပိုင္းကေတာ့ မိမိႏွစ္သတ္ရာ Offset နဲ႔ Point ေတြခ်ၿပီး Lable တတ္တဲ့အပိုင္း ျဖစ္ပါတရ္ဗ်ာ။

အဆင့္(၃)။ Graphics Object နဲ႔ Attribute မ်ားျပင္ဆင္ယူျခင္း။
ရိုးရွင္းလြန္းပါတရ္ဗ်ာ။ က်ေနာ္တို႔ေရးေနၾက ပံုစံအတိုင္းပါပဲ။ ပိုနားလည္သြားေအာင္ Coding ေလးကို ေအာက္မွာ ၾကည့္လိုက္ပါအံုးဗ်ာ။

//Create Graphics Object on Panel
Graphics gr = pnPlotArea.CreateGraphics();

//Define Graphics Quality
gr.SmoothingMode = SmoothingMode.HighQuality;

//Change Custom Start Point of Graphics
gr.TranslateTransform(30,30);

//Refresh Graphic's BG Color
gr.Clear(Color.Silver);
Coding တစ္ေၾကာင္းစီတိုင္းမွာ Comment ေလးေတြနဲ႔ ရွင္းထားၿပီးမို႔ နားလည္ၾကလိမ့္မရ္ ထင္ပါတရ္ဗ်ာ။

အဆင့္(၄)။ X and Y Coordinate Line ႏွစ္ခုနဲ႔ ၄င္းတို႔ရဲ႕ Lable မ်ားျပင္ဆင္ယူျခင္း။

//Create Pen Object Specified Color and Width
using (Pen p = new Pen(Color.Black, 5))
{
    //Create X and Y axes
    p.CustomEndCap = new AdjustableArrowCap(3, 3);
    gr.DrawLine(p, 0, 0, pnPlotArea.Width-40, 0);
    gr.DrawLine(p, 0, 0, 0, pnPlotArea.Height-40);

    //Create Font Object Specified FontFamily,Size,Style
    using(Font f = new Font("Courier New", 18, FontStyle.Bold))
    {
        //Create Axes's Label
  gr.DrawString("X", f, Brushes.Blue, pnPlotArea.Width-60, 10);
        gr.DrawString("Y", f, Brushes.Blue, 10, pnPlotArea.Height-60);
    }               
}
ဒီမွာလည္း ခက္ခဲတဲ့ Coding ေတြမပါ ပါဘူးဗ်ာ။ အနည္းငရ္ရွင္းရမရ္ဆိုရင္ေတာ့ Coding စစခ်င္းမွာ Graphics ဆြဲမည့္ Pen Object ကို Color=Black နဲ႔ Width=5 နဲ႔ ဖန္တီးယူလိုက္ပါတရ္။ ေနာက္ က်ေနာ္တို႔ဆြဲယူမရ္႕ Line တိုင္းကို အဆံုးမွာ မွ်ားေခါင္းတတ္ေပးခ်င္တာျဖစ္တဲ့အတြက္ AdjustableArrowCap(3, 3) Constructor ကိုအသံုးျပဳထားပါတရ္ဗ်ာ။ ထိုေနာက္ DrawLine( )Method ကိုအသံုးျပဳၿပီး X Coordinate အတြက္ Line တစ္လိုင္းနဲ႔ Y Coordinate အတြက္ Line တစ္လိုင္း ဆြဲလိုက္ပါတရ္ဗ်ာ။ ေနာက္တစ္ဆင့္မွာေတာ့ က်ေနာ္တို႔ Font Object တစ္ခုကို မိမိစိတ္ႀကိဳက္ Attribute ေတြနဲ႔ ဖန္တီးယူလိုက္ပါတရ္ဗ်ာ။ မိတ္ေဆြတို႔အေနနဲ႔ေတာ့ Attribute ေတြကို စိတ္ႀကိဳက္ေျပာင္းလဲ အသံုးျပဳႏိုင္ပါတရ္ေနာ္။ ေနာက္ဆံုးအဆင့္မွာေတာ့ DrawString( )Method ကိုအသံုးျပဳၿပီးေတာ့ X နဲ႔ Y ဆိုတဲ့ String ႏွစ္ခုကို Line ႏွစ္ခုရဲ႕ အဆံုးမွာ Lable အေနနဲ႔ ဖန္တီးယူလိုက္တာပဲ ျဖစ္ပါတရ္ဗ်ာ။ ရလာတဲ့ Result ကေတာ့…
Coding တိုင္းမွာလည္း Comment ေလးေတြနဲ႔ ရွင္းထားေပးထားပါတရ္။ နားလည္ၾကလိမ့္မရ္ ထင္ပါတရ္ဗ်ာ။

အဆင့္(၅)။ မိမိႏွစ္သတ္ရာ Offset နဲ႔ Point and Lable မ်ားျပင္ဆင္ယူျခင္း။

//Draw Ticks
int tick = 50;
for (int i = 0; i <= 400; i+=tick)
{
   using (Pen p = new Pen(Color.Black,3))
   {
       //For X-axes's Line
       gr.DrawLine(p, i, 0, i, 10);

       //For Y-axes's Line
       gr.DrawLine(p,0, i, 10, i);

       if (i != 0)
       {
           //For X-axes's Text Lable
           gr.DrawString(i.ToString(),this.Font, Brushes.Green, i+5, 5);

           //For Y-axes's Text Lable
           gr.DrawString(i.ToString(), this.Font, Brushes.Green, 5, i+5);
       }
       else
       {
           //For Point(0,0)
           gr.DrawString("0",this.Font, Brushes.Green, i+5, 5);
       }
   }               
}
ဒီမွာလည္း ခက္ခဲတဲ့ Coding ေတြမပါ ပါဘူးဗ်ာ။ အနည္းငရ္ရွင္းရမရ္ဆိုရင္ေတာ့ Coding စစခ်င္းမွာ For( )Loop ကို အသံုးျပဳၿပီး 400 နဲ႔ ညီသြားတဲ့အေျခအေနထိ DrawLine( )Method ကိုအသံုးျပဳၿပီး Offset ကို 50 တိုးကာ X axes အတြက္ေရာ Y axes အတြက္ပါ Line ေတြကိုဆြဲယူလုိက္ပါတရ္ဗ်ာ။ Text Lable အတြက္ကေတာ့ For( )Loop အထဲမွာပဲ IF( )Statement ကို အသံုးျပဳလိုက္ၿပီး DrawString( )Method နဲ႔ Line Position ထက္ Text Position ကို 5 ေပါင္းေပးကာ ဆြဲယူဖန္တီးလိုက္ပါတရ္ဗ်ာ။ Coding တိုင္းမွာလည္း Comment ေလးေတြနဲ႔ ရွင္းလင္းေပးထားတဲ့အတြက္ေၾကာင့္ နားလည္ၾကလိမ့္မရ္ ထင္ပါတရ္ဗ်ာ။ အခုေလာက္ဆိုရင္ေတာ့ မိတ္ေဆြတို႔ အေနနဲ႔ Custom အေနနဲ႔ Coordinate Graph Surface တစ္ခုကို ဖန္တီးတတ္ၿပီလို႔ မိတ္ေဆြအားလံုး ေလ့လာျခင္းျဖင့္ ေက်နပ္ႏိုင္ၾကပါေစဗ်ာ။

0 Responses so far.

Post a Comment