Home Course Index Next >> PDF Version of this Page C1 Code Comments

Course 2DCis: 2D-Computer Graphics with C#
Chapter C1: The Complete Code of the Intro Project


Copyright © by V. Miszalok, last update: 2011-09-13


Copy all this code into an empty Form1.cs of a new Windows Application C#-project intro1 and clear Form1.Designer.cs and Program.cs.

using System;
using System.Drawing;
using System.Windows.Forms;

public class Form1 : Form
{ [STAThread] static void Main() { Application.Run( new Form1() ); }
  Font  arial18    = new Font( "Arial", 18 );
  Font  arial16    = new Font( "Arial", 16 );
  Font  courier14  = new Font( "Courier New", 14 );
  Brush blackbrush = SystemBrushes.ControlText;
  Brush redbrush   = new SolidBrush( Color.Red );
  Brush whitebrush = new SolidBrush( Color.White );
  Pen   blackpen   = new Pen( Color.Black, 4 );
  Pen   randompen  = new Pen( Color.Black, 20 );

  public Form1()
  { Text       = "intro1";
    BackColor  = Color.White;
    SetStyle( ControlStyles.ResizeRedraw, true );
    StartPosition = FormStartPosition.Manual;
    Top    =  50;
    Left   =  50;
    Width  = 800;
    Height = 600;
  }
  protected override void OnPaint( PaintEventArgs e )
  { //Version 2 ***************************************************
    Graphics g = e.Graphics;
    Rectangle cr = ClientRectangle;
    String s0  = "Hello world, here is intro1 !";
    String s1  = "Change the size of your window by dragging a corner !";
    String s2w = "Form  : Width = " +    Width.ToString();
    String s3w = "Client: Width = " + cr.Width.ToString();
    String s2h = "  Height= " +    Height.ToString();
    String s3h = "  Height= " + cr.Height.ToString();
    g.DrawString( s0       , arial18  , blackbrush, 0,  0 );
    g.DrawString( s1       , arial16  , redbrush  , 0, 20 );
    g.DrawString( s2w + s2h, courier14, blackbrush, 0, 40 );
    g.DrawString( s3w + s3h, courier14, blackbrush, 0, 60 );
    //Version 3 ***************************************************
    Point mid = new Point( cr.Width/2, cr.Height/2 );
    g.DrawString( "left"  ,arial16, blackbrush, 0          , mid.Y        );
    g.DrawString( "right" ,arial16, blackbrush, cr.Width-50, mid.Y        );
    g.DrawString( "top"   ,arial16, blackbrush, mid.X      , 0            );
    g.DrawString( "bottom",arial16, blackbrush, mid.X      , cr.Height-30 );
    //Version 4 ***************************************************
    g.DrawLine( blackpen, 0, 0, cr.Width, cr.Height );
    g.DrawLine( blackpen, cr.Width, 0, 0, cr.Height );
    Int32 w5 = cr.Width  / 5;
    Int32 h5 = cr.Height / 5;
    g.FillRectangle( whitebrush, w5, h5, 3 * w5, 3 * h5 );
    g.DrawRectangle( blackpen  , w5, h5, 3 * w5, 3 * h5 );
    g.DrawEllipse  ( blackpen  , w5, h5, 3 * w5, 3 * h5 );
    //Version 5 ***************************************************
    Int16 i, nn = 120;
    Int32 red, green, blue;
    randompen.EndCap = System.Drawing.Drawing2D.LineCap.DiamondAnchor;
    Point[] splash = new Point[nn];
    Double arcus_1 = 2.0 * Math.PI / nn;
    Double arcus_i, factor, sinus, cosinus;
    Double radius_x = 1.35 * w5;
    Double radius_y = 1.35 * h5;
    Random random = new Random();
    for ( i=0; i < nn; i++ )
    { red   = random.Next( Byte.MaxValue );
      green = random.Next( Byte.MaxValue );
      blue  = random.Next( Byte.MaxValue );
      randompen.Color = Color.FromArgb( red, green, blue );
      factor = Math.Max( 0.25, random.NextDouble() );
      arcus_i = arcus_1 * i;
      cosinus = radius_x * factor * Math.Cos( arcus_i );
      sinus   = radius_y * factor * Math.Sin( arcus_i );
      g.DrawLine( randompen, mid.X, mid.Y, mid.X + (Int32)cosinus, mid.Y + (Int32)sinus );
      splash[i].X = mid.X + (Int32)(cosinus * 0.8);
      splash[i].Y = mid.Y + (Int32)(  sinus * 0.8);
    }
    //Version 6 ***************************************************
    g.FillClosedCurve( redbrush, splash );
    g.DrawString( "splash !", arial18, whitebrush, mid.X - 40, mid.Y - 9 );
    //Version 7 ***************************************************
    System.Threading.Thread.Sleep( 100 ); //Wait 100 milliseconds.
    Invalidate(); //Ask the operating system to raise the Paint event again.
  }
}