📄 drawpanel.cs
字号:
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
namespace ACO
{
// The draw panel paints a network map and animates the nodes capacity
public class DrawPanel : System.Windows.Forms.UserControl
{
// the container for the component
private System.ComponentModel.Container components = null;
// the drawing coordinates for each node
public static Point[] coords = new Point[]
{
new Point(32,22), new Point(95,18), new Point(192,112), new Point(210,112),
new Point(125,268), new Point(152,259), new Point(192,226), new Point(225,214),
new Point(156,350), new Point(223,311), new Point(174,357), new Point(224,345),
new Point(281,349), new Point(125,456), new Point(206,423), new Point(249,403),
new Point(272,411), new Point(308,391), new Point(380,395), new Point(281,439),
new Point(294,443), new Point(308,447), new Point(303,460), new Point(295,468),
new Point(281,464), new Point(227,452), new Point(317,488), new Point(259,480),
new Point(277,451), new Point(179,501),
};
// the jagged array of all connected nodes in the network
private static int[][] Connections = new int[][]
{
new int[]{1,2,4}, new int[]{0,2,3}, new int[]{0,1,3,4,6}, new int[]{1,2,7},
new int[]{0,2,5,8}, new int[]{4,6,10}, new int[]{2,7,9,5}, new int[]{3,6,9,12},
new int[]{4,9,10,13}, new int[]{6,7,8,11,12}, new int[]{5,8,11,15,28,14,13}, new int[]{9,12,15,10},
new int[]{7,9,11,17}, new int[]{8,10,14,25,29}, new int[]{10,16,25,13}, new int[]{10,11,17,16},
new int[]{14,15,19}, new int[]{12,15,20,18}, new int[]{17,21}, new int[]{16,20},
new int[]{19,17,21}, new int[]{20,18,22}, new int[]{21,23,26}, new int[]{22,27},
new int[]{28,23,29}, new int[]{13,14,28,27,29}, new int[]{22,27}, new int[]{26,23,25},
new int[]{10,25,24}, new int[]{13,25,24}
};
// constructor for DrawPanel
public DrawPanel()
{
// initialise windows components
InitializeComponent();
// use double buffering for the network map rendering
SetStyle(ControlStyles.DoubleBuffer, true);
SetStyle(ControlStyles.UserPaint, true);
SetStyle(ControlStyles.AllPaintingInWmPaint, true);
Node n;
// scale panel
this.Width = this.Width * Convert.ToInt32(Global.Scale);
this.Height = this.Height * Convert.ToInt32(Global.Scale);
// instaciate all the nodes for the network with a unique id
for(int i=0;i<Global.NumberOfNodes;i++)
{
// assign the ith id
n = new Node(i);
// assign the ith coordinate
n.Coordinates = DrawPanel.coords[i];
// assign the ith connected nodes
n.Connections = Connections[i];
// add the node to the global array
Global.Nodes[i] = n;
}
// Generate Default Pheromone Tables after all nodes and connections have been
// built in the array
for(int i=0;i<Global.Nodes.Length;i++)
{
// generate the pheromone table
Global.Nodes[i].GenerateDefaultPheromoneTables();
}
// refresh the display
this.Refresh();
}
// this method is called when ever the GUI calls for the form to be repainted
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
Node n;
// create a bitmap for double buffering
Bitmap b = new Bitmap(this.Width, this.Height);
// get the graphics object from the bitmap
Graphics g = Graphics.FromImage(b);
// set the drawing mode to AntiAlias
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
// set the text mode to AntiAlias
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
// three loops are uses to paint so they are layered
// loop through all the nodes
for(int i=0;i<Global.Nodes.Length;i++)
{
n = Global.Nodes[i];
// draw all connections to the node
for(int j=0;j<n.Connections.Length;j++)
g.DrawLine(new Pen(Brushes.Black,1),n.Centroid, Global.Nodes[n.Connections[j]].Centroid);
}
// loop through all the nodes
for(int i=0;i<Global.NumberOfNodes;i++)
{
n = Global.Nodes[i];
// paint the node and fill it
g.FillEllipse(Brushes.White, n.Coordinates.X, n.Coordinates.Y, 10,10);
g.DrawEllipse(new Pen(Brushes.Black,1), n.Coordinates.X, n.Coordinates.Y, 10,10);
// if DontRouteTraffic is flagged then paint the node blue
if(n.DontRouteTraffic)
g.FillEllipse(Brushes.CornflowerBlue, n.Coordinates.X, n.Coordinates.Y, 10,10);
}
// loop through all the nodes
for(int i=0;i<Global.NumberOfNodes;i++)
{
n = Global.Nodes[i];
// set the coordinates to draw the id and curent call load
Point stringDraw1 = new Point(n.Centroid.X+8,n.Centroid.Y);
Point stringDraw2 = new Point(n.Centroid.X-12,n.Centroid.Y);
// draw the id and current call load of the node
g.DrawString(n.CallIDS.Count.ToString(),new Font("Arial",12),Brushes.Blue,stringDraw1);
g.DrawString(n.ID.ToString(),new Font("Arial",10),Brushes.Red,stringDraw2);
}
string str1 = "British Synchronous Digital \n Hierarchy (SDH) network.";
string str2 = "(Appleby & Steward, 1994)";
// draw title
g.DrawString(str1,new Font("Arial",10),Brushes.Black,200,30);
g.DrawString(str2,new Font("Arial",8),Brushes.Black,218,65);
// blt the bitmap the the screen
e.Graphics.DrawImage(b,0,0,this.Width, this.Height);
}
#region Component Designer generated code
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
//
// Panel
//
this.BackColor = System.Drawing.Color.White;
this.Name = "Panel";
this.Size = new System.Drawing.Size(416, 272);
}
#endregion
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -