View this PageEdit this Page (locked)Attachments to this PageHistory of this PageHomeRecent ChangesSearch the SwikiHelp Guide

General left rotation method

  /**
   * Method to copy the current picture to a new
   * picture but rotated to the left 90 degrees
   * @return the new rotated picture
   */
  public Picture copyWithLeftRotation()
  {
    Picture targetPicture = new Picture(this.getHeight(),
                                        this.getWidth());
    Pixel sourcePixel = null;
    Pixel targetPixel = null;
    
    // loop through the columns
    for (int sourceX = 0; 
         sourceX < this.getWidth(); 
         sourceX++)
    {
      // loop through the rows
      for (int sourceY = 0; 
           sourceY < this.getHeight(); 
           sourceY++)
      {
        // set the target pixel color to the source pixel color
        sourcePixel = this.getPixel(sourceX,sourceY);
        targetPixel = targetPicture.getPixel(sourceY, 
              this.getWidth() - 1 - sourceX);
        targetPixel.setColor(sourcePixel.getColor());
      }
    }
    return targetPicture;
  }
 


The following shows how to use the general left rotation method
/**
   * Method to copy Katie rotated to the left 90 degrees
   * @return the picture after Katie has been copied and rotated to the left 90
   */
  public static Picture copyKatieLeftRotation()
  {
    String sourceFile = Picture.getMediaPath("KatieFancy.jpg");
    Picture sourcePicture = new Picture(sourceFile);
    String targetFile = Picture.getMediaPath("7inx95in.jpg");
    Picture targetPicture = null;
    
    // copy with left rotation
    targetPicture = sourcePicture.copyWithLeftRotation();
    
    // show the source and target pictures
    sourcePicture.show();
    targetPicture.show();
    
    return targetPicture;
  }


Link to this Page