make red and blue pens : Pens « 2D Graphics « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Date Time
8.Design Patterns
9.Development Class
10.Event
11.File Stream
12.Generics
13.GUI Windows Form
14.Internationalization I18N
15.Language Basics
16.LINQ
17.Network
18.Office
19.Reflection
20.Regular Expressions
21.Security
22.Services Event
23.Thread
24.Web Services
25.Windows
26.Windows Presentation Foundation
27.XML
28.XML LINQ
C# / C Sharp » 2D Graphics » PensScreenshots 
make red and blue pens
make red and blue pens

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

public class TestGDI1 : System.Windows.Forms.Form{
    
    //in order to paint something OnPaint method needs to be overridden
    
    protected override void OnPaint(System.Windows.Forms.PaintEventArgs pe) {
        //OnPaint method is a member of Form class 
        //The following call sends pe to an event listener Graphics
        base.OnPaint(pe);
        
        //initialize Graphics 
        System.Drawing.Graphics g=pe.Graphics;
        
        //designate the area of the form where the drawing must take place
        //ClientRectangle is a member of Windows.Forms.Control class
        System.Drawing.Rectangle client_area=this.ClientRectangle;
        
        //point11 is at the top left corner of the client_area
        System.Drawing.Point point11=new System.Drawing.Point(client_area.X,client_area.Y)
        //point12 is at the bottom right corner of the client area
        
        System.Drawing.Point point12=new System.Drawing.Point(client_area.Width,client_area.Height);
        
        //create a Brush object of white color
        //SolidBrush means that the color does not change from point to point
        System.Drawing.Brush background=new System.Drawing.SolidBrush(System.Drawing.Color.White);
        
        //color client_area with solid white brush
        g.FillRectangle(background,client_area);
        
        //make red and blue pens
        System.Drawing.Pen p=new System.Drawing.Pen(System.Drawing.Color.Red);
        System.Drawing.Pen p1=new System.Drawing.Pen(System.Drawing.Color.Blue);
        
        //create points and rectangles
        System.Drawing.SizeF size=new System.Drawing.SizeF();
        size.Height=160;
        size. Width=180;
        System.Drawing.PointF point=new System.Drawing.PointF();
        point.X=8;
        point.Y=40;
        System.Drawing.Point point1=new System.Drawing.Point();
        point1.X=300;
        point1.Y=300;
        System.Drawing.Point point2=new System.Drawing.Point();
        point2.X=0;
        point2.Y=0;
        System.Drawing.RectangleF rec =new System.Drawing.RectangleF(point,size);
        
        //draw an ellipse inscribed in the invisible rectangle rec
        //to change the size or shape of the ellipse change an invisible rectangle in which it is inscribed
        //to change the color of the ellipse, change the color of the pen p which is used to draw it
        
        g.DrawEllipse(p,rec);
    
        //draw a line between a pair of points point1 and point2 with pen p1
        g.DrawLine(p1,point1,point2);
    }
    public static void Main() {
        System.Windows.Forms.Application.Run(new TestGDI1());//display form
    }
}



           
       
Related examples in the same category
1.new Pen(lgbrush, Math.Min(cx, cy) / 25), Gradient Pen
2.new Pen(clr, f)
3.new Pen(ForeColor)
4.DashDot style Pen
5.Pen Dash Caps: Flat, Round, Triangle
6.11-Pixel Centered Pen11-Pixel Centered Pen
7.11-Pixel Inset Pen11-Pixel Inset Pen
8.Pen alignment: centerPen alignment: center
9.Pen alignment: insetPen alignment: inset
10.Pen width and colorPen width and color
11.Pen Dash StylesPen Dash Styles
12.illustrates the use of multiple Pensillustrates the use of multiple Pens
13.Dispose a pen
14.StartCap, EndCap
15.creates the custom dash pattern 5
16.Set DashStyle
17.creates the custom dash pattern
18.Set LineJoin for Pen
19.Custom PenCustom Pen
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.