How To Draw Star On Your Windows Application

Friday, 8 July 2011
USING C#

  • First Of All Open New Windows Application.
  • Add Following Namespaces.

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

namespace Draw
{
public class DrawingForm : Form
{
public DrawingForm() //Create Constructor
{
//InitializeComponent
this.Text = "My drawings";
this.Size = new Size(600, 600);
this.Paint += new PaintEventHandler(MyPainting);
}

private void MyPainting(object sender, PaintEventArgs e)
{
Graphics G = e.Graphics;
G.SmoothingMode = SmoothingMode.HighQuality;

PointF[] Star1 = Calculate5StarPoints(new PointF(100f, 100f), 50f, 20f);
SolidBrush FillBrush = new SolidBrush(Color.Pink);
G.FillPolygon(FillBrush, Star1);
G.DrawPolygon(new Pen(Color.Purple, 5), Star1);

PointF[] Star2 = Calculate5StarPoints(new PointF(200f, 150f), 100f, 20f);
HatchBrush pat = new HatchBrush(HatchStyle.Cross, Color.RosyBrown, Color.IndianRed);
G.FillPolygon(pat, Star2);

PointF[] Star3 = Calculate5StarPoints(new PointF(350f, 300f), 200f, 100f);
LinearGradientBrush lin = new LinearGradientBrush(new Point(350, 100), new Point(350, 500),
Color.Salmon, Color.Cyan);
G.FillPolygon(lin, Star3);

PointF[] Star4 = Calculate5StarPoints(new PointF(140f, 400f), 120f, 10f);
G.DrawPolygon(new Pen(Color.LightSalmon, 3), Star4);
}
private PointF[] Calculate5StarPoints(PointF Orig, float outerradius, float innerradius)
{
double Ang36 = Math.PI / 5.0; // 36° x PI/180
double Ang72 = 2.0 * Ang36; // 72° x PI/180
float Sin36 = (float)Math.Sin(Ang36);
float Sin72 = (float)Math.Sin(Ang72);
float Cos36 = (float)Math.Cos(Ang36);
float Cos72 = (float)Math.Cos(Ang72);

PointF[] pnts = { Orig, Orig, Orig, Orig, Orig, Orig, Orig, Orig, Orig, Orig };
pnts[0].Y -= outerradius; // top off the star, or on a clock this is 12:00 or 0:00 hours
pnts[1].X += innerradius * Sin36; pnts[1].Y -= innerradius * Cos36; // 0:06 hours
pnts[2].X += outerradius * Sin72; pnts[2].Y -= outerradius * Cos72; // 0:12 hours
pnts[3].X += innerradius * Sin72; pnts[3].Y += innerradius * Cos72; // 0:18
pnts[4].X += outerradius * Sin36; pnts[4].Y += outerradius * Cos36; // 0:24
pnts[5].Y += innerradius;
pnts[6].X += pnts[6].X - pnts[4].X; pnts[6].Y = pnts[4].Y; // mirror point
pnts[7].X += pnts[7].X - pnts[3].X; pnts[7].Y = pnts[3].Y; // mirror point
pnts[8].X += pnts[8].X - pnts[2].X; pnts[8].Y = pnts[2].Y; // mirror point
pnts[9].X += pnts[9].X - pnts[1].X; pnts[9].Y = pnts[1].Y; // mirror point
return pnts;
}

private void InitializeComponent()
{
this.SuspendLayout();

this.ClientSize = new System.Drawing.Size(292, 266);
this.Name = "DrawingForm";
this.Load += new System.EventHandler(this.DrawingForm_Load);
this.ResumeLayout(false);

}

private void DrawingForm_Load(object sender, EventArgs e)
{
}
}

public class Program
{
[STAThread]
public static int Main()
{
Application.Run(new DrawingForm());
return 0;
}
}
}

USING VB
The Above Code Will Look As follows When Writen In VB.

Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Text
Imports System.Windows.Forms
Imports System.Drawing.Drawing2D

Namespace Draw
Public Class DrawingForm
Inherits Form

Public Sub New()


Me.Text = "My drawings"
Me.Size = New Size(600, 600)
AddHandler Me.Paint, New PaintEventHandler(AddressOf MyPainting)
End Sub

Private Sub MyPainting(sender As Object, e As PaintEventArgs)
Dim G As Graphics = e.Graphics
G.SmoothingMode = SmoothingMode.HighQuality

Dim Star1 As PointF() = Calculate5StarPoints(New PointF(100F, 100F), 50F, 20F)
Dim FillBrush As New SolidBrush(Color.Pink)
G.FillPolygon(FillBrush, Star1)
G.DrawPolygon(New Pen(Color.Purple, 5), Star1)

Dim Star2 As PointF() = Calculate5StarPoints(New PointF(200F, 150F), 100F, 20F)
Dim pat As New HatchBrush(HatchStyle.Cross, Color.RosyBrown, Color.IndianRed)
G.FillPolygon(pat, Star2)

Dim Star3 As PointF() = Calculate5StarPoints(New PointF(350F, 300F), 200F, 100F)
Dim lin As New LinearGradientBrush(New Point(350, 100), New Point(350, 500), Color.Salmon, Color.Cyan)
G.FillPolygon(lin, Star3)

Dim Star4 As PointF() = Calculate5StarPoints(New PointF(140F, 400F), 120F, 10F)
G.DrawPolygon(New Pen(Color.LightSalmon, 3), Star4)
End Sub

Private Function Calculate5StarPoints(Orig As PointF, outerradius As Single, innerradius As Single) As PointF()
Dim Ang36 As Double = Math.PI / 5.0
Dim Ang72 As Double = 2.0 * Ang36
Dim Sin36 As Single = CSng(Math.Sin(Ang36))
Dim Sin72 As Single = CSng(Math.Sin(Ang72))
Dim Cos36 As Single = CSng(Math.Cos(Ang36))
Dim Cos72 As Single = CSng(Math.Cos(Ang72))

Dim pnts As PointF() = {Orig, Orig, Orig, Orig, Orig, Orig, _
Orig, Orig, Orig, Orig}
pnts(0).Y -= outerradius

pnts(1).X += innerradius * Sin36
pnts(1).Y -= innerradius * Cos36

pnts(2).X += outerradius * Sin72
pnts(2).Y -= outerradius * Cos72

pnts(3).X += innerradius * Sin72
pnts(3).Y += innerradius * Cos72


pnts(4).X += outerradius * Sin36
pnts(4).Y += outerradius * Cos36

pnts(5).Y += innerradius

pnts(6).X += pnts(6).X - pnts(4).X
pnts(6).Y = pnts(4).Y


pnts(7).X += pnts(7).X - pnts(3).X
pnts(7).Y = pnts(3).Y

pnts(8).X += pnts(8).X - pnts(2).X
pnts(8).Y = pnts(2).Y
pnts(9).X += pnts(9).X - pnts(1).X
pnts(9).Y = pnts(1).Y
Return pnts
End Function

Private Sub InitializeComponent()
Me.SuspendLayout()

Me.ClientSize = New System.Drawing.Size(292, 266)
Me.Name = "DrawingForm"
AddHandler Me.Load, New System.EventHandler(AddressOf Me.DrawingForm_Load)
Me.ResumeLayout(False)

End Sub

Private Sub DrawingForm_Load(sender As Object, e As EventArgs)

End Sub
End Class

Public Class Program

Public Shared Function Main() As Integer
Application.Run(New DrawingForm())
 

Return 0
 

End Function
End Class
End Namespace

0 comments:

Post a Comment