In this lab you will work with for loops to create
graphical images. Recall that Lab 4 involved adding if statements to a
graphics programs to control the color in a grid element. Those labs
used while loops, but because those loops were simple counting
loops, they would be simpler to write as for loops. In this lab
we will change them to for loops and make other changes.
- Set up a new eclipse project for each lab (detailed instructions)
- Copy the files DrawingPanel.java,
DrawLoop.java and IfGrid.java into your project
folder.
Run the DrawLoop program to make sure it works. Programs
using the Drawing Panel stay running until you close the pop up Drawing
Panel window, so try to remember to close the Drawing Panel each time
before running your program again. It will get confusing if you
leave several versions of your program running.
Look over the DrawLoop program and make sure you understand it. You are going to be making a series of small changes. When you are done it should look like this:
Below are the changes that you need to make. Make each change one at
a time, test it, and make sure you understand it before moving on.
- Replace the while loop with a for loop. Recall
that a for loop gathers together the initialization, continuation
condition, and increment as: for (init; cond; incr)
- Change the for loop to create 10 repetitions of the figure
instead of 8. Run the program. Do you see a difference?
- Change the drawing panel size to accommodate the new grid elements
- Use the g.drawString(string, x, y) to place a number in the oval.
- Remember the trick for converting a integer to a string is to
just + it onto an empty string e.g: "" + i
Copy and paste your program and a screen capture of the drawing panel into your solutions document.
In Windows a screen capture can be done by:
- Selecting the window you want to capture.
- Hit Alt-PrintScreen to capture an image of your window to the clipboard.
- Switch into your editor and paste it with Ctrl-V.
On
the Mac:
- Command-Shift-4
- Click on one corner and drag mouse to diagonal corner
- you will find the screen snapshot on the desktop and you can
insert this into your word file
Modify the program from Exercise 1 to produce the following image (note that the numbers in the ovals have changed) :
Copy and paste your program
and a screen capture of the drawing panel into your solutions
document.
The rest of the exercises will use the program IfGrid.java to
experiment with nested for loops. Run the program to make sure it works.
Make the following changes:
- Replace both while loops with for loops.
- Change the for loop controlling the number of rows to have 12 rows as in the diagram below.
- You will again need to increase the drawing panel size to show your changes.
- As in lab4 change the if statement to have checkerboard coloring.
Copy and paste your program and a screen capture of the drawing panel into your solutions document.
Well, let's make the grid slightly more interesting by producing a
multiplication table. Modify the program from the previous exercise to
generate an image like the following:
Copy and paste your program
and a screen capture of the drawing panel into your solutions
document.
Well, everyone knows zero times a number is zero. Modify the code so
that we have the following multiplication table :
Copy and paste your program
and a screen capture of the drawing panel into your solutions
document.
Save your solutions
document to your USB flash drive and submit it.