Jump to content

Checkerboard program with StdDraw class

- - - - -

  • Please log in to reply
5 replies to this topic

#1
Hiche

Hiche

    Newbie

  • Members
  • Pip
  • 9 posts
public class CheckerBoard

{

	public static void main(String[] args)

    {

		StdDraw.setCanvasSize(600, 600);

		

        for (int i = 0; i < 3; i++)

        {

        	int input = Integer.parseInt(args[i]);

            printCheckerboard(input);

        }

       

    }

       

    public static void printCheckerboard(int N)

    {

    	StdDraw.setXscale(0, N);

        StdDraw.setYscale(0, N);

                      

        for (int rows = 0; rows < N; rows++)

        {

        	for (int cols = 0; cols < N; cols++)

            {

        		if((rows + cols) % 2 == 0)

                {

        			StdDraw.setPenColor(StdDraw.RED);

                }

                else

                {

                	StdDraw.setPenColor(StdDraw.BLACK);

                }

        		StdDraw.filledSquare(cols + .5 , rows + .5, .5);

            }

        }

        StdDraw.show();

    }

       

}

This works fine, but each checkerboard -- different squares/dimensions -- overwrites the other. I want it so that the checkerboards are aligned next to each other in a 1000x1000 canvas size (or something roughly equal). How exactly can should I edit my program?

#2
fread

fread

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 787 posts
You should have probably said where to find StdDraw.

---------- Post added at 05:45 PM ---------- Previous post was at 04:52 PM ----------

Maybe someone else here knows a better way but, the only way i see to draw to separate regions of the canvas is to modify (or add) methods in the StdDraw class.
Perfection of means and confusion of ends seem to characterize our age. Albert Einstein :confused:

#3
Eieio

Eieio

    Learning Programmer

  • Members
  • PipPipPip
  • 47 posts
Any reason why you need to use StdDraw??? I would suggest against even going though the agony of using that unless you have to.

Also are you trying to have more than one board at a time? or do you mean the individual squares themselves?

Is this a desktop application?

#4
fread

fread

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 787 posts
Can you modify StdDraw?
Perfection of means and confusion of ends seem to characterize our age. Albert Einstein :confused:

#5
Hiche

Hiche

    Newbie

  • Members
  • Pip
  • 9 posts
No, we cannot modify the file. Basically, what I want to do is print the checkerboards next to each other in the SAME canvas of a large size (not necessarily 1000 but one that can hold three same-sized checkerboards of NxN squares (N varies)). In my case, they are being overwritten on each other. Unfortunately, we cannot use another class; we are limited to this, albeit frustrating to use, class.

---------- Post added at 09:18 PM ---------- Previous post was at 09:10 PM ----------

Like so:

Posted Image

See how they have the same size but different squares. I need this very soon, if anyone is willing to help, thank you.

#6
fread

fread

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 787 posts
I managed to draw the three boards without overlap but they are not three same size squares like the image you provided and some of the inner sqaures on one board are not perfect squares. I guess the solution lies here:
StdDraw.filledSquare(cols + .5 , rows + .5, .5);
I also changed rows and cols to class variables and on each call to printCheckerboard i adjust row and col to force a draw to a different section of the canvas. Make a single call to printCheckerboard and adjust this line for ex.:
StdDraw.filledSquare( (cols + .5) / 2, (rows + .5) / 2 , .3);
to see my point.
Hence we can work out the maths to draw to another section.
Perfection of means and confusion of ends seem to characterize our age. Albert Einstein :confused:




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users