Advertisement
Guest User

Untitled

a guest
Oct 4th, 2013
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 8.99 KB | None | 0 0
  1. Imports System.IO
  2.  
  3. Public Class MainForm
  4.     Dim nodes(-1) As nodes
  5.     Dim colorN As New System.Drawing.Pen(System.Drawing.Color.Red)
  6.     Dim colorL As New System.Drawing.Pen(System.Drawing.Color.Blue)
  7.     Dim colorT As System.Drawing.Brush = Brushes.Black
  8.     Dim colorR As New System.Drawing.Pen(DefaultBackColor)
  9.     Dim drawTheory As Boolean
  10.     Dim oldTheory As nodes
  11.     Dim newTheory As nodes
  12.     Dim drawID As Integer = -1
  13.  
  14.     Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  15.         Randomize()
  16.     End Sub
  17.  
  18.     Private Sub drawNode(n As nodes, g As System.Drawing.Graphics, cN As System.Drawing.Pen, cL As System.Drawing.Pen, cT As System.Drawing.Brush)
  19.         If Not n.calculated Then
  20.             n.calculated = True
  21.             If Not n.drawed Then
  22.                 g.DrawEllipse(cN, n.x, n.y, 20, 20)
  23.                 g.DrawString(n.id.ToString & " " & n.name, DefaultFont, cT, n.x - 5, n.y - 5)
  24.                 n.drawed = True
  25.             End If
  26.             For i As Integer = 0 To n.line.GetLength(0) - 1
  27.                 If nodes(n.line(i).toID).drawed Then
  28.                     g.DrawLine(cL, (nodes(n.line(i).toID).x + 10), (nodes(n.line(i).toID).y + 10), (n.x + 10), (n.y + 10))
  29.                     g.DrawString(n.line(i).weigth.ToString, DefaultFont, cT, Convert.ToInt32(nodes(n.line(i).toID).x / 2 + (n.x + 10) / 2), Convert.ToInt32(nodes(n.line(i).toID).y / 2 + (n.y + 10) / 2))
  30.                 Else
  31.                     g.DrawEllipse(cN, nodes(n.line(i).toID).x, nodes(n.line(i).toID).y, 20, 20)
  32.                     g.DrawString(nodes(n.line(i).toID).id.ToString & " " & nodes(n.line(i).toID).name, DefaultFont, cT, nodes(n.line(i).toID).x - 5, nodes(n.line(i).toID).y - 5)
  33.                     g.DrawString(n.line(i).weigth.ToString, DefaultFont, cT, Convert.ToInt32(nodes(n.line(i).toID).x / 2 + (n.x + 10) / 2), Convert.ToInt32(nodes(n.line(i).toID).y / 2 + (n.y + 10) / 2))
  34.                     nodes(n.line(i).toID).drawed = True
  35.                     g.DrawLine(cL, (nodes(n.line(i).toID).x + 10), (nodes(n.line(i).toID).y + 10), (n.x + 10), (n.y + 10))
  36.                 End If
  37.                 drawNode(nodes(n.line(i).toID), g, cN, cL, cT)
  38.             Next
  39.         End If
  40.     End Sub
  41.  
  42.     Private Sub drawNodeTheory(n As nodes, g As System.Drawing.Graphics, cN As System.Drawing.Pen, cL As System.Drawing.Pen)
  43.         g.DrawEllipse(cN, n.x, n.y, 20, 20)
  44.         For i As Integer = 0 To n.line.GetLength(0) - 1
  45.             g.DrawLine(cL, (nodes(n.line(i).toID).x + 10), (nodes(n.line(i).toID).y + 10), (n.x + 10), (n.y + 10))
  46.         Next
  47.     End Sub
  48.  
  49.     Private Sub Button2_Click(sender As System.Object, e As System.EventArgs)
  50.         newTheory = New nodes
  51.         oldTheory = New nodes
  52.         newTheory.id = nodes.GetLength(0)
  53.         For i As Integer = 0 To CheckedListBox1.CheckedItems.Count + 1
  54.             If CheckedListBox1.CheckedItems.IndexOf(i) > -1 Then
  55.                 newTheory.newVerbindung(Convert.ToInt32(CheckedListBox1.Items.IndexOf(i)), Convert.ToInt32(201 * Rnd()))
  56.             End If
  57.         Next
  58.         drawTheory = True
  59.  
  60.     End Sub
  61.  
  62.     Private Sub MainForm_MouseMove(sender As System.Object, e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove
  63.         If drawTheory Then
  64.             newTheory.x = e.X
  65.             newTheory.y = e.Y
  66.             Me.Invalidate()
  67.         End If
  68.  
  69.     End Sub
  70.  
  71.     Private Sub MianForm_MouseClick(sender As System.Object, e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseClick
  72.         If drawTheory Then
  73.             If e.Button = Windows.Forms.MouseButtons.Right Then
  74.                 newTheory = New nodes
  75.                 oldTheory = New nodes
  76.             End If
  77.             If e.Button = Windows.Forms.MouseButtons.Left Then
  78.                 ReDim Preserve nodes(nodes.GetLength(0))
  79.                 nodes(nodes.GetLength(0) - 1) = New nodes
  80.                 nodes(nodes.GetLength(0) - 1) = newTheory
  81.                 CheckedListBox1.Items.Add(nodes.GetLength(0) - 1)
  82.                 drawID = nodes.GetLength(0) - 1
  83.                 For i As Integer = 0 To nodes.GetLength(0) - 1
  84.                     nodes(i).drawed = False
  85.                     nodes(i).calculated = False
  86.                 Next
  87.             End If
  88.             drawTheory = False
  89.  
  90.             Dim region As System.Drawing.Region
  91.             region = Me.Region
  92.  
  93.             Me.Invalidate(region)
  94.  
  95.         End If
  96.     End Sub
  97.  
  98.     Private Sub MainForm_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
  99.         For i As Integer = 0 To nodes.GetLength(0) - 1
  100.             nodes(i).drawed = False
  101.             nodes(i).calculated = False
  102.         Next
  103.         If drawTheory Then
  104.             drawNodeTheory(oldTheory, e.Graphics, colorR, colorR)
  105.             drawNodeTheory(newTheory, e.Graphics, colorN, colorL)
  106.             oldTheory = newTheory
  107.         End If
  108.         For i As Integer = 0 To nodes.GetLength(0) - 1
  109.             If Not nodes(i).calculated Then
  110.                 drawNode(nodes(i), e.Graphics, colorN, colorL, colorT)
  111.             End If
  112.         Next
  113.     End Sub
  114.  
  115.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  116.         If Not CheckBox1.Checked Then
  117.             Button1.Visible = False
  118.             Using sr As StreamReader = New StreamReader("bahnhof.txt")
  119.                 Dim line As String
  120.                 Dim break As Boolean
  121.                 Dim array(2) As String
  122.                 Do
  123.                     line = sr.ReadLine()
  124.                     If Not line = "#Kanten" Then
  125.                         ReDim Preserve nodes(nodes.GetLength(0))
  126.                         nodes(nodes.GetLength(0) - 1) = New nodes
  127.                         nodes(nodes.GetLength(0) - 1).id = nodes.GetLength(0) - 1
  128.                         nodes(nodes.GetLength(0) - 1).name = sr.ReadLine()
  129.                         nodes(nodes.GetLength(0) - 1).x = Convert.ToInt32(40 + ((Me.Width - 40) - 40 + 1) * Rnd()) - 20
  130.                         nodes(nodes.GetLength(0) - 1).y = Convert.ToInt32(40 + ((Me.Height - 40) - 40 + 1) * Rnd()) - 20
  131.                     Else
  132.                         break = True
  133.                     End If
  134.                 Loop Until break
  135.                 Do
  136.                     line = sr.ReadLine()
  137.                     If Not line Is Nothing Then
  138.                         array = Split(line)
  139.                         If Convert.ToInt32(array(1)) > Convert.ToInt32(array(0)) Then
  140.                             nodes(Convert.ToInt32(array(0)) - 1).newVerbindung(Convert.ToInt32(array(1)) - 1, Convert.ToInt32(array(2)))
  141.                         Else
  142.                             nodes(Convert.ToInt32(array(1)) - 1).newVerbindung(Convert.ToInt32(array(0)) - 1, Convert.ToInt32(array(2)))
  143.                         End If
  144.  
  145.                     End If
  146.                 Loop Until line Is Nothing
  147.                 Me.Refresh()
  148.                 sr.Close()
  149.             End Using
  150.         Else
  151.             newTheory = New nodes
  152.             oldTheory = New nodes
  153.             newTheory.id = nodes.GetLength(0)
  154.             For i As Integer = 0 To CheckedListBox1.CheckedItems.Count + 1
  155.                 If CheckedListBox1.CheckedItems.IndexOf(i) > -1 Then
  156.                     newTheory.newVerbindung(Convert.ToInt32(CheckedListBox1.Items.IndexOf(i)), Convert.ToInt32(201 * Rnd()))
  157.                 End If
  158.             Next
  159.             drawTheory = True
  160.         End If
  161.     End Sub
  162.  
  163.     Private Sub CheckBox1_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles CheckBox1.CheckedChanged
  164.         ReDim nodes(-1)
  165.         Button1.Visible = True
  166.         CheckedListBox1.Items.Clear()
  167.         If Not CheckBox1.Checked Then
  168.             Button1.Text = "Zeichne aus der Datei ""bahnhof.txt"""
  169.             CheckedListBox1.Visible = False
  170.             Label1.Visible = False
  171.         Else
  172.             Button1.Text = "Zeichne im Kreativmodus"
  173.             CheckedListBox1.Visible = True
  174.             Label1.Visible = True
  175.         End If
  176.         Me.Refresh()
  177.     End Sub
  178.  
  179.     Private Sub CheckedListBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles CheckedListBox1.SelectedIndexChanged
  180.  
  181.     End Sub
  182. End Class
  183.  
  184. Public Class nodes
  185.     Public line(-1) As lines
  186.     Public name As String
  187.     Public id As Integer = 0
  188.     Public x As Integer = 0
  189.     Public y As Integer = 0
  190.     Public drawed As Boolean = False
  191.     Public calculated As Boolean = False
  192.  
  193.     Public Sub newVerbindung(ByVal id As Integer, ByVal weigth As Integer)
  194.         For i = 0 To line.GetLength(0) - 1
  195.             If line(i).toID = id Then
  196.                 Return
  197.             End If
  198.         Next
  199.         ReDim Preserve line(line.GetLength(0))
  200.         line(line.GetLength(0) - 1) = New lines
  201.         line(line.GetLength(0) - 1).toID = id
  202.         line(line.GetLength(0) - 1).weigth = weigth
  203.     End Sub
  204.  
  205. End Class
  206.  
  207. Public Class lines
  208.     Public toID As Integer
  209.     Public weigth As Integer
  210. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement