none
Help! (I know the username makes it ironic) RRS feed

  • Question

  • So I'm trying to write a code that lets you draw in a graphics window when you click and not draw when the mouse button is up.

    Here's my code:

    start:
      GraphicsWindow.MouseDown = OnMouseDown
      GraphicsWindow.MouseUp = OnMouseUp
      x = GraphicsWindow.MouseX
      y = GraphicsWindow.MouseY
      xl=x-1
      xr=x+1
      yu = y-1
      yd = y+1
      GraphicsWindow.Title = x + "," + y 
      Sub OnMouseDown
      GraphicsWindow.SetPixel(x,y,graphicswindow.GetRandomColor())
      GraphicsWindow.SetPixel(xl,y,graphicswindow.GetRandomColor())
      GraphicsWindow.SetPixel(xr,y,graphicswindow.GetRandomColor())
      GraphicsWindow.SetPixel(x,yu,graphicswindow.GetRandomColor())
      GraphicsWindow.SetPixel(x,yd,graphicswindow.GetRandomColor())
      GraphicsWindow.SetPixel(xl,yu,graphicswindow.GetRandomColor())
      GraphicsWindow.SetPixel(xr,yu,graphicswindow.GetRandomColor())
      GraphicsWindow.SetPixel(xl,yd,graphicswindow.GetRandomColor())
      GraphicsWindow.SetPixel(xr,yd,graphicswindow.GetRandomColor())
      Goto start
    EndSub


      Sub OnMouseUp
        Goto start
        EndSub 

    Now honestly I am brand new to sub routines so if there's anyone who could tell me more about that that would be awesome

    Anyway the error I keep gettin is:

    Bad label in ILGenerator

    If anyone can help me out please leave an answer.

    Thursday, February 16, 2012 4:18 AM

