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 a sequence of code into a self-contained and reusable “recipe.” Methods have some very nice properties:
Person A types.
Play Lightbot. Complete the first two worlds: Basics and Procedures.
Show your completed world 2 screen to your instructor or TA. If you are completing this outside of lab, take a screenshot.
Person B types. Open your IntelliJ project and create a package named labs.lab03
.
Java includes a pretty fun class named Robot
that lets you programmatically hijack the mouse, grab screenshots, and issue keypresses. You can use Robot
to interact with the computer as a human would, generating user input that affects any program.
In this checkpoint, you’ll use this class to control a drawing program. Follow these steps to get it up and running.
Square
with a main
method.main
method of your Square
class, construct an instance of the Robot
class. We have been constructing instances for a while now with code like this: Scanner in = new Scanner(System.in);
Random generator = new Random();
Robot
in a similar way. Be sure to pick a meaningful name.throws
clause to your main
method.drawDot
to draw a single dot by clicking the mouse at a location on the screen. Accept three parameters: a Robot
and two int
s, one for an x
pixel coordinate and one for y
. Use the methods of Robot
to 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. Use the left mouse button, which is button 1. Note, however, that you cannot pass a 1 as a parameter. Read the documentation to see what parameters are legal.Robot
is doing as your program executes. Robot
has a method for this.drawDot
method by calling it from main
. Pass as actual parameters three values: the Robot
you 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 Robot
, an x
location, a y
location, an int
named deltaX
, and an int
named deltaY
. It draws a first dot at (x
, 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 drawDot
.drawLineWithFiveDots
method by calling it from your main
method. Have it draw a horizontal line. (What is deltaY
in a horizontal line?) You should see five splotches of color in the paint program.drawSquare
. Accept a Robot
as 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: Be sure to commit and push your work to GitLab after every coding session.
Comments