android
  #1  
Old 01-13-2010, 02:55 PM
kushndonuts kushndonuts is offline
Junior Member
 
Join Date: Jan 2010
Location: Maryland
Posts: 29
Default Help making a paint app

I am currently trying to make a paint program but I can't seem to figure out how to draw a line by tapping one point then tapping another point and have a line drawn between the two coordinates.

Heres what I have so far
Code:
cblk = color.new(0,0,0);
cwht = color.new(255,255,255);
cred = color.new(255,0,0);

screen.fillrect(0,0,400,240,blk);

function makeButton(x,y,width,height,bordercolor,bgcolor,textsize,textcolor,string)
 screen.fillrect(x,y,width,height,bgcolor);
 screen.drawrect(x,y,x+width,y+height,bordercolor);
 text.color(textcolor);
 text.size(textsize);
 text.draw(x,y,string);
end;

function get_pts()
 while true do 
  if (control.read() == 1) and (control.isTouch() == 1) then
   if (touch.click() == 1) then
    x1,y1 = touch.pos();
    screen.drawpixel(x1,y1,cwht);
    return x1,y1;
   end;
  end;
 end;
  if (control.read() == 1) and (control.isTouch() == 1) then
   if (touch.click() == 1) then
    x2,y2 = touch.pos();
   end;
  end;
end;