Answers

  • Hi,

    I think you missed a few things:

    a) Event Handler
    GraphicsWindow.MouseUp and GraphicsWindow.MouseDown take sub routines - but these sub routines are simply there to handle the core event. This means in your case: Just do something quick. Do NOT go into an endless loop!

    b) Think carefully about what you want to do. The application normaly looks like something like this:
    <Initialisation stuff>
    <Main Loop of application>
    <Set of routines>

    So initialisation could be to open the GraphicsWindow, set the event handler and so on.
    Main loop can be this start: ... goto start thing - but I would avoid goto statements. (Try to use for and while instead!)

    So lets think about your requirements:
    Your application should draw when the mouse is moved. When the mouse button is pressed, the drawing should start and when the mouse goes up again, nothing should be drawn. So we could define a variable draw - when the variable is 1, we draw else we do not draw.
    So thin about the OnMouseUp and OnMouseDown reoutines - what must they do? Think about the variable!

    Next the drawing - you was drawing a point, but think about a fast moving mouse. So maybe we remember the last position and then draw a line to the current position. But when the application starts, we do not have a first position. So we need to recognice that no line must be drawn. So we could store -1 inside the old location for this.

    So lets start to build the application:
    First initialisation - so we Show the graphics window, set draw to 0 and oldx / oldy to -1 and of course: Set the event handler for mouseup and mouse down.
    Then comes our main loop:
    We check if draw is 1 - in such a case we set x/y to the mousex/y. Then we check if we have to draw a line through a check if oldx is -1 or not. If it is not -1, we draw a line.
    Then we set oldx/oldy to x/y - outside the if oldx <> -1 check but still inside the draw=1 check.
    If we are not drawing, we want to start a new line - so we have to set oldx/oldy to -1 in case draw is not 1.

    That is the whole logic. Lets have a look, what application we got:

    GraphicsWindow.Show()
    GraphicsWindow.MouseDown = OnMouseDown
    GraphicsWindow.MouseUp = OnMouseUp
    oldx = -1
    oldy = -1
    draw = 0

    While(1=1)
      If (draw = 1) Then
        x = GraphicsWindow.MouseX
        y = GraphicsWindow.MouseY
        If (oldx <> -1) Then
          GraphicsWindow.DrawLine(oldx, oldy, x, y)
        EndIf
        oldx = x
        oldy = y
      Else
        oldx = -1
        oldy = -1
      EndIf
    EndWhile


    Sub OnMouseUp
      draw = 0
    EndSub

    Sub OnMouseDown
      draw = 1
    EndSub
     

    I hope you was able to follow my explanation first. If not: Feel free to ask. We will try to help you then to fully understand what it going on.

    With kind regards,

    Konrad

    Thursday, February 16, 2012 12:04 PM
  • Hello Programmerhelp

    This is a sample code

    GraphicsWindow.MouseDown = Mousedown

    Sub Mousedown

      While Mouse.IsLeftButtonDown

      GraphicsWindow.BrushColor = GraphicsWindow.GetRandomColor()

      GraphicsWindow.FillRectangle(GraphicsWindow.MouseX,GraphicsWindow.MouseY,5,5)

      EndWhile

      EndSub



    Wednesday, May 2, 2012 2:23 AM
    Answerer
  • This is an example that comes with SmallBasic. Please check your samples folder.

    GraphicsWindow.BackgroundColor = "Black"
    GraphicsWindow.PenColor = "White"
    GraphicsWindow.MouseDown = OnMouseDown
    GraphicsWindow.MouseMove = OnMouseMove
    GraphicsWindow.KeyDown = OnKeyDown
    Sub OnKeyDown
      If (GraphicsWindow.LastKey = "D1") Then
        GraphicsWindow.PenColor = "Red"
      endif
      If (GraphicsWindow.LastKey = "D2") Then
        GraphicsWindow.PenColor = "Blue"
      endif
      If (GraphicsWindow.LastKey = "D3") Then
        GraphicsWindow.PenColor = "LightGreen"
      endif
    EndSub
    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

    Kirk.
    Wednesday, May 2, 2012 9:50 AM
  • One more advice.  You should use Program.Delay() for CPU and other program.  For example,

    GraphicsWindow.MouseDown = OnMouseDown
    GraphicsWindow.MouseUp = OnMouseUp
    GraphicsWindow.MouseMove = OnMouseMove
    i = 0
    o = 0
    MAX = 100
    While "True"
      While o < i
        o = o + 1
        x = ax[Math.Remainder(o - 1, MAX) + 1]
        y = ay[Math.Remainder(o - 1, MAX) + 1]
        DrawDots()
      EndWhile
      Program.Delay(50)
    EndWhile
    Sub OnMouseDown
      clicked = "True"
    EndSub
    Sub OnMouseUp
      clicked = "False"
    EndSub
    Sub OnMouseMove
      mx = GraphicsWindow.MouseX
      my = GraphicsWindow.MouseY
      GraphicsWindow.Title = mx + "," + my
      If clicked Then
        i = i + 1
        ax[Math.Remainder(i - 1, MAX) + 1] = mx
        ay[Math.Remainder(i - 1, MAX) + 1] = my
      EndIf
    EndSub  
    Sub DrawDots
      xl = x - 1
      xr = x + 1
      yu = y - 1
      yd = y + 1
      For x = xl To xr
        For y = yu To yd
          GraphicsWindow.SetPixel(x, y, GraphicsWindow.GetRandomColor())
        EndFor
      EndFor
    EndSub
    


    Nonki Takahashi

    Wednesday, May 2, 2012 4:08 PM
    Moderator

