Question
In this part of the assignment, you need to write a checkerboard(int width, Color c) method that takes two parameters: the size of the checks
In this part of the assignment, you need to write a checkerboard(int width, Color c) method that takes two parameters: the size of the checks and the color of the checks. This method should iterate through the image pixels, turning the appropriate pixels the color passed in. The three images above show the method run on three different input photos, with a different check size and color passed in each time. With the picture PilotBruce.jpg, we called checkerboard(15, Color.RED). With the bread picture, we called checkerboard(50, Color.BLUE) and with the warholized picture of Celine, we called checkerboard(30, Color.BLACK). In order to write this method, you will need to:
Check the precondition that the check width passed in is less than the width of the picture object.
Iterate through every pixel in the image.
Check each pixel to see if it should be checkerboarded (this is the hard part, sketch this out on paper to try figure it out)
If the pixel should be checkerboarded, change its color to the passed in color.
Hint: work on figuring out how to put the checks in the odd rows first. So, get something like this going:
Then, once you get that working, figure out how to also put checks in the even rows, but staggered over.
Further hint: youll really need to get out some paper and work on this with a pencil to figure out the pattern and how to program it. Labeling graph paper with array indices would be useful for this. You need to come up with a way to mathematically determine when the checks should be drawn. The modulus operator will be useful. Look back at the process we used in Lab 9, Part B, that may be helpful to you for a way to think this through.
/** Method to put a checkerboard pattern on a picture *Assignment 4 part 3 */ public void checkerboard(int width, Color c){ // Checks if the first parameter is more than half the width the picture if (width > (int)(this.getWidth() * 0.5)){ System.out.println("Error! First parameter is more than half the width the picture!"); return; } Pixel pixel = null; // loops through the pixels in this picture for (int x = 0; x < getWidth(); x++){ for (int y = 0; y < getHeight(); y++){ // Creates a square every other width and every even row and colunm if (((x/width) % 2 == 0) && ((y/width) % 2 == 0)){ pixel = getPixel(x ,y); pixel.setColor(c); } } } // loops through the pixels in this picture for (int x = 0; x < getWidth(); x++){ for (int y = 0; y < getHeight(); y++){ // Creates a square every other width and every odd row and colunm if (((x/width) % 2 == 1) && ((y/width) % 2 == 1)){ pixel = getPixel(x ,y); pixel.setColor(c); } } } } }
How would I make this work?
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started