World Image Recursion Exercise

Make this picture using recursion, building one block at a time, for as many blocks as requested. Use recursion.

Start with this program, and fill in the recursive method:

import javalib.worldimages.*;

import java.awt.Color;

import java.util.Scanner;

import javalib.worldcanvas.*;

public class Checkerboard

{

    public static void main(){

        Scanner screen = new Scanner(System.in);

        WorldCanvas canvas = new WorldCanvas(1000,1000);

        canvas.show();

        int numberSquares;

        do{

            System.out.println("Enter the number of squares");

            numberSquares = screen.nextInt();

            WorldImage cBoard= makeCheckerBoardRow(numberSquares) ;

            canvas.clear();

            canvas.drawImage(cBoard);

        }while(numberSquares > 0);

        canvas.close();

    }

    /**

     * Make a checkboard Row image of the size given 

     * questions to ask yourself:

     * When will I want to return nothing or one square?

     * When I am asked for 3 squares what will I have available to me?

     *     Remember you have the parameter variables

     *        and the result of any other possible call to makeCheckerBoardRow

     */

    public static WorldImage makeCheckerBoardRow(int size){

        if (size <= 1){

            return AImage.makeRectangle(50,50,Color.RED,Mode.FILLED);

        }

        else if (size % 2 == 0){

            return  makeCheckerBoardRow(size - 1).beside(AImage.makeRectangle(50,50,Color.BLACK,Mode.FILLED));

        }

        else

        {

            return makeCheckerBoardRow(size - 1).beside(AImage.makeRectangle(50,50,Color.RED,Mode.FILLED));

        }

    }

}

 

Notes on PictureIT:

 

Import the following libraries:

·         import javalib.worldimages.*;

·         import javalib.worldcanvas.*;

·         import java.awt.Color;

Methods you will need:

makeRectangle Static method of AImage:

·         makeRectangle(int width, int height, Color color, Mode mode) will return a rectangle. The available Modes are FILLED (aka SOLID) and OUTLINED. You can use colors: BLACK, RED, BLUE and others.

·         Example: WorldImage mypic = AImage.makeRectangle(50,50,Color.RED,Mode.FILLED);

beside Instance method of WorldImage:

·         beside(WorldImage

... others) Returns a WorldImage that  Concatenates two or more images horizontally.

·         Example: WorldImage twoSquares = AImage.makeRectangle(50,50,Color.RED,Mode.FILLED).beside(AImage.makeRectangle(50,50,Color.BLACK,Mode.FILLED));

·         Example2: WorldImage pic = pic1.beside(pic);

show Instance method of WorldCanvas:

·         show() : Show an empty window. 

·         Example: mycanvas.show();

drawImage Instance method of WorldCanvas:

·         drawImage(WorldImage) places the image on the canvas starting at 0,0. 

·         Example: mycanvas.drawImage(mypic);

clear Instance method of WorldCanvas:

·         clear() removes the picture currently on the canvas.

·         Example: mycanvas.clear();

close Instance method of WorldCanvas:

·         close() closes the canvas window

·         Example: mycanvas.close();

Find all picture it library javadoc at:

·         http://home.adelphi.edu/sbloch/javalib_doc/

·         Click on AImage for all methods to create shapes.

·         Click on WorldImage for all methods to place shapes or adjust them.

·         Click on WorldCanvas for the methods to control the window