FillEllipse: Red Traffic Light : Graphics « 2D Graphics « C# / C Sharp






FillEllipse: Red Traffic Light

 

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

class Form1 : Form {
    private int lightColor = 1;

    public Form1() {
        InitializeComponent();
    }

    private void Form1_Paint(object sender, PaintEventArgs e) {
        Graphics g = e.Graphics;

        g.FillRectangle(Brushes.White, this.ClientRectangle);
        Rectangle r = new Rectangle(10, 10, 60, 180);
        g.FillRectangle(Brushes.LightGray, r);
        Rectangle r1 = new Rectangle(10, 10, 60, 60);
        Rectangle r2 = new Rectangle(10, 70, 60, 60);
        Rectangle r3 = new Rectangle(10, 130, 60, 60);
        switch (lightColor) {
            case 1:
                g.FillEllipse(Brushes.Red, r1);
                g.FillEllipse(Brushes.Black, r2);
                g.FillEllipse(Brushes.Black, r3);
                break;
            case 2:
                g.FillEllipse(Brushes.Black, r1);
                g.FillEllipse(Brushes.Yellow, r2);
                g.FillEllipse(Brushes.Black, r3);
                break;
            case 3:
                g.FillEllipse(Brushes.Black, r1);
                g.FillEllipse(Brushes.Black, r2);
                g.FillEllipse(Brushes.Green, r3);
                break;
        }
    }

    private void Form1_Click(object sender, EventArgs e) {
        lightColor++;
        if (lightColor == 4)
            lightColor = 1;
        this.Invalidate();
    }
    private void InitializeComponent() {
        this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
        this.ClientSize = new System.Drawing.Size(292, 271);
        this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
        this.Click += new System.EventHandler(this.Form1_Click);
    }

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

}

 








Related examples in the same category

1.Graphics: TranslateClip
2.Graphics: SetClip
3.Scribble with MouseScribble with Mouse
4.FillPolygon: Alternate and Winding Fill Modes
5.One-Inch Ellipse
6.Graphics: FillRectangles
7.Graphics: FillRectangle
8.Graphics: FillEllipse
9.Wide Polyline: DrawLines
10.Line and Arc Combo
11.Graphics: FillClosedCurve
12.Bezier Art: DrawBeziers
13.Graphics: MeasureString
14.Clear a Graphics
15.Graphics: DrawRectangles
16.Graphics: DrawRectangle
17.Draw Ellipse
18.Graphics: DrawArc
19.Graphics: DrawPie
20.Draw a Rectangle
21.Draw an Image
22.Graphics: Draw an Icon
23.Graphics: DrawString
24.Use different Font object to draw a line of text
25.Graphics: ScaleTransform
26.Graphics: Transform
27.Graphics: TranslateTransform
28.Graphics: Pen Alignment
29.Graphics: PageScale
30.Graphics: PageUnit
31.DpiY and DpiX
32.PixelOffsetMode
33.Paint along points in a list pointsPaint along points in a list points
34.Picture Cube: DrawImage
35.Clipping: ResetClip
36.Rotate the text 45 degrees clockwise then Translate the text 150 pixels horizontally
37.Transform and RotateTransform in a loop
38.Apply the scaling transformation(scale subsequent operations by 2x horizontally and 3x vertically)
39.Translate the text 150 pixels horizontally and 75 vertically
40.Reset the transformation Translate by 30 pixels in vertical direction
41.Rotate the text through 45 degrees
42.Fill the area 'bounded' by the path
43.DrawImage with destination points
44.DrawImage with size
45.Graphics.SmoothingMode
46.DrawPie with offset
47.TransformPoints
48.Fill a Rectangle with TextureBrush
49.Fill a Rectangle with LinearGradientBrush