The LINE Command for Graphics

by Welopez

"No, Welopez," Pablo shouted at me! "You cannot use the LINE command when printing to the MAINWIN! How many times do I have to tell you that? Can't you ever pay attention?"

"But I've used it before when I was programming in...."

"Stop! We'll have no blasphemy where I can hear it! This is Just Basic, or had you forgotten? When you want to draw a LINE in Just Basic, you must use a window for graphics, or a graphic box! Got that?"

"Uhh, well, yes, Pablo; but that darned Artie Abacii is so fussy! He insists that I enter a whole bunch of numbers to draw a line! Isn't there an easier way to draw a simple line?"

"Of course there is, Dummy!"

Sometimes these Worbles can aggravate the heck out of me. "Okay, Pablo, please explain the simple way to me. I'm all ears!"

"Yeah, you and Dumbo have that in common! Okay, the LINE command has four parameters. You type LINE, and assign the beginning point as x1 y1."

"I read that in the help file... but where is x1 y1? Isn't it easier for me to PLACE the pen, and then use GO to draw a line with turtle graphics?"

"It might be easier for you, Welopez, but turtles can't do math, so you're still going to have to work the problem and tell the turtle where to go. x1 is the coordinate for the right component of your starting point, y1 is the location of the down component of your starting point. All coordinates in Just Basic are given as right-something, down-something, from the margin of your container. If you're drawing a line on a window for graphics, the starting point will be right from the left margin, and down from the top margin. If you're drawing to a graphicbox, the x and y will be right from the left margin of the graphicbox, NOT the window margin, and down from the top, no matter where the graphicbox is placed on the window. Is that clear?"

"Well, now that you explain it that way...."

"x1 and y1 are the starting points of your line. x2 and y2 will be the ending points of your line. If x2 is less than x1, the line extends to the left of x1. If y2 is less than y1, the line extends above y1. What is difficult about that?"

"Okay, Pablo, thanks for explaining that to me... but I'm still not sure where those points will be on my window."

"Try running this little snippet, Welopez. Even your tiny brain should be able to point-and-click. The static text I've included will tell you where you are clicking, the coordinates inside the GRAPHICBOX where the line will begin or end. The text is set to be displayed for only a second and a half, so if you're a slow reader, increase the TIMER value in the GOSUB to provide a longer delay."

      'Demo using the LINE command
      'by Welo, 11/02/06

      msg1$="Click the mouse at the position where you wish "+_
          "to begin drawing the line."

      msg2$="Now, click the mouse at the position where you "+_
          "wish to end the line."

      NOMAINWIN
      WindowWidth=400
      WindowHeight=600
      UpperLeftX=INT((DisplayWidth-WindowWidth)/2)
      UpperLeftY=INT((DisplayHeight-WindowHeight)/2)

      GRAPHICBOX #demo.gb1, 10, 10, 370, 370
      STATICTEXT #demo.st1, msg1$, 10, 410, 370, 50
      BUTTON #demo.btn1, "Draw", [drawLine], LL, 100, 10, 60, 25
      BUTTON #demo.btn2, "Quit", [quit], LL, 250, 10, 60, 25

      OPEN "Draw a Line" FOR WINDOW AS #demo
      PRINT #demo, "trapclose [quit]"
      PRINT #demo, "font arial 14"
      PRINT #demo.gb1, "setfocus"
      PRINT #demo.gb1, "when leftButtonDown [beginPsn]"

      WAIT
      [beginPsn]  'Get the starting position
          x1=MouseX
          y1=MouseY
          PRINT #demo.st1, "The right coordinate to begin the line is "; x1; "."+_
              "  The down component is "; y1; "."
          GOSUB [delay]
          PRINT #demo.st1, msg2$
          PRINT #demo.gb1, "setfocus"
          PRINT #demo.gb1, "when leftButtonDown [finishPsn]"
          WAIT

      [finishPsn]  'Get the ending position
          x2=MouseX
          y2=MouseY
          PRINT #demo.st1, "The right coordinate to end the line is "; x2; "."+_
              "  The down component is "; y2; "."
          GOSUB [delay]
          PRINT #demo.st1, "Click the Draw button to show the line."
      WAIT

      [drawLine]
      PRINT #demo.gb1, "DOWN"
      PRINT #demo.gb1, "COLOR red"
      PRINT #demo.gb1, "SIZE 4"
      PRINT #demo.gb1, "LINE "; x1; " "; y1; " "; x2; " "; y2
      PRINT #demo.gb1, "DISABLE"  'Don't allow any further input!
      PRINT #demo.st1, "Press Quit to end the program.  No fooling!"

      WAIT
      [quit]
          CLOSE #demo
          END

      [delay]
          TIMER 1500, [cont]  'More is longer!
          WAIT
          [cont]
          TIMER 0
      RETURN

"In this routine, we're using MouseX and MouseY to capture the screen coordinates where you have clicked for x1 y1, and again for x2 y2. It's not required that you use x1 and y1 as the variable names, you can write startRight=MouseX and startDown=MouseY, then endRight=MouseX and endDown=MouseY, you simply have to keep in mind what your variable names are when you want to print them, and remember that variables sent to a PRINT statement must be outside the quoted string, and must have the spacing preserved."

"Wow! That's really neat! You mean I don't have to ask Artie Abacii to calculate the coordinates with his binary abacus?"

"Sheesh! I've made this simply point-and-click, Welopez. Why would you want to bother him? Besides, Artie is sort of aloof with dummies, you know? He's not mellowed-out like us artists. If you need to calculate the ending position using trig, or continually update the end position to show movement, then you can ask for Artie's help. Other wise, I'd recommend letting the little snot sleep."

You know, Artie has been sort of snotty with me lately. It's probably a good idea if I let him sleep. It's certainly not a bad idea