none
Paint Palette program help? RRS feed

  • Question

  • Hi, I'm quite new to programming and have started it at school. I've found it pretty fun and everything, but also very hard. 

    I'm trying to create a simple paint program. At the moment, I cannot get the cursor to click on any of the other colours and get the brush colour to change to that specific colour. it only works with red. I also want the cursor to not be able to paint over any of the colour pixels. I do have an idea as to what to do - I just don't know how to do it. 

    A little bit of help would be very much appreciated. 

    program code; JHQ298 

    Monday, December 1, 2014 7:09 PM

Answers

  • Hi Annie and welcome to the SB forum.

    Here's a quick sample (<80 min) I did for a Paint program:  NNH855-0. Runs locally.

    It might give you some ideas. The sample requires an internet connection to d/load the pallet, otherwise you'll get an error message.

    Happy coding.

    btw:

    Coding does seem hard at first especially if you want to do a lot. Be patient, work through the curriculum and try some stuff. Make some mistakes, try to learn what works and what doesn't.

    Take your time, keep practicing and gradually your skill will grow.


    Tuesday, December 2, 2014 10:30 PM
    Moderator
  • Hello Annie,

    here is a rough sample program. I removed it from one of my first programs so there are some bugs lurking, but overall it works and, ultimately,  it will help you get started and understand the basics. Note: To start drawing, click on the " paint " button. If you want to change the ink color, you'll need to click on the rectangle of the your desired color then click the " change color " button. Hope this helps. If you have any questions or if the sample I provided doesn't work or fit your needs please let me know. Have fun programming!  

    GraphicsWindow.Title = "Pixel Painter 0.1"
    GraphicsWindow.Width = Desktop.Width '1366 px
    GraphicsWindow.Height = Desktop.Height '768 px
    GraphicsWindow.Top = 0
    GraphicsWindow.Left = 0
    GraphicsWindow.CanResize = "False"
    
    selectionInterface()
    Controls.ButtonClicked = selectTool 
    
    Sub selectionInterface
      GraphicsWindow.BackgroundColor = "White"
      
      'Border separates canvas from tools 
      GraphicsWindow.PenColor = "Black"
      GraphicsWindow.BrushColor = "Ivory"
      GraphicsWindow.DrawRectangle(0,0,195,745) 'tool box
      GraphicsWindow.FillRectangle(0,0,195,745) 'tool box
      
      GraphicsWindow.BrushColor = "Blue"
      
      'Buttons
      drawButton = Controls.AddButton("Paint",20,190)
      Controls.SetSize(drawButton,155,45)
      ChangeColorButton = Controls.AddButton("Change Color",20,520) 'change color
      Controls.SetSize(changeColorButton,160,30)
      
      'Color selection boxes
      GraphicsWindow.PenColor = "Black" 'border of all color selector boxes
      GraphicsWindow.BrushColor = "Black" 'Black
      blackBlock = Shapes.AddRectangle(65,30)
      Shapes.Move(blackBlock,20,305)
      GraphicsWindow.BrushColor = "White" 'White
      whiteBlock = Shapes.AddRectangle(65,30)
      Shapes.Move(whiteBlock,110,305)
      GraphicsWindow.BrushColor = "Red" 'Red
      redBlock = Shapes.AddRectangle(65,30)
      Shapes.Move(redBlock,20,340)
      GraphicsWindow.BrushColor = "Blue" 'Blue
      blueBlock = Shapes.AddRectangle(65,30)
      Shapes.Move(blueBlock,110,340)
      GraphicsWindow.BrushColor = "pink" 'Pink
      pinkBlock = Shapes.AddRectangle(65,30)
      Shapes.Move(pinkBlock,20,375)
      GraphicsWindow.BrushColor = "Orange" 'Orange
      orangeBlock = Shapes.AddRectangle(65,30)
      Shapes.Move(orangeBlock,110,375)
      GraphicsWindow.BrushColor = "Yellow" 'Yellow
      yellowBlock = Shapes.AddRectangle(65,30)
      Shapes.Move(yellowBlock,20,410)
      GraphicsWindow.BrushColor = "Purple" 'Purple
      purpleBlock = Shapes.AddRectangle(65,30)
      Shapes.Move(purpleBlock,110,410)
      GraphicsWindow.BrushColor = "Brown" 'Brown
      brownBlock = Shapes.AddRectangle(65,30)
      Shapes.Move(brownBlock,20,445)
      GraphicsWindow.BrushColor = "Gray" 'Gray
      grayBlock = Shapes.AddRectangle(65,30)
      Shapes.Move(grayBlock,110,445)
      GraphicsWindow.BrushColor = "Green" 'Green
      greenBlock = Shapes.AddRectangle(65,30)
      Shapes.Move(greenBlock,20,480)
      
      GraphicsWindow.BrushColor = "Black" 'For color indicator box; leave here
      GraphicsWindow.PenColor = "Black" 'For color indicator box; leave here
    EndSub 
    
    Sub selectTool 
      GraphicsWindow.MouseMove = moveMouse
      GraphicsWindow.MouseDown = pressDown
      
      If (color = 1) Then 
        GraphicsWindow.PenColor = "Black"
        GraphicsWindow.BrushColor = "Black"
      ElseIf (color = 2) Then 
        GraphicsWindow.PenColor = "White"
        GraphicsWindow.BrushColor = "White"
      ElseIf (color = 3) Then 
        GraphicsWindow.PenColor = "Red"
        GraphicsWindow.BrushColor = "Red"
      ElseIf (color = 4) Then 
        GraphicsWindow.PenColor = "Blue"
        GraphicsWindow.BrushColor = "Blue"
      ElseIf (color = 5) Then 
        GraphicsWindow.PenColor = "Pink"
        GraphicsWindow.BrushColor = "Pink"
      ElseIf (color = 6) Then 
        GraphicsWindow.PenColor = "Orange"
        GraphicsWindow.BrushColor = "Orange"
      ElseIf (color = 7) Then 
        GraphicsWindow.PenColor = "Yellow"
        GraphicsWindow.BrushColor = "Yellow" 
      ElseIf (color = 8) Then 
        GraphicsWindow.PenColor = "Purple"
        GraphicsWindow.BrushColor = "Purple" 
      ElseIf (color = 9) Then 
        GraphicsWindow.PenColor = "Brown"
        GraphicsWindow.BrushColor = "Brown" 
      ElseIf (color = 10) Then 
        GraphicsWindow.PenColor = "Gray"
        GraphicsWindow.BrushColor = "Gray" 
      ElseIf (color = 11) Then 
        GraphicsWindow.PenColor = "Green"
        GraphicsWindow.BrushColor = "Green"
      EndIf 
      
      If (Controls.GetButtonCaption(Controls.LastClickedButton) = "Paint" Or Controls.GetButtonCaption(Controls.LastClickedButton) = "Change Color") Then 
        pos = 1
      EndIf 
      
      'This must be placed last; Box that indicates current color 
      colorIndicator = Shapes.AddRectangle(160,40)
      Shapes.Move(colorIndicator,20,555)
    EndSub 
    
    Sub moveMouse
      x = GraphicsWindow.MouseX 
      y = GraphicsWindow.MouseY 
      
      'Allows shapes and pen to follow cursor
      If (Mouse.IsLeftButtonDown) Then 
        If (x > 195) Then 
          If (pos = 1) Then 
            drawLine = Shapes.AddLine(prevX,prevY,x,y) 'drawing line for "paint" button; not line button
          EndIf
        EndIf 
      EndIf 
      
      prevX = x 'This variable must be after all drawLine statements and outside all if else statements 
      prevY = y 'This variable must be after all drawLine statements and outside all if else statements  
    EndSub 
    
    Sub pressDown 
      prevX = GraphicsWindow.MouseX 
      prevY = GraphicsWindow.MouseY  
      
      'Selects color & disables all tools in specified area then corresponds with color statements in sub selectTool
      If (x > 20 And x < 80 And y > 300 And y < 330) Then 
        pos = 0 
        color = 1
      ElseIf (x > 110 And x < 180 And y > 300 And y < 330) Then 
        pos = 0 
        color = 2 
      ElseIf (x > 20 And x < 80 And y > 335 And y < 365) Then 
        pos = 0 
        color = 3 
      ElseIf (x > 110 And x < 180 And y > 335 And y < 365) Then 
        pos = 0 
        color = 4
      ElseIf (x > 20 And x < 80 And y > 370 And y < 400) Then 
        pos = 0
        color = 5 
      ElseIf (x > 110 And x < 180 And y > 370 And y < 400) Then 
        pos = 0
        color = 6 
      ElseIf (x > 20 And x < 80 And y > 405 And y < 435) Then 
        pos = 0
        color = 7
      ElseIf (x > 110 And x < 180 And y > 405 And y < 435) Then 
        pos = 0
        color = 8 
      ElseIf (x > 20 And x < 80 And y > 440 And y < 470) Then 
        pos = 0 
        color = 9
      ElseIf (x > 110 And x < 180 And y > 440 And y < 470) Then 
        pos = 0 
        color = 10 
      ElseIf (x > 20 And x < 80 And y > 475 And y < 505) Then 
        pos = 0 
        color = 11
      EndIf 
    EndSub 

    Also, I believe there was a monthly challenge for this specific program. There may be some more programs and insight there. Here's the link:

    https://social.msdn.microsoft.com/Forums/en-US/64c300a5-334b-450a-bdbd-fb368d8cb2ba/challenge-of-the-month-october-2014?forum=smallbasic



    Tuesday, December 2, 2014 3:08 AM
  • Annie, welcome to Small Basic forum!

    Change

    getcolour = GraphicsWindow.GetPixel(10,10)

    to

    getcolour = GraphicsWindow.GetPixel(GraphicsWindow.MouseX,GraphicsWindow.MouseY)

    in your code.


    Nonki Takahashi

    Wednesday, December 3, 2014 6:52 AM
    Moderator

