Built-in programs

paint
draw with the mouse
Type this at the prompt of a computer to copy it onto the medium:
> examples paint > edit paint

The program
-- Paint: hold the mouse button and move over the picture. Right button: eraser.
-- Below: the 16 colours and 4 brush sizes. C clears the page.
local size = gfx.size()
local W = size.w
local H = size.h
local BAR = H - 11 -- the tool bar starts at this row of pixels
local BRUSHES = {1, 3, 5, 9}
local color = 15
local brush = 2
local lastX = 0
local lastY = 0
local function drawBar()
gfx.rect(1, BAR, W, 12, "gray", true)
for c = 0, 15 do
gfx.rect(2 + c * 10, BAR + 2, 9, 8, c, true)
if c == color then
gfx.rect(1 + c * 10, BAR + 1, 11, 10, "white")
end
end
for i = 1, #BRUSHES do
local x = 166 + (i - 1) * 12
gfx.rect(x, BAR + 1, 11, 10, "black", true)
gfx.pixel(x + 5, BAR + 6, "white", math.min(BRUSHES[i], 7))
if i == brush then
gfx.rect(x, BAR + 1, 11, 10, "yellow")
end
end
end
local function page()
gfx.rect(1, 1, W, BAR - 1, "white", true)
end
term.set_bg(term.colors.black)
term.clear()
page()
drawBar()
while true do
local e = os.pull_event()
if (e.name == "click" or e.name == "drag") and e.py >= BAR then
if e.name == "click" and e.px < 162 then
color = math.min(15, (e.px - 2) // 10)
elseif e.name == "click" then
brush = math.max(1, math.min(#BRUSHES, (e.px - 166) // 12 + 1))
end
drawBar()
elseif e.name == "click" or e.name == "drag" then
local ink = color
if e.button == 2 then
ink = "white" -- the eraser paints the page colour
end
if e.name == "drag" then
-- a line from the last point: no gaps when the mouse moves fast
gfx.line(lastX, lastY, e.px, math.min(e.py, BAR - 1), ink, BRUSHES[brush])
else
gfx.pixel(e.px, e.py, ink, BRUSHES[brush])
end
lastX = e.px
lastY = math.min(e.py, BAR - 1)
elseif e.name == "char" and string.lower(e.char) == "c" then
page()
end
end