Results 1 to 9 of 9

Thread: Fun with graphics

Threaded View

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    May 2005
    Posts
    898

    Fun with graphics

    Ok, I'd like y'all to check this out, it doesn't teach anything about GDI+ but it's fun to look at.

    You need a project with a Form with a button (Button1)

    when you've started the prog, click the form and move the mouse around. (StepZ is controlled by the mousewheel)

    VB Code:
    1. Public Class Form1
    2.     Inherits System.Windows.Forms.Form
    3.  
    4.     '###
    5.     ' Windows Form Designer generated code
    6.     '###
    7.  
    8.     Private TheTimer As Timer
    9.     Private BM1 As New Bitmap(100, 100)
    10.     Private fgr As Graphics = Me.CreateGraphics
    11.  
    12.     Private StepX As Double = 16 ^ -1
    13.     Private StepY As Double = 12 ^ -1
    14.     Private StepZ As Double = 8 ^ -1
    15.  
    16.  
    17.     Private Sub Button1_Click(ByVal sender As System.Object, _
    18.                            ByVal e As System.EventArgs) Handles Button1.Click
    19.         If TheTimer Is Nothing Then
    20.             UpdateInfo()
    21.             TheTimer = New Timer
    22.             TheTimer.Interval = 50
    23.             AddHandler TheTimer.Tick, AddressOf Draw
    24.             TheTimer.Enabled = True
    25.         Else
    26.             TheTimer.Enabled = Not TheTimer.Enabled
    27.         End If
    28.     End Sub
    29.  
    30.     Private Sub Draw(ByVal Sender As Object, ByVal e As System.EventArgs)
    31.         Static OffsetX As Double = 0
    32.         Static OffsetY As Double = 0
    33.         Static OffsetZ As Double = 0
    34.         Static z As Integer = 0
    35.  
    36.         Dim y As Integer
    37.         Dim xred As Integer
    38.         Dim ygreen As Integer
    39.         Dim zblue As Integer = CInt((1 + Math.Sin(OffsetZ + z / BM1.Width * 2 * Math.PI)) * 127)
    40.  
    41.         For x As Integer = 0 To BM1.Width - 1
    42.             xred = CInt((1 + Math.Sin(OffsetX + x / BM1.Width * 2 * Math.PI)) * 127)
    43.  
    44.             For y = 0 To BM1.Height - 1
    45.                 ygreen = CInt((1 + Math.Sin(OffsetY + y / BM1.Height * 2 * Math.PI)) * 127)
    46.                 BM1.SetPixel(x, y, Color.FromArgb(xred, ygreen, zblue))
    47.  
    48.             Next y
    49.         Next x
    50.  
    51.         fgr.DrawImage(BM1, 0, 0, 600, 600)
    52.  
    53.         OffsetX += 2 * Math.PI * StepX
    54.         If OffsetX > 2 * Math.PI Then OffsetX -= 2 * Math.PI
    55.         OffsetY += 2 * Math.PI * StepY
    56.         If OffsetY > 2 * Math.PI Then OffsetY -= 2 * Math.PI
    57.         OffsetZ += 2 * Math.PI * StepZ
    58.         If OffsetZ > 2 * Math.PI Then OffsetZ -= 2 * Math.PI
    59.     End Sub
    60.  
    61.     Private Sub Form1_MouseMove(ByVal sender As Object, _
    62.                                               ByVal e As System.Windows.Forms.MouseEventArgs) _
    63.                                                   Handles MyBase.MouseMove
    64.         Const MaxStep As Double = 1 / 10
    65.         If e.Button = MouseButtons.Left Then
    66.             StepX = (1 + 2 * (e.X - Me.Width) / Me.Width) * MaxStep
    67.             StepY = (1 + 2 * (e.Y - Me.Width) / Me.Width) * MaxStep
    68.             UpdateInfo()
    69.         End If
    70.     End Sub
    71.  
    72.     Private Sub UpdateInfo()
    73.         Me.Text = "StepX:" & StepX.ToString("0.###") & _
    74.                   " StepY:" & StepY.ToString("0.###") & _
    75.                   " StepZ:" & StepZ.ToString("0.###")
    76.     End Sub
    77.  
    78.     Private Sub Form1_MouseWheel(ByVal sender As Object, _
    79.                                  ByVal e As System.Windows.Forms.MouseEventArgs) _
    80.                                  Handles MyBase.MouseWheel
    81.  
    82.         If Not e.Delta.Equals(0) Then
    83.             Select Case e.Delta > 0
    84.                 Case True
    85.                     StepZ += 0.005
    86.                     If StepZ > 0.1 Then StepZ = 0.1
    87.                 Case False
    88.                     StepZ -= 0.005
    89.                     If StepZ < -0.1 Then StepZ = -0.1
    90.             End Select
    91.             UpdateInfo()
    92.         End If
    93.  
    94.     End Sub
    95. End Class
    Last edited by grilkip; Jul 1st, 2005 at 03:26 PM. Reason: deleted some surpluss crap
    "so just keep in mind that fantasy is not the same as realtiy and make sure u remember that wii sports may be fun but u cant count on it as exercise ok cool bye" - HungarianHuman

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width