private void DrawEdge(GraphPoint PointA, GraphPoint PointB, int weight, Color color) { Graphics g = CreateGraphics(); Pen p = new Pen(color); g.DrawLine(p, PointB.X + 10, PointB.Y + 10, PointA.X + 10, PointA.Y + 10); //Label weight if (weight != -1) { int xMid = Math.Abs((PointB.X + 10) - (PointA.X + 10)) / 2; int yMid = Math.Abs((PointB.Y + 10) - (PointA.Y + 10)) / 2; Point choosenPoint = new Point(); if (PointB.X > PointA.X) { choosenPoint.X = PointA.X + xMid - 5; } else { choosenPoint.X = PointB.X + xMid - 5; } if (PointB.Y > PointA.Y) { choosenPoint.Y = PointA.Y + yMid - 5; } else { choosenPoint.Y = PointB.Y + yMid - 5; } g.DrawString(weight.ToString(), new Font("Arial", 8), new SolidBrush(Color.Blue), choosenPoint); } }
private void DrawNumber(GraphPoint vertex) { Graphics g = Graphics.FromImage(vertex.Image); g.DrawString(vertex.number.ToString(), new Font("Microsoft Sans Serif", 30), Brushes.Black, new Point(80, 80)); }
private void Form1_MouseClick(object sender, MouseEventArgs e) { i++; GraphPoint vertex = new GraphPoint(e.X - 10, e.Y - 10, i); MyGraph.AddVertex(vertex); vertex.Click += new System.EventHandler(this.picturePoint_click); // += add to event this.Controls.Add(vertex); DrawNumber(vertex); }
void picturePoint_click(object sender, EventArgs e) { var pic = sender as GraphPoint; if (pic.status) //Unselect button { pic.Image = new Bitmap(@"img\button.png"); pic.status = false; DrawNumber(pic); } else //Find out if another button was selected and is waiting for connection { GraphPoint buttonThatWasFinded = Graph.vertices.Find(x => x.status == true); if (buttonThatWasFinded != null) //if YES then make connection { //Make edge //Random weight Random rand = new Random(); int weight = rand.Next(1, 100); //Draw Edge and lable weight DrawEdge(pic, buttonThatWasFinded, weight, Color.Black); //set default status buttonThatWasFinded.status = false; pic.status = false; buttonThatWasFinded.Image = new Bitmap(@"img\button.png"); //add edge in graph list MyGraph.AddEdge(pic, buttonThatWasFinded, weight); DrawNumber(pic); DrawNumber(buttonThatWasFinded); } else //if NO then denote this button as selected { pic.status = true; pic.Image = new Bitmap(@"img\buttonPressed.png"); DrawNumber(pic); } } }