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

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:


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

1 Like