illustrates filling shapes with a brush

image_pdfimage_print


   

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

public class Example21_4 : System.Windows.Forms.Form
{
  private System.ComponentModel.Container components = null;

  public Example21_4()
  {
    InitializeComponent();
  }

  private void InitializeComponent()
  {
    this.BackColor = System.Drawing.Color.White;
    this.ClientSize = new System.Drawing.Size(400, 400);
    this.Name = "Example21_4";
    this.Text = "Example21_4";
    this.Paint += new System.Windows.Forms.
      PaintEventHandler(this.Example21_4_Paint);
  }


  static void Main() 
  {
    Application.Run(new Example21_4());
  }

  private void Example21_4_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
  {
    Graphics g = e.Graphics;
    Brush brSolid = new SolidBrush(Color.Blue);
    Brush brHatch = new HatchBrush(HatchStyle.HorizontalBrick,
      Color.Red, Color.Yellow);
    Brush brGradient = new LinearGradientBrush(new Rectangle(0, 0, 200, 200), Color.Black, Color.LightGray, 45, false);
    g.FillRectangle(brGradient, 10, 10, 200, 200);
    g.FillEllipse(brHatch, 200, 200, 150, 190);
    g.FillPie(brSolid, 0, 0, 300, 300, 285, 75);
  }
}


           
          


Gradient brush demo

image_pdfimage_print


   

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

public class Form1 : System.Windows.Forms.Form{
    private System.ComponentModel.IContainer components;

  public Form1(){
    InitializeComponent();
  }


  #region Windows Form Designer generated code
  private void InitializeComponent(){
      this.components = new System.ComponentModel.Container();
      this.timer1 = new System.Windows.Forms.Timer(this.components);

      this.timer1.Enabled = true;
      this.timer1.Tick += new System.EventHandler(this.timer1_Tick);

      this.AutoScaleBaseSize = new System.Drawing.Size(6, 15);
      this.ClientSize = new System.Drawing.Size(292, 260);
      this.Name = "Form1";
      this.Text = "LinearGradientBrush Demo";
      this.Load += new System.EventHandler(this.Form1_Load);
      this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);

    }
  #endregion

  static void Main(){
    Application.Run(new Form1());
  }

    private void Form1_Load(object sender, System.EventArgs e) {
      this.BackColor = Color.FromArgb(255, 0, 0, 255);  
    }

    private System.Windows.Forms.Timer timer1;

    private float angle = 0;

    private LinearGradientBrush GetBrush()
    {
      return new LinearGradientBrush(
        new Rectangle( 20, 20, 200, 100),
        Color.Orange,
        Color.Yellow,
        0.0F,
        true);
    }


    private void Rotate( Graphics graphics, LinearGradientBrush brush )
    {
      brush.RotateTransform(angle);
      brush.SetBlendTriangularShape(.5F);
      graphics.FillRectangle(brush, brush.Rectangle);
    }

    private void Rotate(Graphics graphics)
    {
      angle += 5 % 360;
      Rotate(graphics, GetBrush());
    }

    private void timer1_Tick(object sender, System.EventArgs e)
    {
      Rotate(CreateGraphics());
    }

    private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
    {
      Rotate(e.Graphics);
    }
}



           
          


Use Brush to draw a Rectangle

image_pdfimage_print


   

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

public class Form1 : System.Windows.Forms.Form{
  private System.ComponentModel.Container components = null;

  public Form1(){
    InitializeComponent();
        SetStyle(ControlStyles.Opaque, true);
  }
  protected override void Dispose( bool disposing ){
    if( disposing ){
      if (components != null) 
      {
        components.Dispose();
      }
    }
    base.Dispose( disposing );
  }
    protected override void OnPaint(PaintEventArgs e) {
         Graphics g = e.Graphics;
         g.FillRectangle(Brushes.White, ClientRectangle);
         g.FillRectangle(Brushes.Red, new Rectangle(10, 10, 50, 50));
    }
  private void InitializeComponent(){
    this.components = new System.ComponentModel.Container();
    this.Size = new System.Drawing.Size(300,300);
    this.Text = "Form1";
  }
  static void Main() {
    Application.Run(new Form1());
  }
}



           
          


