C# Project
In this project we will be creating a simple Radar screen in CSharp using Visual Studio. At the end of this project we will have an application mimic a radar screen. You are encouraged to experiment with the code and change the look and design as per your imagination.
Following variables are used in this project to create the simple radar.
Variable | Note |
---|---|
t | The Timer variable. |
WIDTH | The width of the plan surface on which we will draw circle. |
HEIGHT | The height of the plan surface on which we will draw circle. |
HAND | The lenght of the hand. Note! we will be drawing a circle whose diameter is equal to the width of the plan surface. So, HAND will be half the WIDTH as radius of a circle is half the diameter. |
u | There will be two lines swiping the screen to create the radar screen. The value of u (in DEGREE) is for the first line which will be leading. |
cx | x coordinate of the center of the circle. cx = WIDTH / 2 |
cy | y coordinate of the center of the circle. cy = HEIGHT / 2 |
x | x coordinate of the first line or hand that will be leading. |
y | y coordinate of the first line or hand that will be leading. |
tx | x coordinate of the second line or hand that will be following the leading line or hand. |
ty | y coordinate of the second line or hand that will be following the leading line or hand. |
lim | This is to make the second line to be 20 degree behind the first line. |
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace SimpleRadar
{
public partial class Form1 : Form
{
Timer t = new Timer();
int WIDTH = 300, HEIGHT = 300, HAND = 150;
int u; //in degree
int cx, cy; //center of the circle
int x, y; //HAND coordinate
int tx, ty, lim = 20;
Bitmap bmp;
Pen p;
Graphics g;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//create Bitmap
bmp = new Bitmap(WIDTH + 1, HEIGHT + 1);
//background color
this.BackColor = Color.Black;
//center
cx = WIDTH / 2;
cy = HEIGHT / 2;
//initial degree of HAND
u = 0;
//timer
t.Interval = 5; //in millisecond
t.Tick += new EventHandler(this.t_Tick);
t.Start();
}
private void t_Tick(object sender, EventArgs e)
{
//pen
p = new Pen(Color.Green, 1f);
//graphics
g = Graphics.FromImage(bmp);
//calculate x, y coordinate of HAND
int tu = (u - lim) % 360;
if (u >= 0 && u <= 180)
{
//right half
//u in degree is converted into radian.
x = cx + (int)(HAND * Math.Sin(Math.PI * u / 180));
y = cy - (int)(HAND * Math.Cos(Math.PI * u / 180));
}
else
{
x = cx - (int)(HAND * -Math.Sin(Math.PI * u / 180));
y = cy - (int)(HAND * Math.Cos(Math.PI * u / 180));
}
if (tu >= 0 && tu <= 180)
{
//right half
//tu in degree is converted into radian.
tx = cx + (int)(HAND * Math.Sin(Math.PI * tu / 180));
ty = cy - (int)(HAND * Math.Cos(Math.PI * tu / 180));
}
else
{
tx = cx - (int)(HAND * -Math.Sin(Math.PI * tu / 180));
ty = cy - (int)(HAND * Math.Cos(Math.PI * tu / 180));
}
//draw circle
g.DrawEllipse(p, 0, 0, WIDTH, HEIGHT); //bigger circle
g.DrawEllipse(p, 80, 80, WIDTH - 160, HEIGHT - 160); //smaller circle
//draw perpendicular line
g.DrawLine(p, new Point(cx, 0), new Point(cx, HEIGHT)); // UP-DOWN
g.DrawLine(p, new Point(0, cy), new Point(WIDTH, cy)); //LEFT-RIGHT
//draw HAND
g.DrawLine(new Pen(Color.Black, 1f), new Point(cx, cy), new Point(tx, ty));
g.DrawLine(p, new Point(cx, cy), new Point(x, y));
//load bitmap in picturebox1
pictureBox1.Image = bmp;
//dispose
p.Dispose();
g.Dispose();
//update
u++;
if (u == 360)
{
u = 0;
}
}
}
}
namespace SimpleRadar
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.pictureBox1 = new System.Windows.Forms.PictureBox();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
this.SuspendLayout();
//
// pictureBox1
//
this.pictureBox1.Dock = System.Windows.Forms.DockStyle.Fill;
this.pictureBox1.Location = new System.Drawing.Point(0, 0);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(386, 325);
this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
this.pictureBox1.TabIndex = 0;
this.pictureBox1.TabStop = false;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(386, 325);
this.Controls.Add(this.pictureBox1);
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "RADAR";
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.PictureBox pictureBox1;
}
}
ADVERTISEMENT