120 likes | 303 Views
Events and interactivity. IP 10 Mr. Mellesmoen 2014. How are events useful?. So far all we have create some interactive programs, but nothing too fancy….yet. Today we will learn about events and interactivity. Looking at a simple program first. Try this:
E N D
Events and interactivity IP 10 Mr. Mellesmoen 2014
How are events useful? So far all we have create some interactive programs, but nothing too fancy….yet. Today we will learn about events and interactivity
Looking at a simple program first Try this: Graphics.Window.MouseDown = OnMouseDown Sub OnMouseDown GraphicsWindow.ShowMessage(“You Clicked.”, “Hello”) EndSub
Clicking So that was pretty basic, whenever you clicked on the graphic window you got your message. Let’s try something else…see the next slide…
A bunch of dots GraphicsWindow.BrushColor="Blue" GraphicsWindow.MouseDown= OnMouseDown Sub OnMouseDown x= GraphicsWindow.mouseX-10 y= GraphicsWindow.mouseY-10 GraphicsWindow.FillEllipse(x, y, 20, 20) EndSub Try changing SHAPES (I.E. Fillrectangle)
Multiple events You can create subroutines in this program that will change what a program does if the user performs a specific action. The next program illustrates this by using Key Strokes.
Random colors GraphicsWindow.BrushColor="Red" GraphicsWindow.MouseDown= OnMouseDown GraphicsWindow.KeyDown = OnKeyDown Sub OnKeyDown GraphicsWindow.BrushColor=GraphicsWindow.GetRandomColor() EndSub Sub OnMouseDown x= GraphicsWindow.MouseX-10 y= GraphicsWindow.MouseY-10 GraphicsWindow.FillEllipse(x, y, 20, 20) EndSub
A paint program Now we can create a program that allows the user to draw with the mouse in the graphics window. On the next slide is the information you need
A paint program GraphicsWindow.MouseMove = OnMouseMove Sub OnMouseMove x = GraphicsWindow.MouseX y = GraphicsWindow.MouseY GraphicsWindow.DrawLine (prevX, prevY, x, y) prevX = x prevY = y EndSub
Did you notice… Every time you run the program your drawing starts in the top left corner (0, 0) You should have also noticed that you just need to move the mouse and really can’t ever stop drawing. Adding a MouseDown and IsLeftButtonDown property will fix this up. See next slide…
A better paint program GraphicsWindow.MouseMove = OnMouseMove GraphicsWindow.MouseDown= OnMouseDown Sub OnMouseDown PrevX = GraphicsWindow.MouseX PrevY= GraphicsWindow.MouseY EndSub Sub OnMouseMove x = GraphicsWindow.MouseX y = GraphicsWindow.MouseY If (Mouse.IsLeftButtonDown) Then GraphicsWindow.DrawLine (prevX, prevY, x, y) EndIf prevX = x prevY = y EndSub