function draw_line(x1,y1,x2,y2,linecolor)
 get_pts
 screen.drawline(
 screen.update();
end;

function draw_dot(x,y)
 makeButton(0,0,53,40,cred,blk,20,cred," Exit");
 makeButton(0,41,53,40,cred,cblk,18,cred,"Clear");
 makeButton(0,82,53,40,cred,cblk,20,cred," Line");
 screen.drawpixel(x,y,cwht);
 screen.update();
end;
draw_dot(0,0);

while true do
 if (control.read() == 1) and (control.isTouch() == 1) then
  if (touch.move() == 1) then
   x,y = touch.pos();
   draw_dot(x,y);
  end;
  if (touch.click() == 1) then
   x,y = touch.pos();
   if (y < 40) and (x < 50) then
     break;
   end;
  end;
  if (touch.click() == 1) then
   x,y = touch.pos();
   if (y > 40) and (y < 80) and (x < 50) then
     screen.fillrect(0,0,400,240,cblk);
    screen.update();
    do_out(0,0);
   end;
  end;
  if (touch.click() == 1) then
   x,y = touch.pos();
    if (y > 81) and (y < 122) and (x < 50) then
      draw_line();
   end;
  end;  
 end;
 os.sleep(1);
end;
I know there are some incomplete parts that are probably entirely wrong.
If there is a way to interrupt a while loop I could make it all work eventually but I can't seem to find that info. If somebody can point me in the right direction or give me any help it would be great.
Reply With Quote

Advertisement [Remove Advertisement]

  #2  
Old 01-13-2010, 03:03 PM
bzdbbb's Avatar
bzdbbb bzdbbb is offline
X-Fi2 Development Moderator
 
Join Date: Dec 2009
Location: Gloucestershire, UK
Posts: 94
Default

instead of putting

Code:
 while true do
try putting an expression in the while statement. The while loop will keep executing while the expression is true. It is possible to use "break" commands to stop a while loop but this can cause errors, using variable expressions is much more stable.

e.g.
Code:
looping = 1;
while looping == 1 do
	if control.read() == 1 then
		if (control.isButton() == 1) then
			if (button.home() == 1) and (button.click() == 1) then
				looping = 0;
			end;
		end;
	end;
end;
Make sure you put "==" rather than "=" - I always make that mistake :P

I havn't had a look through the rest of your code, but it doesn't look too bad on first glance.
Reply With Quote

  #3  
Old 01-13-2010, 03:09 PM
kushndonuts kushndonuts is offline
Junior Member
 
Join Date: Jan 2010
Location: Maryland
Posts: 29
Default

Thanks a lot I was thinking of doing that but had no idea how.
I have one question about the code you gave me with the "endgame = 0;" line. Shouldn't that be "looping = 0;" instead.
Reply With Quote

  #4  
Old 01-13-2010, 03:14 PM
Jan_DK's Avatar
Jan_DK Jan_DK is offline
Junior Member
 
Join Date: Mar 2009
Location: Denmark
Posts: 88
Default

Hi.

It's hard for me to jump into a unfinished code, and to see what's missing
vs. what's wrong. but any way :-)

You call this function from draw_line but you don't use the returned x1,y1
and the function never gets to the bottom part?

Code:
function get_pts()

 while true do 
  if (control.read() == 1) and (control.isTouch() == 1) then
       if (touch.click() == 1) then
        x1,y1 = touch.pos();
        screen.drawpixel(x1,y1,cwht);
        return x1,y1;
       end;
  end;

  end; --loop end ?

  if (control.read() == 1) and (control.isTouch() == 1) then
       if (touch.click() == 1) then
        x2,y2 = touch.pos();
       end;
  end;

end;
Jan_DK
Reply With Quote

  #5  
Old 01-13-2010, 03:19 PM
bzdbbb's Avatar
bzdbbb bzdbbb is offline
X-Fi2 Development Moderator
 
Join Date: Dec 2009
Location: Gloucestershire, UK
Posts: 94
Default

Quote:
I have one question about the code you gave me with the "endgame = 0;" line. Shouldn't that be "looping = 0;" instead.
Lol, yeah. I've corrected it now. I copied and pasted that from the new build of pong, deleted and changed all the specific parts but didn't notice that :P
Reply With Quote

  #6  
Old 01-13-2010, 03:23 PM
kushndonuts kushndonuts is offline
Junior Member
 
Join Date: Jan 2010
Location: Maryland
Posts: 29
Default

Yeah I just fixed that with what bzdbbb gave me and that should fix my main problem

Code:
cblk = color.new(0,0,0);
cwht = color.new(255,255,255);
cred = color.new(255,0,0);

screen.fillrect(0,0,400,240,blk);

function makeButton(x,y,width,height,bordercolor,bgcolor,textsize,textcolor,string)
 screen.fillrect(x,y,width,height,bgcolor);
 screen.drawrect(x,y,x+width,y+height,bordercolor);
 text.color(textcolor);
 text.size(textsize);
 text.draw(x,y,string);
end;

function draw_line()

getpta = 1
 while getpta == 1 do 
  if (control.read() == 1) and (control.isTouch() == 1) then
   if (touch.click() == 1) then
    x1,y1 = touch.pos();
    screen.drawpixel(x1,y1,cwht);
    screen.update();
    getpta = 0;
   end;
  end;
 end;

getptb = 1
 while getptb == 1 do
  if (control.read() == 1) and (control.isTouch() == 1) then
   if (touch.click() == 1) then
    x2,y2 = touch.pos();
    screen.drawline(x1,y1,x2,y2,cwht);
    screen.update();
    getptb = 0;
   end;
  end;
 end;

end;

function draw_dot(x,y)
 makeButton(0,0,53,40,cred,blk,20,cred," Exit");
 makeButton(0,41,53,40,cred,cblk,18,cred,"Clear");
 makeButton(0,82,53,40,cred,cblk,20,cred," Line");
 screen.drawpixel(x,y,cwht);
 screen.update();
end;
draw_dot(0,0);

while true do
 if (control.read() == 1) and (control.isTouch() == 1) then
  if (touch.move() == 1) then
   x,y = touch.pos();
   draw_dot(x,y);
  end;
  if (touch.click() == 1) then
   x,y = touch.pos();
   if (y < 40) and (x < 50) then
     break;
   end;
  end;
  if (touch.click() == 1) then
   x,y = touch.pos();
   if (y > 40) and (y < 80) and (x < 50) then
     screen.fillrect(0,0,400,240,cblk);
    screen.update();
    draw_dot(0,0);
   end;
  end;
  if (touch.click() == 1) then
   x,y = touch.pos();
    if (y > 81) and (y < 122) and (x < 50) then
      draw_line();
   end;
  end;  
 end;
 os.sleep(1);
end;
Reply With Quote

  #7  
Old 01-13-2010, 03:27 PM
kushndonuts kushndonuts is offline
Junior Member
 
Join Date: Jan 2010
Location: Maryland
Posts: 29
Default

Quote:
Lol, yeah. I've corrected it now. I copied and pasted that from the new build of pong, deleted and changed all the specific parts but didn't notice that :P
I figured that it was a mistake and fixed it myself.
Reply With Quote

  #8  
Old 01-13-2010, 03:35 PM
kushndonuts kushndonuts is offline
Junior Member
 
Join Date: Jan 2010
Location: Maryland
Posts: 29
Default

Hmmm.... I tried running it and it said "Error Occured!"
So... um what did I do wrong or what is it that I need to add.
Reply With Quote

  #9  
Old 01-13-2010, 03:42 PM
Jan_DK's Avatar
Jan_DK Jan_DK is offline
Junior Member
 
Join Date: Mar 2009
Location: Denmark
Posts: 88
Default

Quote:
Originally Posted by kushndonuts View Post
Hmmm.... I tried running it and it said "Error Occured!"
So... um what did I do wrong or what is it that I need to add.
Same here,,,

Color blk ?

function draw_dot(x,y)
makeButton(0,0,53,40,cred,blk,20,cred," Exit");
Reply With Quote

  #10  
Old 01-13-2010, 03:52 PM
Jan_DK's Avatar
Jan_DK Jan_DK is offline
Junior Member
 
Join Date: Mar 2009
Location: Denmark
Posts: 88
Default

function makeButton(x,y,width,height,bordercolor,bgcolor,te xtsize,textcolor,string)

string is a reserved keyword use strings eg.

Recommend using the SCiTE editor that comes with Lua 5.1 for windows
or any other Lua highlight enable editor.

Jan_DK

Last edited by Jan_DK; 01-13-2010 at 04:13 PM.
Reply With Quote

  #11  
Old 01-13-2010, 03:54 PM
kushndonuts kushndonuts is offline
Junior Member
 
Join Date: Jan 2010
Location: Maryland
Posts: 29
Default

Thanks.
I think i fixed all the spelling mistakes now.
Here's the updated code

Code:
cblk = color.new(0,0,0);
cwht = color.new(255,255,255);
cred = color.new(255,0,0);

screen.fillrect(0,0,400,240,cblk);

function makeButton(x,y,width,height,bordercolor,bgcolor,textsize,textcolor,string)
 screen.fillrect(x,y,width,height,bgcolor);
 screen.drawrect(x,y,x+width,y+height,bordercolor);
 text.color(textcolor);
 text.size(textsize);
 text.draw(x,y,string);
end;

function draw_line()
getpta = 1;
 while getpta == 1 do
  if (control.read() == 1) and (control.isTouch() == 1) then
   if (touch.click() == 1) then
    x1,y1 = touch.pos();
    screen.drawpixel(x1,y1,cwht);
    screen.update();
    getpta = 0;
   end;
  end;
 end;
getptb = 1;
 while getptb == 1 do
  if (control.read() == 1) and (control.isTouch() == 1) then
   if (touch.click() == 1) then
    x2,y2 = touch.pos();
    screen.drawline(x1,y1,x2,y2,cwht);
    screen.update();
    getptb = 0;
   end;
  end;
 end;
end;

function draw_dot(x,y)
 makeButton(0,0,53,40,cred,cblk,20,cred," Exit");
 makeButton(0,41,53,40,cred,cblk,18,cred,"Clear");
 makeButton(0,82,53,40,cred,cblk,20,cred," Line");
 screen.drawpixel(x,y,cwht);
 screen.update();
end;
draw_dot(0,0);

while true do
 if (control.read() == 1) and (control.isTouch() == 1) then
  if (touch.move() == 1) then
   x,y = touch.pos();
   draw_dot(x,y);
  end;
  if (touch.click() == 1) then
   x,y = touch.pos();
   if (y < 40) and (x < 50) then
     break;
   end;
  end;
  if (touch.click() == 1) then
   x,y = touch.pos();
   if (y > 40) and (y < 80) and (x < 50) then
     screen.fillrect(0,0,400,240,cblk);
    screen.update();
    draw_dot(0,0);
   end;
  end;
  if (touch.click() == 1) then
   x,y = touch.pos();
    if (y > 81) and (y < 122) and (x < 50) then
      draw_line();
   end;
  end;
 end;
 os.sleep(1);
end;
Reply With Quote

  #12  
Old 01-13-2010, 03:57 PM
kushndonuts kushndonuts is offline
Junior Member
 
Join Date: Jan 2010
Location: Maryland
Posts: 29
Default

Quote:
string is a reserved keyword user strings eg.
K then I fixed that. btw I am using SciTE

Code:
cblk = color.new(0,0,0);
cwht = color.new(255,255,255);
cred = color.new(255,0,0);

screen.fillrect(0,0,400,240,cblk);

function makeButton(x,y,width,height,bordercolor,bgcolor,textsize,textcolor, strings)
 screen.fillrect(x,y,width,height,bgcolor);
 screen.drawrect(x,y,x+width,y+height,bordercolor);
 text.color(textcolor);
 text.size(textsize);
 text.draw(x,y,strings);
end;

function draw_line()
getpta = 1;
 while getpta == 1 do
  if (control.read() == 1) and (control.isTouch() == 1) then
   if (touch.click() == 1) then
    x1,y1 = touch.pos();
    screen.drawpixel(x1,y1,cwht);
    screen.update();
    getpta = 0;
   end;
  end;
 end;
getptb = 1;
 while getptb == 1 do
  if (control.read() == 1) and (control.isTouch() == 1) then
   if (touch.click() == 1) then
    x2,y2 = touch.pos();
    screen.drawline(x1,y1,x2,y2,cwht);
    screen.update();
    getptb = 0;
   end;
  end;
 end;
end;

function draw_dot(x,y)
 makeButton(0,0,53,40,cred,cblk,20,cred," Exit");
 makeButton(0,41,53,40,cred,cblk,18,cred,"Clear");
 makeButton(0,82,53,40,cred,cblk,20,cred," Line");
 screen.drawpixel(x,y,cwht);
 screen.update();
end;
draw_dot(0,0);

while true do
 if (control.read() == 1) and (control.isTouch() == 1) then
  if (touch.move() == 1) then
   x,y = touch.pos();
   draw_dot(x,y);
  end;
  if (touch.click() == 1) then
   x,y = touch.pos();
   if (y < 40) and (x < 50) then
     break;
   end;
  end;
  if (touch.click() == 1) then
   x,y = touch.pos();
   if (y > 40) and (y < 80) and (x < 50) then
     screen.fillrect(0,0,400,240,cblk);
    screen.update();
    draw_dot(0,0);
   end;
  end;
  if (touch.click() == 1) then
   x,y = touch.pos();
    if (y > 81) and (y < 122) and (x < 50) then
      draw_line();
   end;
  end;
 end;
 os.sleep(1);
end;
Reply With Quote

  #13  
Old 01-13-2010, 04:01 PM
kushndonuts kushndonuts is offline
Junior Member
 
Join Date: Jan 2010
Location: Maryland
Posts: 29
Default

GREAT it runs now but it crashed when I touched the line button
Reply With Quote

  #14  
Old 01-13-2010, 04:15 PM
bzdbbb's Avatar
bzdbbb bzdbbb is offline
X-Fi2 Development Moderator
 
Join Date: Dec 2009
Location: Gloucestershire, UK
Posts: 94
Default

Try commenting out each function and seeing if it runs without that function. Locate the exact problem and then working out what's wrong with it. If you can't see what wrong after that then it must be a logical error rather than a syntax error. Add a "text.draw" line and get the zen to draw ALL the variables on the screen and check they do what you want them too. Make sure to put in "tostring()" with numerical values.

And comment your code so when it's easy to see what does what when you look over it.

Try all that and you should find the problem..
Reply With Quote

  #15  
Old 01-13-2010, 04:28 PM
kushndonuts kushndonuts is offline
Junior Member
 
Join Date: Jan 2010
Location: Maryland
Posts: 29
Default

Thanks for the help I'll try everything you mentioned. Although I don't exactly know how to add a "text.draw"
Reply With Quote

  #16  
Old 01-13-2010, 04:29 PM
Jan_DK's Avatar
Jan_DK Jan_DK is offline
Junior Member
 
Join Date: Mar 2009
Location: Denmark
Posts: 88
Default

I also recommend using audio.beep's to debug like this:

function draw_line()
audio.beep(700,400);
--code
audio.beep(1700,400);
end;

I only hear the first beep then it crash.

Jan_DK
Reply With Quote

  #17  
Old 01-13-2010, 05:22 PM
Jan_DK's Avatar
Jan_DK Jan_DK is offline
Junior Member
 
Join Date: Mar 2009
Location: Denmark
Posts: 88
Default

Hehe after unplugging 30 times and trying, your code works !
it dos not crash, it just go into a loop where it apparently don't read control input without os.sleep(10) function.

Add those os.sleep(10) and it works, one click on the line button,
2nd. on screen to set first point 3rd. on screen to draw the line.

Code:
function draw_line()
    getpta = 1;
     while getpta == 1 do
     os.sleep(10);
          if (control.read() == 1) and (control.isTouch() == 1) then
               if (touch.click() == 1) then
                    x1,y1 = touch.pos();
                    screen.drawpixel(x1,y1,cwht);
                    screen.update();
                    getpta = 0;
               end;
          end;
     end;
    getptb = 1;
     while getptb == 1 do
     os.sleep(10);
          if (control.read() == 1) and (control.isTouch() == 1) then
               if (touch.click() == 1) then
                    x2,y2 = touch.pos();
                    screen.drawline(x1,y1,x2,y2,cwht);
                    screen.update();
                    getptb = 0;
               end;
          end;
     end;
end;
Jan_DK
Reply With Quote

  #18  
Old 01-13-2010, 05:29 PM
Jan_DK's Avatar
Jan_DK Jan_DK is offline
Junior Member
 
Join Date: Mar 2009
Location: Denmark
Posts: 88
Default

After discovering that os.sleep() don't take milliseconds but units of 10ms
i wanted to change the os.sleep(1) = wait 10ms to os.wait(1) = wait 1 ms
in the Starfield screensaver to speed it up, i got the speed-up, but
after changing that, I could not exit the apps. only reset works, so
I guess that the control event thingy in the X-Fi2 needs at least 10ms
to work, even the power off button.


Jan_DK
Reply With Quote

  #19  
Old 01-13-2010, 06:09 PM
ThievingSix ThievingSix is offline
Member
 
Join Date: Aug 2008
Posts: 218
Default

Example use "If then else", your control loop used the same calls a bit too much:
Code:
clBlack = color.new(0,0,0);
clWhite = color.new(255,255,255);
clRed = color.new(255,0,0);

screen.fillrect(0,0,400,240,clBlack)

function makeButton(x,y,width,height,bordercolor,bgcolor,textsize,textcolor,strings)
  screen.fillrect(x,y,width,height,bgcolor);
  screen.drawrect(x,y,x + width,y + height,bordercolor);
  text.color(textcolor);
  text.size(textsize);
  text.draw(x,y,strings);
end;

function draw_line()
  local getpta = 1;
  while getpta == 1 do
    if (control.read() == 1) and (control.isTouch() == 1) then
      if (touch.click() == 1) then
        x1, y1 = touch.pos();
        screen.drawpixel(x1,y1,clWhite);
        screen.update();
        getpta = 0;
      end;
    end;
    os.sleep(4);
  end;
  local getptb = 1;
  while getptb == 1 do
    if (control.read() == 1) and (control.isTouch() == 1) then
      if (touch.click() == 1) then
        x2, y2 = touch.pos();
        screen.drawline(x1,y1,x2,y2,clWhite);
        screen.update();
        getptb = 0;
      end;
    end;
    os.sleep(4);
  end;
end;

function draw_dot(x,y)
  makeButton(0,0,53,40,clRed,clBlack,20,clRed," Exit");
  makeButton(0,41,53,40,clRed,clBlack,18,clRed,"Clear");
  makeButton(0,82,53,40,clRed,clBlack,20,clRed," Line");
  screen.drawpixel(x,y,clWhite);
  screen.update();
end;
draw_dot(0,0);

while true do
  if (control.read() == 1) and (control.isTouch() == 1) then
    if (touch.move() == 1) then
      x,y = touch.pos();
      draw_dot(x,y);
    elseif (touch.click() == 1) then
      x,y = touch.pos();
      if (y < 40) and (x < 50) then
        break;
      elseif (y > 40) and (y < 80) and (x < 50) then
        screen.fillrect(0,0,400,240,clBlack);
        screen.update();
        draw_dot(x,y);
      elseif (y > 81) and (y < 122) and (x < 50) then
        draw_line();
      end;
    end;
  end;
  os.sleep(4);
end;
Reply With Quote

  #20  
Old 01-14-2010, 07:46 AM
kushndonuts kushndonuts is offline
Junior Member
 
Join Date: Jan 2010
Location: Maryland
Posts: 29
Default

Thank you so much! This just made my day
I eventually will add a more colors and if somebody figures out how to save a screenshot I will be able to add a save function
Reply With Quote

Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



All times are GMT -5. The time now is 07:27 PM.