Welcome to lab 3!
If you have checkpoints from the last lab to show your instructor or TA, do so immediately. No credit will be given if you have not already completed the work, nor will credit be given after the first 10 minutes of this lab.
Work with a partner that you have not worked with before.
Our goal today is to learn more about methods, which let us extract reusable sequences of code into a separate block of code. Methods have some very nice properties:
abstractaway low-level details and enable to us write programs at a much higher level of thinking.
Person A types.
Play Lightbot. Complete the first two worlds: Basics and Procedures.
Person B types. Open your Eclipse workspace and create a package named
The Java library includes a pretty fun class named
Robot, you can hijack the mouse pointer, grab screenshots, and programmatically issue keypresses. Essentially you can make your program act as a fake user, generating user input—even in other programs.
In this checkpoint, you’ll use
Robot to control a drawing program. Follow these steps to get it up and running.
mainmethod. Do not name your class
Robot, otherwise it will hide the builtin
mainmethod of your
Squareclass, construct an instance of the
Robotclass. You’ll see some red in Eclipse when you try to do so. Hover over the red and you’ll learn that the code can throw something called an exception. Click the Add throws declaration quick fix.
drawDot. Accept as parameters a
ints, one for an
xpixel coordinate and one for
y. Use the methods of
Robotto simulate a mouse click (which is a press followed by a release) at the given location. Check out the documentation to see what methods are available. Include a slight delay after pressing and releasing so that slow humans like us can see what the
Robotis doing as your program executes.
drawDotmethod by calling it from
main. Pass the
Robotyou made in step 4 and the location of a pixel somewhere in the drawing canvas of your paint program. You should see the cursor move and a dot appear on the canvas. Do not continue until you see this single dot in your paint program.
drawLineWithFiveDots. Accept five parameters: a
deltaX, and an
deltaY. It draws a first dot at (
y), a second dot at (
x + deltaX,
y + deltaY), a third dot at (
x + 2 * deltaX,
y + 2 * deltaY), and so on. Use only five statements to accomplish this—five calls to
drawLineWithFiveDotsmethod by calling it from your
mainmethod. Have it draw a horizontal line. (What is
deltaYin a horizontal line?) You should see five splotches of color in the paint program.
drawSquare. Accept a
Robotas the sole parameter. Have it draw a square using just four calls to
drawLineWithFiveDots, and nothing else. Call it from
main. The end result should look something like this, though your brush style and color may differ: