Tipp 0073 Ampel zeichnen
Autor/Einsender:
Datum:
  Michael Werner
11.04.2005
Entwicklungsumgebung:   VB.Net 2003
Framework:   1.1
Mit Hilfe von GDI+-Zeichenmethoden, insbesondere der Klasse GraphicsPath wird eine animierte Ampel gezeichnet.
 
Imports System.Drawing.Drawing2D

Public Class Form1
  Inherits System.Windows.Forms.Form
Vom Windows Form Designer generierter Code
  Dim g As Graphics = Me.CreateGraphics

  Dim R As Rectangle = New Rectangle(125, 25, 40, 110)
  Dim R_1 As RectangleF = New RectangleF(130, 30, 30, 30)
  Dim R_2 As RectangleF = New RectangleF(130, 65, 30, 30)
  Dim R_3 As RectangleF = New RectangleF(130, 100, 30, 30)

  Dim B_red As Brush = Brushes.Red
  Dim B_green As Brush = Brushes.YellowGreen
  Dim B_yellow As Brush = Brushes.Yellow

  Dim B_darkred As Brush = Brushes.DarkRed
  Dim B_darkgreen As Brush = Brushes.DarkOliveGreen
  Dim B_darkyellow As Brush = Brushes.DarkGoldenrod

  Dim P_black As Pen = New Pen(Color.Black, 2)

  Private Sub Form1_Paint(ByVal sender As Object, _
        ByVal e As System.Windows.Forms.PaintEventArgs) _
        Handles MyBase.Paint

    Dim myPath As New GraphicsPath
    myPath.AddRectangle(R)
    myPath.AddEllipse(R_1)
    myPath.AddEllipse(R_2)
    myPath.AddEllipse(R_3)
    myPath.FillMode = FillMode.Alternate
    g.DrawPath(New Pen(Color.Black, 3), myPath)
    g.FillPath(Brushes.DarkGray, myPath)

    g.DrawEllipse(P_black, R_1)
    g.DrawEllipse(P_black, R_2)
    g.DrawEllipse(P_black, R_3)

    ROT()
  End Sub

  Private Sub Button1_Click(ByVal sender As System.Object, _
          ByVal e As System.EventArgs) Handles Button1.Click
    Timer1.Enabled = Not Timer1.Enabled
    Timer2.Enabled = Not Timer2.Enabled
    If Timer1.Enabled Then
      Button1.Text = "&Ampel ausschalten"
      StatusBar1.Text = "Ampel ist eingeschaltet."
    Else
      Button1.Text = "&Ampel einschalten"
      StatusBar1.Text = "Ampel ist ausgeschaltet."
    End If
  End Sub

#Region "Timer"
  Private Sub Timer1_Tick(ByVal sender As System.Object, _
          ByVal e As System.EventArgs) Handles Timer1.Tick
    Static i As Int32
    i += 1
    If i > 4 Then
      i = 1
    End If
    Select Case i
      Case 1
        ROT()
      Case 2
        ROTGELB()
      Case 3
        GRUEN()
      Case 4
        GELB()
    End Select
  End Sub

  Private Sub Timer2_Tick(ByVal sender As System.Object, _
          ByVal e As System.EventArgs) Handles Timer2.Tick
    Static i As Int32
    i += 1
    If i > 2 Then
      i = 1
    End If
    If i Mod 2 = 0 Then
      AUS()
    Else
      GELB()
    End If
  End Sub
#End Region

#Region "Ampelzustände"
  Private Sub ROT()
    g.FillEllipse(B_red, R_1)
    g.FillEllipse(B_darkyellow, R_2)
    g.FillEllipse(B_darkgreen, R_3)
  End Sub

  Private Sub ROTGELB()
    g.FillEllipse(B_red, R_1)
    g.FillEllipse(B_yellow, R_2)
    g.FillEllipse(B_darkgreen, R_3)
  End Sub

  Private Sub GRUEN()
    g.FillEllipse(B_green, R_3)
    g.FillEllipse(B_darkred, R_1)
    g.FillEllipse(B_darkyellow, R_2)
  End Sub

  Private Sub GELB()
    g.FillEllipse(B_yellow, R_2)
    g.FillEllipse(B_darkred, R_1)
    g.FillEllipse(B_darkgreen, R_3)
  End Sub

  Private Sub AUS()
    g.FillEllipse(B_darkred, R_1)
    g.FillEllipse(B_darkyellow, R_2)
    g.FillEllipse(B_darkgreen, R_3)
  End Sub
#End Region

  Private Sub Button2_Click(ByVal sender As System.Object, _
          ByVal e As System.EventArgs) Handles Button2.Click
    Me.Close()
  End Sub
End Class
 
Weitere Links zum Thema
Geometrische Figuren zeichnen
Kuchendiagramm erstellen
Text zeichnen

Windows-Version
98/SE
ME
NT
2000
XP
Vista
Win 7


Download  (6,5 kB) Downloads bisher: [ 1467 ]

Vorheriger Tipp Zum Seitenanfang Nächster Tipp

Startseite | Tipps | Projekte | Tutorials | Bücherecke | VB-/VBA-Tipps | API-Referenz | Komponenten | VB.Net-Forum | VB/VBA-Forum | DirectX-Forum | Foren-Archiv | DirectX | Chat | Spielplatz | Links | Suchen | Stichwortverzeichnis | Feedback | Impressum

Seite empfehlen Bug-Report
Letzte Aktualisierung: Montag, 23. Januar 2012