CS 145 Lab 7 – Loops
Welcome to lab 7!
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.
You must work with a partner that you have not worked with before.
In this lab, we’ll explore loops, which let us execute a task many times, but only code it once.
Checkpoint 1
Person A types. Heading to Coding Bat and solve the following problems:
Checkpoint 2
Person B types.
Write a class Splotches
with a main
method that makes an instance of a BufferedImage
with an RGB pixel format. See the BufferedImage
documentation for details.
Add a method plotRectangle
that accepts the following parameters:
- a
BufferedImage
to draw on - a
Color
to plot - a rectangle’s lower-left x-coordinate, of type
int
- a rectangle’s lower-left y-coordinate, of type
int
- a rectangle’s upper-right x-coordinate, of type
int
- a rectangle’s upper-right y-coordinate, of type
int
Inside the method, fill all the pixels inside the rectangle’s bounds with the random color. Use the setRGB
method of the BufferedImage
class. Plot exactly one rectangle.
Back in main
, call this method many times using a loop. Randomly place and color these rectangles, and plot so many that you can no longer see the background color. Write the file to disk using ImageIO.write
. Pass "png"
as the image format and a file on your Desktop
as the destination file. You can select such a file like so:
File dst = new File(new File(System.getProperty("user.home")), "Desktop/splotches.png");
Tweak your random settings until you generate something you’d hang on your fridge.