using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
namespace TwinkleStars
{
public partial class Form1 : Form
{
Graphics paper;
Color c = Color.Black;
Star star = new Star( );
public Form1( )
{
//double buffer use here helps with the flicker reduction
SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint |
ControlStyles.UserPaint, true);
Cursor.Hide( );
BackColor = c;
InitializeComponent( );
}
protected override void OnPaint(PaintEventArgs e)
{
paper = e.Graphics;
star.drawStars(paper);
//uses less processor than invalidate
this.Refresh( );
//Invalidate( );
}
protected override void onkeydown(KeyEventArgs e)
{
if (e.KeyCode == Keys.Escape)
{
this.Dispose( );
Close( );
}
base.onkeydown(e);
}
}
public class Star
{
private int x, y, w, h;
private int numStars = 300;
Random r = new Random( );
Brush b = Brushes.Gray;
Brush [] bArray = { Brushes.WhiteSmoke, Brushes.DimGray };
public Rectangle [] starRec = new Rectangle [300];
public Rectangle starRecs
{
get { return starRec [300]; }
}
public Star( )
{
for (int i = 0; i < numStars; i++)
{
x = r.Next(1024);
y = r.Next(790);
h = w = r.Next(1, 3);
starRec [i] = new Rectangle(x, y, w, h);
}
}
public void drawStars(Graphics paper)
{
for (int i = 0; i < numStars; i++)
{
int x = r.Next(0, 2);
if (i % (numStars / 2) == 0)
{
paper.FillEllipse(b, starRec [i]);
}
else
{
paper.FillEllipse((Brush) bArray [x], starRec [i]);
}
}
}
}
}
Help is appreciated!

New Topic/Question
Reply



MultiQuote





|