Here is the expected output of my code:
And here is my code:
import java.awt.*;
public class CafeWall
{
final static int MORTAR = 2;
final static int WIDTH = 650;
final static int HEIGHT = 400;
public static void main( String[] args )
{
StdDraw.setCanvasSize(WIDTH, HEIGHT);
StdDraw.setXscale(0, WIDTH);
StdDraw.setYscale(0, HEIGHT);
// Change from Color.GRAY to change background color.
StdDraw.setPenColor( Color.GRAY );
StdDraw.filledRectangle(WIDTH/2, HEIGHT/2, WIDTH/2, HEIGHT/2);
drawRow( 0, 378, 4, 20 );
drawRow( 50, 300, 5, 30 );
drawGrid( 10, 36, 4, 25, 0 );
drawGrid( 250, 40, 3, 25, 10 );
drawGrid( 425, 2, 5, 20, 10 );
drawGrid( 400, 234, 2, 35, 35 );
}
// Draw a row of squares, the total number of squares is pairs * 2
// (x, y) is the lower left corner of the first box
public static void drawRow( int x, int y, int pairs, int size )
{
StdDraw.setPenColor(Color.BLACK);
StdDraw.filledRectangle(x, y, size, size);
StdDraw.setPenColor(Color.BLUE);
StdDraw.line(x, y, x+size, y+size);
StdDraw.line(x, y+size, x+size, y);
StdDraw.setPenColor(Color.WHITE);
StdDraw.filledRectangle(x+size, y, size, size);
}
// Draw a grid of 2 * pairs rows
public static void drawGrid( int x, int y, int pairs, int size,
int offset )
{
int startingX = x;
for(int line = 1; line <= pairs; line++) {
if(line % 2 ==1) { //if line is odd
x = startingX;
} else { //else line is even and must be shifted by offset specified
x = startingX + offset;
}
for(int i = 1; i <= pairs; i++) {
drawRow(x, y, pairs, size);
x = x + 2*size;
}
y = y + size + MORTAR;
}
}
}
Problems:
I have been staring at this for hours. I cannot seem to figure out where I have gone wrong. Can anyone offer some guidance?
(Additional note) This is the actual output of this code for those who are wondering:
There are two significant issues you have with your drawRow()
:
This is what that method should look like:
public static void drawRow( int x, int y, int pairs, int size ) {
// loop for each pair.
for (int i = 0; i < pairs; i++) {
StdDraw.setPenColor(Color.BLACK);
// Note the correct centering of the rectangles
StdDraw.filledRectangle(x + size/2, y + size/2, size / 2, size/2);
StdDraw.setPenColor(Color.BLUE);
StdDraw.line(x, y, x+size, y+size);
StdDraw.line(x, y+size, x+size, y);
StdDraw.setPenColor(Color.WHITE);
// Note the correct centering of the rectangles
StdDraw.filledRectangle(x+size+size/2, y + size/2, size / 2, size/2);
// advance to the next pair.
x += size * 2;
}
}
in your drawGrid()
method you should have an additional rows
parameter which indicates how many rows to draw. I have modified that method to look like:
public static void drawGrid( int x, int y, int rows, int pairs, int size,
int offset )
{
int startingX = x;
for(int line = 0; line < rows; line++) {
x = startingX + (line % 2) * offset;
drawRow(x, y, pairs, size);
y = y + size + MORTAR;
}
}
Note the 'clever' use of the (line % 2) * offset
. Figure that one out.
Putting it together, and changing the size of some of the rows, I have the main method calls:
drawRow( 0, 378, 4, 20 );
drawRow( 50, 300, 5, 30 );
// Bottom left
drawGrid( 10, 36, 4, 4, 20, 0 );
// bottom mid
drawGrid( 250, 40, 6, 3, 25, 10 );
// bottom right
drawGrid( 425, 2, 10, 5, 16, 8 );
// top right
drawGrid( 400, 234, 4, 2, 35, 35 );
and my result looks like:
These are not huge changes from what you had... just a little bit of logic here and there.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With