using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; namespace PS6_Q1 { // // Problem Set 6, Question 1 // Written by Paul Hudak // October 27, 2006 // public class Form1 : System.Windows.Forms.Form { // // Paints a checkerboard pattern on a Form. // private System.ComponentModel.Container components = null; public Form1() { // Required for Windows Form Designer support InitializeComponent(); Size = new Size(500,500); // set form size Text = "Checkerboard"; // and form label BackColor = Color.Black; // might look better with a black background } protected override void OnPaint(PaintEventArgs e) { // size of individual squares, in pixels int size = 50; // get handle on graphics window Graphics g = e.Graphics; // use nested loops to create 8x8 checkerboard for (int i=0; i<8; i=i+1) for (int j=0; j<8; j=j+1) { /* this is the obvious solution if ((i+j)%2==0) // determines which color to use g.FillRectangle(Brushes.Red,50+i*size,50+j*size,size,size); else g.FillRectangle(Brushes.Yellow,50+i*size,50+j*size,size,size); */ // this solution uses a conditional expression g.FillRectangle((i+j)%2==0 ? Brushes.Red : Brushes.Yellow, 50+i*size,50+j*size,size,size); } } // Clean up any resources being used. protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { this.components = new System.ComponentModel.Container(); this.Size = new System.Drawing.Size(300,300); this.Text = "Form1"; } #endregion // The main entry point for the application. [STAThread] static void Main() { Application.Run(new Form1()); } } }