Use Brushes.Aquamarine to draw a Ellipse

image_pdfimage_print


   

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

public class Form1 : System.Windows.Forms.Form{
  private System.ComponentModel.Container components = null;

  public Form1(){
    InitializeComponent();
        SetStyle(ControlStyles.Opaque, true);
  }
  protected override void Dispose( bool disposing ){
    if( disposing ){
      if (components != null) 
      {
        components.Dispose();
      }
    }
    base.Dispose( disposing );
  }
    protected override void OnPaint(PaintEventArgs e) {
         Graphics g = e.Graphics;
         g.FillRectangle(Brushes.White, ClientRectangle);

         g.FillEllipse(Brushes.Aquamarine, new Rectangle(60, 20, 50, 30));
    }
  private void InitializeComponent(){
    this.components = new System.ComponentModel.Container();
    this.Size = new System.Drawing.Size(300,300);
    this.Text = "Form1";
  }
  static void Main() {
    Application.Run(new Form1());
  }
}



           
          


Use Brushes.BlueViolet ro draw a Polygon

image_pdfimage_print


   

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

public class Form1 : System.Windows.Forms.Form{
  private System.ComponentModel.Container components = null;

  public Form1(){
    InitializeComponent();
        SetStyle(ControlStyles.Opaque, true);
  }
  protected override void Dispose( bool disposing ){
    if( disposing ){
      if (components != null) 
      {
        components.Dispose();
      }
    }
    base.Dispose( disposing );
  }
    protected override void OnPaint(PaintEventArgs e) {
         Graphics g = e.Graphics;
         g.FillRectangle(Brushes.White, ClientRectangle);



         g.FillPolygon(Brushes.BlueViolet, new Point[] {
                                                          new Point(110, 10),
                                                          new Point(150, 10),
                                                          new Point(160, 40),
                                                          new Point(120, 20),
                                                          new Point(120, 60),
         });
    }
  private void InitializeComponent(){
    this.components = new System.ComponentModel.Container();
    this.Size = new System.Drawing.Size(300,300);
    this.Text = "Form1";
  }
  static void Main() {
    Application.Run(new Form1());
  }
}



           
          


Solid brush demo

image_pdfimage_print


   

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

  public class MainForm : System.Windows.Forms.Form
  {
    private System.ComponentModel.Container components;

    public MainForm()
    {
      InitializeComponent();
      BackColor = Color.LemonChiffon;  
      Text = "www.kutayzorlu.com/java2s/com";    
      Size = new Size(200, 200);    
      CenterToScreen();        
    }

    protected override void Dispose( bool disposing )
    {
      if( disposing )
      {
        if (components != null) 
        {
          components.Dispose();
        }
      }
      base.Dispose( disposing );
    }

    private void InitializeComponent()
    {
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(292, 273);
      this.Text = "Form1";
      this.Resize += new System.EventHandler(this.MainForm_Resize);
      this.Paint += new System.Windows.Forms.PaintEventHandler(this.MainForm_Paint);

    }
    [STAThread]
    static void Main() 
    {
      Application.Run(new MainForm());
    }

    private void MainForm_Resize(object sender, System.EventArgs e)
    {
      Invalidate();
    }

    private void MainForm_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
    {
      Graphics g = e.Graphics;
      g.DrawString("www.kutayzorlu.com/java2s/com", 
        new Font("Times New Roman", 20), 
        new SolidBrush(Color.Black), 
        this.DisplayRectangle);
    }
  }



           
          


Brushes.Black

image_pdfimage_print
   
 

using System;
using System.Drawing;
using System.Windows.Forms;
   
class HelloWorld: Form
{
     public static void Main()
     {
          Application.Run(new HelloWorld());
     }
     public HelloWorld()
     {
          Text = "Hello World";
          BackColor = Color.White;
     }
     protected override void OnPaint(PaintEventArgs pea)
     {
          Graphics graphics = pea.Graphics;
   
          graphics.DrawString("Hello, Windows Forms!", Font, 
                          Brushes.Black, 0, 0);
     }
}