Topic 1 - Variables and Data Types
Topic 2 - Conditionals and Strings
Topic 3 - Loops
Topic 4 - Arrays
Topic 5 - File Handling
Semester 1 Projects
Topic 6 - Classes/Objects and Methods
Topic 7 - ArrayLists
Semester Projects

Part 1c: Do Backflips with 2D Structures

Implement the following rotation method:

  • public Picture rotateRight()
    returns a brand-new Picture that holds this Picture, only rotated right by 90 degrees. Thus, if the original Picture was 50×70, the new image will be 70×50.

For the rotateRight method, you will need to use a different one of the Picture class’s constructors in order to create a brand-new image of a different size. Here is an example call:

Picture myPicture = new Picture(newWidth, newHeight);

Of course, this presumes that you have defined newHeight and newWidth beforehand! From there, you will want to loop through all of the pixels, copying their color from the old image (this.) to the appropriate place in the new image (whatever you’ve named it). Be sure to return the new image in the end! Personally, I looped through the destination picture’s pixels and – for each one – computed the location from which to grab the source pixel that corresponded to it. The reverse works equally well.

Note that objects of type Pixel have methods that will get and set all of the components of a color at once! They use Java’s Color class, e.g.,

Color c = some_pixel.getColor();
some_other_pixel.setColor( c ); 

or even the equivalent

some_other_pixel.setColor( some pixel.getColor() );

Tests

When you complete this part, all of the tests from PictureTest_Rotate.java should pass.