All replies

  • Hi,

    I think you missed a few things:

    a) Event Handler
    GraphicsWindow.MouseUp and GraphicsWindow.MouseDown take sub routines - but these sub routines are simply there to handle the core event. This means in your case: Just do something quick. Do NOT go into an endless loop!

    b) Think carefully about what you want to do. The application normaly looks like something like this:
    <Initialisation stuff>
    <Main Loop of application>
    <Set of routines>

    So initialisation could be to open the GraphicsWindow, set the event handler and so on.
    Main loop can be this start: ... goto start thing - but I would avoid goto statements. (Try to use for and while instead!)

    So lets think about your requirements:
    Your application should draw when the mouse is moved. When the mouse button is pressed, the drawing should start and when the mouse goes up again, nothing should be drawn. So we could define a variable draw - when the variable is 1, we draw else we do not draw.
    So thin about the OnMouseUp and OnMouseDown reoutines - what must they do? Think about the variable!

    Next the drawing - you was drawing a point, but think about a fast moving mouse. So maybe we remember the last position and then draw a line to the current position. But when the application starts, we do not have a first position. So we need to recognice that no line must be drawn. So we could store -1 inside the old location for this.

    So lets start to build the application:
    First initialisation - so we Show the graphics window, set draw to 0 and oldx / oldy to -1 and of course: Set the event handler for mouseup and mouse down.
    Then comes our main loop:
    We check if draw is 1 - in such a case we set x/y to the mousex/y. Then we check if we have to draw a line through a check if oldx is -1 or not. If it is not -1, we draw a line.
    Then we set oldx/oldy to x/y - outside the if oldx <> -1 check but still inside the draw=1 check.
    If we are not drawing, we want to start a new line - so we have to set oldx/oldy to -1 in case draw is not 1.

    That is the whole logic. Lets have a look, what application we got:

    GraphicsWindow.Show()
    GraphicsWindow.MouseDown = OnMouseDown
    GraphicsWindow.MouseUp = OnMouseUp
    oldx = -1
    oldy = -1
    draw = 0

    While(1=1)
      If (draw = 1) Then
        x = GraphicsWindow.MouseX
        y = GraphicsWindow.MouseY
        If (oldx <> -1) Then
          GraphicsWindow.DrawLine(oldx, oldy, x, y)
        EndIf
        oldx = x
        oldy = y
      Else
        oldx = -1
        oldy = -1
      EndIf
    EndWhile


    Sub OnMouseUp
      draw = 0
    EndSub

    Sub OnMouseDown
      draw = 1
    EndSub
     

    I hope you was able to follow my explanation first. If not: Feel free to ask. We will try to help you then to fully understand what it going on.

    With kind regards,

    Konrad

    Thursday, February 16, 2012 12:04 PM
  • Just to add to what Konrad said, Bad label in ILGenerator is the error given when you use a Goto statement to jump to a label outside of a Sub-EndSub block.
    Friday, February 17, 2012 5:37 PM
  • Thanks you guys!
    Sunday, February 19, 2012 7:00 PM
  • Hello Programmerhelp

    This is a sample code

    GraphicsWindow.MouseDown = Mousedown

    Sub Mousedown

      While Mouse.IsLeftButtonDown

      GraphicsWindow.BrushColor = GraphicsWindow.GetRandomColor()

      GraphicsWindow.FillRectangle(GraphicsWindow.MouseX,GraphicsWindow.MouseY,5,5)

      EndWhile

      EndSub



    Wednesday, May 2, 2012 2:23 AM
    Answerer
  • This is an example that comes with SmallBasic. Please check your samples folder.

    GraphicsWindow.BackgroundColor = "Black"
    GraphicsWindow.PenColor = "White"
    GraphicsWindow.MouseDown = OnMouseDown
    GraphicsWindow.MouseMove = OnMouseMove
    GraphicsWindow.KeyDown = OnKeyDown
    Sub OnKeyDown
      If (GraphicsWindow.LastKey = "D1") Then
        GraphicsWindow.PenColor = "Red"
      endif
      If (GraphicsWindow.LastKey = "D2") Then
        GraphicsWindow.PenColor = "Blue"
      endif
      If (GraphicsWindow.LastKey = "D3") Then
        GraphicsWindow.PenColor = "LightGreen"
      endif
    EndSub
    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

    Kirk.
    Wednesday, May 2, 2012 9:50 AM
  • One more advice.  You should use Program.Delay() for CPU and other program.  For example,

    GraphicsWindow.MouseDown = OnMouseDown
    GraphicsWindow.MouseUp = OnMouseUp
    GraphicsWindow.MouseMove = OnMouseMove
    i = 0
    o = 0
    MAX = 100
    While "True"
      While o < i
        o = o + 1
        x = ax[Math.Remainder(o - 1, MAX) + 1]
        y = ay[Math.Remainder(o - 1, MAX) + 1]
        DrawDots()
      EndWhile
      Program.Delay(50)
    EndWhile
    Sub OnMouseDown
      clicked = "True"
    EndSub
    Sub OnMouseUp
      clicked = "False"
    EndSub
    Sub OnMouseMove
      mx = GraphicsWindow.MouseX
      my = GraphicsWindow.MouseY
      GraphicsWindow.Title = mx + "," + my
      If clicked Then
        i = i + 1
        ax[Math.Remainder(i - 1, MAX) + 1] = mx
        ay[Math.Remainder(i - 1, MAX) + 1] = my
      EndIf
    EndSub  
    Sub DrawDots
      xl = x - 1
      xr = x + 1
      yu = y - 1
      yd = y + 1
      For x = xl To xr
        For y = yu To yd
          GraphicsWindow.SetPixel(x, y, GraphicsWindow.GetRandomColor())
        EndFor
      EndFor
    EndSub
    


    Nonki Takahashi

    Wednesday, May 2, 2012 4:08 PM
    Moderator