2D Arrays: Image Manipulation Project

Hi, CodingLovers
im doing last project in Java beginners and got stuck in 2D Arrays: Image Manipulation Project
How to make Negative Version of the Image
step 10-15
i did in many times but cannot find the right one. Need a help
https://www.codecademy.com/courses/learn-java/projects/2-d-arrays-image-manipulation-project

2 Likes

In the method I created to get the new 2D array of inverted-color pixels,
I did something like
new_value = 255 - old_value
for each color of the pixel:

details

I looped through the 2D array that was a parameter for the function (using nested loops) to get each pixel individually.

  public static int[][] negativeColor(int[][] imageTwoD) { 
    int rows = imageTwoD.length;
    int cols = imageTwoD[0].length;
    int negative[][] = new int[rows][cols];
    for (int i = 0; i < rows; i++) {
      for (int j = 0; j < cols; j++) {
        // single pixel
        int[] rgba = getRGBAFromPixel(imageTwoD[i][j]);
        rgba[0] = 255 - rgba[0];  // red
        rgba[1] = 255 - rgba[1];  // green
        rgba[2] = 255 - rgba[2];  // blue
        negative[i][j] = getColorIntValFromRGBA(rgba);
      }
    }
    return negative;
  }

In the main method, I put

    int[][] negative = negativeColor(imageData); # negativeColor is the method for getting the negative (as a 2D array)
    twoDToImage(negative, "./apple_negative.jpg");

and then changed the “address” in the fake browser to end with apple_negative.jpg instead of apple.jpg

3 Likes

Hey! I’m working on this project right now and I noticed that this solution works, however I don’t quite understand how the instructions are asking you to do that.
The final step in this method says to " return the modified image. In the main() method, load an image and pass it into the negativeColor() method. Save the modified image and view it in the browser", but maybe I’m just not understanding how the solution corresponds to the solution they’d like to see.

2 Likes

can You give me your project 2D Arrays: Image Manipulation Project in Codecademy ?

Thank you so much!

I also love the way that you separated the rows and columns in your negativeColor method. I might try that in the next tasks.

Hi, I got the negative of the apple image but what are the 2 errors indicated? Obviously, it doesn’t like the way I initialized the new variable.

It says:’ ImageProcessing.java:49: error: cannot assign a value to final variable length
negativeImageData.length = imageTwoD.length;
^
ImageProcessing.java:50: error: cannot assign a value to final variable length
negativeImageData[0].length = imageTwoD[0].length;
^
2 errors

Web Browser’

The bit of code it doesn’t accept is this:

public static int negativeColor(int imageTwoD) {

int negativeImageData;
negativeImageData.length = imageTwoD.length;
negativeImageData[0].length = imageTwoD[0].length;

for (int k=0; k < imageTwoD.length; k++){
for (int l=0; l < imageTwoD[0].length; l++){
int rgba = getRGBAFromPixel(imageTwoD[k][l]);
rgba [0] = 255-rgba [0];
rgba [1] = 255-rgba [1];
rgba [2] = 255-rgba [2];
negativeImageData [k][l] = getColorIntValFromRGBA(rgba);
}
}
return negativeImageData;
}

If anybody knows the answer please let me know!

Thank you!
Pat

I don’t think that you can set the .length for an array in javascript.

I think you’d have to create an array with the appropriate length instead.
so instead of

int[][]  negativeImageData;
negativeImageData.length = imageTwoD.length;

you could do

int[][]  negativeImageData;
negativeImageData = new int[imageTwoD.length][];

or

int[][]  negativeImageData = new int[imageTwoD.length][];

Figured out another way to implement the stretchHorizontally function.

	public static int[][] stretchHorizontally(int[][] imageTwoD) {
    int[][] stretchedImage = new int[imageTwoD.length][imageTwoD[0].length * 2];
    for (int i = 0; i < stretchedImage.length; i++) {
      for (int j = 0; j < stretchedImage[0].length; j++) {
        stretchedImage[i][j] = imageTwoD[i][(int) Math.floor(j / 2.0)];
      }
    }
		return stretchedImage;
	}

I’m working on this project (https://www.codecademy.com/courses/learn-java/projects/2-d-arrays-image-manipulation-project) and am struggling with multiple problems.

Can someone please send me their work, so I can reference it and see where I went wrong? (click on the copy code–clipboard-- in the code editor and just paste here in the replies.
I would highly appreciate it.

Thank you so much in advance!!