Chasing lights, running ants.

ddanbe 5 Tallied Votes 996 Views Share

Ever wanted to know how to implement running lights? well here is your chance to find out.
Open a new Forms application enlarge the Form a bit and drop a Panel and a Timer on it. Add the class ChasingLights to the solution, see code. Implement a form Load, panel Paint and Timer Tick event. See code. I have included the result you should get(without the "lights" running;) ) Also added a few fancy tricks you can do with strings. Have fun!

sknake commented: lol! Thanks danny :) +5
DdoubleD commented: cool graphics! +1
Antenka commented: Indeed, fancy :) +1
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;

// October 2009 Danny Marivoet
// This class can be used anyway you want
// If you misuse it, my name will be Manuel, from Barzelona, mee no noothing!
namespace Marquee
{
    class ChasingLights
    {
        public enum LightShape
        {
            Square, Round
        }

        private struct LampState
        {
            public Point Pos;
            public bool On;
        }

        private List<LampState> Lamps = new List<LampState>();
        private LampState bulb = new LampState();
        private bool reverse = false;

        public ChasingLights()
        {
            LampSize = new Size(8, 8);
            DisplayRect = new Rectangle(0, 0, 64, 64);
            LampOnColor = Color.Yellow;
            LampOffColor = Color.Red;
            LampShape = LightShape.Round;
            bulb.On = false;
        }

        public Size LampSize { get; set; }
        public Rectangle DisplayRect { get; set; }
        public Color LampOnColor { get; set; }
        public Color LampOffColor { get; set; }
        public LightShape LampShape { get; set; }

        public void Draw(Graphics G)
        {
            G.SmoothingMode = SmoothingMode.AntiAlias;
            reverse = !reverse;
            DrawLightFrame(G, reverse);
            DrawText(G);
        }

        public void MakeLightFrame(int rows, int cols)
        {
            bulb.On = false;
            bulb.Pos.Y = 0;
            for (int c = 0; c < cols; c++)
            {
                bulb.Pos.X = c * LampSize.Width;
                bulb.On = !bulb.On;
                Lamps.Add(bulb);
            }
            for (int r = 1; r < rows; r++)
            {
                bulb.Pos.Y = r * LampSize.Height;
                bulb.On = !bulb.On;
                Lamps.Add(bulb);
            }
            for (int c = cols - 2; c > 0; c--)
            {
                bulb.Pos.X = c * LampSize.Width;
                bulb.On = !bulb.On;
                Lamps.Add(bulb);
            }
            bulb.Pos.X = 0;
            for (int r = rows - 1; r > 0; r--)
            {
                bulb.Pos.Y = r * LampSize.Height;
                bulb.On = !bulb.On;
                Lamps.Add(bulb);
            }
        }

        private void DrawLightFrame(Graphics G, bool reverse)
        {
            foreach (LampState item in Lamps)
            {
                SolidBrush B = new SolidBrush(item.On == reverse ? LampOnColor : LampOffColor);
                Rectangle R = new Rectangle(item.Pos, LampSize);
                switch (LampShape)
                {
                    case LightShape.Square:
                        G.FillRectangle(B, R);
                        break;
                    case LightShape.Round:
                        G.FillEllipse(B, R);
                        break;
                    default:
                        break;
                }
            }
        }

        private void DrawText(Graphics G)
        {
            string strText = "Scott";
            Font font = new Font("Times New Roman", 72);
            Brush hB = new HatchBrush(HatchStyle.HorizontalBrick, Color.White, Color.Blue);
            G.DrawString(strText, font, hB, 50, 10);
            strText = "The C# coder!";
            Font Af = new Font("Arial Black", 24);
            GraphicsPath path = new GraphicsPath();
            // I know: all the numbers that follow is NOT good coding practice
            path.AddString(strText, Af.FontFamily, (int)Af.Style, 36, new Point(30, 150), new StringFormat());
            PointF[] ptDest = { new PointF(60, 100), new PointF(280 , 100), 
                                  new PointF(20, 200), new PointF(330, 200) };
            RectangleF Rbnds = path.GetBounds();
            path.Warp(ptDest, Rbnds);
            G.FillPath(new SolidBrush(Color.Blue), path);
        }

    }
}

/////////////////////////////////////////////////////////////////////////////////////////

using System;
using System.Windows.Forms;

namespace Marquee
{
    public partial class Form1 : Form
    {
        private ChasingLights Marquee = new ChasingLights();

        public Form1()
        {
            InitializeComponent();
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {        
            Marquee.Draw(e.Graphics);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            panel1.Size = new System.Drawing.Size(360, 220);
            Marquee.DisplayRect = panel1.ClientRectangle;
            Marquee.LampShape = ChasingLights.LightShape.Round;
            Marquee.LampSize = new System.Drawing.Size(10, 10);
            int Ncols = Marquee.DisplayRect.Width / Marquee.LampSize.Width;
            int Nrows = Marquee.DisplayRect.Height / Marquee.LampSize.Height;
            Marquee.MakeLightFrame(Nrows, Ncols);
            timer1.Start();
            timer1.Interval = 500;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            this.Refresh();
        }
    }
}
Saikalyankumar -2 Junior Poster in Training

Excellent Boss, its executing with Lighting Effects.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.