1

2D 配列を操作する 2 つのメソッドを実装する必要があります。

  • grayAndFlipLeftToRight- このメソッドは、画像をグレー スケール値の 2D 配列に変換し、左から右に反転します。つまり、左を向いていたオブジェクトが右を向くようになります。一番左の列の要素は、一番右の列の要素と交換されます。

  • grayAndRotate90- このメソッドは、画像をグレースケール値の 2D 配列に変換してから、時計回りに 90 度回転させます (右側に配置します)。

コード:

public class PictureUtil
{
    /**
     * Gets a version of the given Picture in gray scale and flipped left to right
     * @param pic the Picture to convert to gray scale and flip
     * @return a version of the original Picture in gray scale and flipped
     * left to right.
     */
    public static Picture grayAndFlipLeftToRight( Picture pic)
    {
        int[][] pixels = pic.getGrayLevels();

        for (int i = 0; i < pixels.length; i++) {             
            for (int fromStart = 0; fromStart < pixels[0].length; fromStart++) { 
                int fromEnd = pixels[0].length-1;

                while (fromEnd >= 0) {
                    int saved = pixels[i][fromStart];
                    pixels[i][fromStart] = pixels[i][fromEnd];
                    pixels[i][fromEnd] = saved; 

                    fromEnd--;
                }                                               
            }
        }

        return new Picture(pixels);
    }

    /**
     * Gets a version of the given Picture in gray scale and rotated 90 degrees clockwise
     * @param pic the Picture to convert to gray scale and rotate 90 degrees clockwise
     * @return a version of the original Picture in gray scale and rotated 90 degrees clockwise
     */
    public static Picture grayAndRotate90( Picture pic)
    {
        int[][] pixels = pic.getGrayLevels(); 

        int numberOfColums = pixels.length;
        int numberOfRows = pixels[0].length;        
        int[][] rotate = new int[numberOfRows][numberOfColums];

        for (int i = 0; i < pixels.length; i++) { 
            int seek = 0;
            for (int j = 0; j < pixels[0].length; j++) {            
                pixels[i][j] = rotate[seek][numberOfColums - 1];
                seek++;
            }
        }

        return new Picture(rotate); 
    }
}

私の実装は、両方の方法で正しく機能しません。

  • この問題を解決するには?
4

2 に答える 2