2017-04-03 9 views
0

私は画像を回転させる方法について学習しており、画像を180度と270度回転させる必要があります。私は270を開始していないし、範囲外のエラーを修正する方法を理解できないようだ。Javaの初心者の座標が範囲外

public Picture rotate180() 
{ 
    Picture rotated = new Picture(); 

    for (int x = 0, roty = 0; x < getWidth(); x++, roty++) 
    { 
     for (int y = 0, rotx = getWidth()-1; y<getHeight(); y++, rotx--) 
     { 
     Pixel oldPixel = getPixel(x,y); 
     Pixel newPixel = getPixel(rotx,roty); 
     newPixel.setColor(oldPixel.getColor()); 
     } 
    } 
return rotated; 
} 
+0

返品のタイプ –

+0

私は実際にそれを編集しました。投稿後に私は返品の部分を切り捨てたことを認識しました。すべてはそれに固定されています。 – CBlair

+0

rotate180()で範囲外の例外が発生しますか?どの行?また、** rotate **という戻り値は作成後も変更されません。 ** rotate **から** newPixel **を取得し、その色を設定する必要があります。 –

答えて

0

前述のように、あなたのforループはピクチャが回転されても何もしません。私はあなたの "getWidth()"呼び出しが '回転'の幅を取得すると仮定します。もしそうなら、rotate.getWidth()を入れてそのプロパティにアクセスする必要があります。他のコードでも同じアプローチを使用してください。 rotated.getHeight(); rotated.getPixel (x, y) など また、forループのインクリメントとして2つのint値を使用していますが、これらの値は常に等しいものです。使用することができます。より一般的なケースでは

1
private enum Degree { 
    _90_ { 
     public Picture rotate(Picture picture) { 
      Picture res = swapRowCol(new Picture(picture.height, picture.width), picture); 

      // mirror each column 
      for (int col = 0; col < res.width; col++) 
       for (int row1 = 0, row2 = res.height - 1; row1 < row2; row1++, row2--) 
        swapColor(res, col, row1, col, row2); 

      return res; 
     } 
    }, 
    _180_ { 
     public Picture rotate(Picture picture) { 
      Picture res = new Picture(picture.width, picture.height); 

      // desc rows and mirror each one 
      for (int row1 = 0, row2 = res.height - 1; row1 < res.height; row1++, row2--) 
       for (int col1 = 0, col2 = picture.width - 1; col1 < picture.width; col1++, col2--) 
        res.getPixel(col2, row2).setColor(picture.getPixel(col1, row1).getColor()); 

      return res; 
     } 
    }, 
    _270_ { 
     public Picture rotate(Picture picture) { 
      Picture res = swapRowCol(new Picture(picture.height, picture.width), picture); 

      // mirror each row 
      for (int row = 0; row < res.height; row++) 
       for (int col1 = 0, col2 = res.width - 1; col1 < col2; col1++, col2--) 
        swapColor(res, col1, row, col2, row); 

      return res; 
     } 
    }; 

    public abstract Picture rotate(Picture picture); 

    protected static Picture swapRowCol(Picture dst, Picture src) { 
     // (x;y) -> (y;x) 
     for (int row = 0; row < src.height; row++) 
      for (int col = 0; col < src.width; col++) 
       dst.getPixel(row, col).setColor(src.getPixel(col, row).getColor()); 

     return dst; 
    } 

    protected static void swapColor(Picture res, int srcCol, int srcRow, int dstCol, int dstRow) { 
     Pixel pixel1 = res.getPixel(srcCol, srcRow); 
     Pixel pixel2 = res.getPixel(dstCol, dstRow); 
     int color = pixel1.getColor(); 
     pixel1.setColor(pixel2.getColor()); 
     pixel2.setColor(color); 
    } 
} 
1

、任意の角度に回転するために、あなたは持つjavax.imageioを使用する必要があります。*あなたの方法は、 `Picture`を持っているにもかかわらず、何も戻っていない

import java.awt.Graphics2D; 
import java.awt.RenderingHints; 
import java.awt.geom.AffineTransform; 
import java.awt.geom.Point2D; 
import java.awt.image.BufferedImage; 

public final class ImageUtils { 
    public static BufferedImage rotateImage(BufferedImage image, int degreeAngle) { 
     degreeAngle = normalizeAngle(degreeAngle); 

     if (degreeAngle == 0 || image == null) 
      return image; 

     double theta = Math.toRadians(degreeAngle); 
     double cosTheta = Math.abs(Math.cos(theta)); 
     double sinTheta = Math.abs(Math.sin(theta)); 
     int width = (int)Math.rint(image.getWidth() * cosTheta + image.getHeight() * sinTheta); 
     int height = (int)Math.rint(image.getWidth() * sinTheta + image.getHeight() * cosTheta); 
     Graphics2D graphics = null; 

     try { 
      AffineTransform at = new AffineTransform(); 
      BufferedImage rotatedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); 
      graphics = rotatedImage.createGraphics(); 

      at.rotate(theta, image.getWidth()/2, image.getHeight()/2); 
      at.preConcatenate(balanceImagePosition(at, image)); 

      graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); 
      graphics.drawRenderedImage(image, at); 

      return rotatedImage; 
     } finally { 
      if (graphics != null) 
       graphics.dispose(); 
     } 
    } 

    /** 
    * Transform given angle in degree of any range to the [0;360) range 
    * 
    * @param degreeAngle angle in degree 
    * @return normalized angle in degree 
    */ 
    private static int normalizeAngle(int degreeAngle) { 
     if (degreeAngle <= -360 || degreeAngle >= 360) 
      degreeAngle %= 360; 
     if (degreeAngle < 0) 
      degreeAngle += 360; 

     return degreeAngle; 
    } 

    private static AffineTransform balanceImagePosition(AffineTransform at, BufferedImage image) { 
     Point2D[] points = { new Point2D.Double(0, 0), new Point2D.Double(image.getWidth(), 0), new Point2D.Double(0, 
       image.getHeight()), new Point2D.Double(image.getWidth(), image.getHeight()) }; 
     double tx = image.getWidth(); 
     double ty = image.getHeight(); 

     for (Point2D point : points) { 
      Point2D tmp = at.transform(point, null); 
      tx = Math.min(tx, tmp.getX()); 
      ty = Math.min(ty, tmp.getY()); 
     } 

     at = new AffineTransform(); 
     at.translate(-tx, -ty); 

     return at; 
    } 
} 
関連する問題