All replies

  • Hello Annie,

    here is a rough sample program. I removed it from one of my first programs so there are some bugs lurking, but overall it works and, ultimately,  it will help you get started and understand the basics. Note: To start drawing, click on the " paint " button. If you want to change the ink color, you'll need to click on the rectangle of the your desired color then click the " change color " button. Hope this helps. If you have any questions or if the sample I provided doesn't work or fit your needs please let me know. Have fun programming!  

    GraphicsWindow.Title = "Pixel Painter 0.1"
    GraphicsWindow.Width = Desktop.Width '1366 px
    GraphicsWindow.Height = Desktop.Height '768 px
    GraphicsWindow.Top = 0
    GraphicsWindow.Left = 0
    GraphicsWindow.CanResize = "False"
    
    selectionInterface()
    Controls.ButtonClicked = selectTool 
    
    Sub selectionInterface
      GraphicsWindow.BackgroundColor = "White"
      
      'Border separates canvas from tools 
      GraphicsWindow.PenColor = "Black"
      GraphicsWindow.BrushColor = "Ivory"
      GraphicsWindow.DrawRectangle(0,0,195,745) 'tool box
      GraphicsWindow.FillRectangle(0,0,195,745) 'tool box
      
      GraphicsWindow.BrushColor = "Blue"
      
      'Buttons
      drawButton = Controls.AddButton("Paint",20,190)
      Controls.SetSize(drawButton,155,45)
      ChangeColorButton = Controls.AddButton("Change Color",20,520) 'change color
      Controls.SetSize(changeColorButton,160,30)
      
      'Color selection boxes
      GraphicsWindow.PenColor = "Black" 'border of all color selector boxes
      GraphicsWindow.BrushColor = "Black" 'Black
      blackBlock = Shapes.AddRectangle(65,30)
      Shapes.Move(blackBlock,20,305)
      GraphicsWindow.BrushColor = "White" 'White
      whiteBlock = Shapes.AddRectangle(65,30)
      Shapes.Move(whiteBlock,110,305)
      GraphicsWindow.BrushColor = "Red" 'Red
      redBlock = Shapes.AddRectangle(65,30)
      Shapes.Move(redBlock,20,340)
      GraphicsWindow.BrushColor = "Blue" 'Blue
      blueBlock = Shapes.AddRectangle(65,30)
      Shapes.Move(blueBlock,110,340)
      GraphicsWindow.BrushColor = "pink" 'Pink
      pinkBlock = Shapes.AddRectangle(65,30)
      Shapes.Move(pinkBlock,20,375)
      GraphicsWindow.BrushColor = "Orange" 'Orange
      orangeBlock = Shapes.AddRectangle(65,30)
      Shapes.Move(orangeBlock,110,375)
      GraphicsWindow.BrushColor = "Yellow" 'Yellow
      yellowBlock = Shapes.AddRectangle(65,30)
      Shapes.Move(yellowBlock,20,410)
      GraphicsWindow.BrushColor = "Purple" 'Purple
      purpleBlock = Shapes.AddRectangle(65,30)
      Shapes.Move(purpleBlock,110,410)
      GraphicsWindow.BrushColor = "Brown" 'Brown
      brownBlock = Shapes.AddRectangle(65,30)
      Shapes.Move(brownBlock,20,445)
      GraphicsWindow.BrushColor = "Gray" 'Gray
      grayBlock = Shapes.AddRectangle(65,30)
      Shapes.Move(grayBlock,110,445)
      GraphicsWindow.BrushColor = "Green" 'Green
      greenBlock = Shapes.AddRectangle(65,30)
      Shapes.Move(greenBlock,20,480)
      
      GraphicsWindow.BrushColor = "Black" 'For color indicator box; leave here
      GraphicsWindow.PenColor = "Black" 'For color indicator box; leave here
    EndSub 
    
    Sub selectTool 
      GraphicsWindow.MouseMove = moveMouse
      GraphicsWindow.MouseDown = pressDown
      
      If (color = 1) Then 
        GraphicsWindow.PenColor = "Black"
        GraphicsWindow.BrushColor = "Black"
      ElseIf (color = 2) Then 
        GraphicsWindow.PenColor = "White"
        GraphicsWindow.BrushColor = "White"
      ElseIf (color = 3) Then 
        GraphicsWindow.PenColor = "Red"
        GraphicsWindow.BrushColor = "Red"
      ElseIf (color = 4) Then 
        GraphicsWindow.PenColor = "Blue"
        GraphicsWindow.BrushColor = "Blue"
      ElseIf (color = 5) Then 
        GraphicsWindow.PenColor = "Pink"
        GraphicsWindow.BrushColor = "Pink"
      ElseIf (color = 6) Then 
        GraphicsWindow.PenColor = "Orange"
        GraphicsWindow.BrushColor = "Orange"
      ElseIf (color = 7) Then 
        GraphicsWindow.PenColor = "Yellow"
        GraphicsWindow.BrushColor = "Yellow" 
      ElseIf (color = 8) Then 
        GraphicsWindow.PenColor = "Purple"
        GraphicsWindow.BrushColor = "Purple" 
      ElseIf (color = 9) Then 
        GraphicsWindow.PenColor = "Brown"
        GraphicsWindow.BrushColor = "Brown" 
      ElseIf (color = 10) Then 
        GraphicsWindow.PenColor = "Gray"
        GraphicsWindow.BrushColor = "Gray" 
      ElseIf (color = 11) Then 
        GraphicsWindow.PenColor = "Green"
        GraphicsWindow.BrushColor = "Green"
      EndIf 
      
      If (Controls.GetButtonCaption(Controls.LastClickedButton) = "Paint" Or Controls.GetButtonCaption(Controls.LastClickedButton) = "Change Color") Then 
        pos = 1
      EndIf 
      
      'This must be placed last; Box that indicates current color 
      colorIndicator = Shapes.AddRectangle(160,40)
      Shapes.Move(colorIndicator,20,555)
    EndSub 
    
    Sub moveMouse
      x = GraphicsWindow.MouseX 
      y = GraphicsWindow.MouseY 
      
      'Allows shapes and pen to follow cursor
      If (Mouse.IsLeftButtonDown) Then 
        If (x > 195) Then 
          If (pos = 1) Then 
            drawLine = Shapes.AddLine(prevX,prevY,x,y) 'drawing line for "paint" button; not line button
          EndIf
        EndIf 
      EndIf 
      
      prevX = x 'This variable must be after all drawLine statements and outside all if else statements 
      prevY = y 'This variable must be after all drawLine statements and outside all if else statements  
    EndSub 
    
    Sub pressDown 
      prevX = GraphicsWindow.MouseX 
      prevY = GraphicsWindow.MouseY  
      
      'Selects color & disables all tools in specified area then corresponds with color statements in sub selectTool
      If (x > 20 And x < 80 And y > 300 And y < 330) Then 
        pos = 0 
        color = 1
      ElseIf (x > 110 And x < 180 And y > 300 And y < 330) Then 
        pos = 0 
        color = 2 
      ElseIf (x > 20 And x < 80 And y > 335 And y < 365) Then 
        pos = 0 
        color = 3 
      ElseIf (x > 110 And x < 180 And y > 335 And y < 365) Then 
        pos = 0 
        color = 4
      ElseIf (x > 20 And x < 80 And y > 370 And y < 400) Then 
        pos = 0
        color = 5 
      ElseIf (x > 110 And x < 180 And y > 370 And y < 400) Then 
        pos = 0
        color = 6 
      ElseIf (x > 20 And x < 80 And y > 405 And y < 435) Then 
        pos = 0
        color = 7
      ElseIf (x > 110 And x < 180 And y > 405 And y < 435) Then 
        pos = 0
        color = 8 
      ElseIf (x > 20 And x < 80 And y > 440 And y < 470) Then 
        pos = 0 
        color = 9
      ElseIf (x > 110 And x < 180 And y > 440 And y < 470) Then 
        pos = 0 
        color = 10 
      ElseIf (x > 20 And x < 80 And y > 475 And y < 505) Then 
        pos = 0 
        color = 11
      EndIf 
    EndSub 

    Also, I believe there was a monthly challenge for this specific program. There may be some more programs and insight there. Here's the link:

    https://social.msdn.microsoft.com/Forums/en-US/64c300a5-334b-450a-bdbd-fb368d8cb2ba/challenge-of-the-month-october-2014?forum=smallbasic



    Tuesday, December 2, 2014 3:08 AM
  • Hi Annie and welcome to the SB forum.

    Here's a quick sample (<80 min) I did for a Paint program:  NNH855-0. Runs locally.

    It might give you some ideas. The sample requires an internet connection to d/load the pallet, otherwise you'll get an error message.

    Happy coding.

    btw:

    Coding does seem hard at first especially if you want to do a lot. Be patient, work through the curriculum and try some stuff. Make some mistakes, try to learn what works and what doesn't.

    Take your time, keep practicing and gradually your skill will grow.


    Tuesday, December 2, 2014 10:30 PM
    Moderator
  • Annie, welcome to Small Basic forum!

    Change

    getcolour = GraphicsWindow.GetPixel(10,10)

    to

    getcolour = GraphicsWindow.GetPixel(GraphicsWindow.MouseX,GraphicsWindow.MouseY)

    in your code.


    Nonki Takahashi

    Wednesday, December 3, 2014 6:52 AM
    Moderator