This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Gumball's profile picture

ApplePAINT Help.

Started by Gumball, 21 October 2014 - 04:26 AM
Gumball #1
Posted 21 October 2014 - 06:26 AM
Hi, im developing a paint program for my OS, AppleOS, but iv'e ran into a snag, I almost got a save function/ something that memorizes where you click and what color, but its not letting my. Heres the code I used. First, i'll explain the 3 variables. Color is a variable in my paint program that whenever you click a color button, it changes the color to something like colors.red, or colors.green . X and Y is just where you clicked.


if(X >=51 and X <=52 and Y==1 and button ==1) then
  color = colors.red -- color changing code
  else
  colorIndexNumber = colorIndexNumber + 1
  clickIndexTable[colorIndex] = X,Y,color -- sets the table index
--[[ color index to where you clicked and the color, so if you clicked at 1,1 and the color is colors.green it sets click index table to 1,1,color, so at the end when it redraws what you made, it does
for a=1,#clickIndexTable do
  paintutils.drawPixel(1,1,colors.red)
end
so, it should have done that, but it said expected X,Y and color.
--]]
Bomb Bloke #2
Posted 21 October 2014 - 12:52 PM
You're missing a lot of your code out here - not to mention the exact wording of the error - but I can at least tell you that this line won't do what you're thinking:

clickIndexTable[colorIndex] = X,Y,color -- sets the table index

When it runs, clickIndexTable[colorIndex] will be set to the value of X. The values of Y and color won't be assigned to anything. See here as to why.

It appears what you're wanting to do is create a sub-table in clickIndexTable:

clickIndexTable[colorIndex] = {X,Y,color} -- sets the table index

… and unpack its contents when calling paintutils.drawPixel() within your loop:

paintutils.drawPixel(unpack(clickIndexTable[a]))

Personally, I'd use X and Y to determine the indexes within which to store the colour data:

clickIndexTable[Y][X] = color -- sets the table index

.
.
.

for b = 1, table.maxn(clickIndexTable) do
  for a = 1, table.maxn(clickIndexTable[b]) do
    if clickIndexTable[b][a] then paintutils.drawPixel(a,b,clickIndexTable[b][a]) end
  end
end

This way you automatically overwrite old data about pixels which the user drew over more than once. It does limit potential for an undo function, however.
Gumball #3
Posted 22 October 2014 - 06:37 AM
Thanks! :D/>