asked 133k views
0 votes
Package Unit3_Mod2;

public class ImageExample3 {

public static void main (String[] argv)
{
int[][][] A = {
{
{255,200,0,0}, {255,150,0,0}, {255,100,0,0}, {255,50,0,0},
},
{
{255,0,200,0}, {255,0,150,0}, {255,0,100,0}, {255,50,0,0},
},
{
{255,0,0,200}, {255,0,0,150}, {255,0,0,100}, {255,0,0,50},
},
};

// Add one pixel on each side to give it a "frame"
int[][][] B = frameIt (A);

ImageTool im = new ImageTool ();
im.showImage (B, "test yellow frame");
}

public static int[][][] frameIt (int[][][] A)
{
//add code here
}
}
Make a yellow frame , one pixel to each side.

asked
User Codious
by
7.6k points

1 Answer

3 votes

Answer:

To add a yellow frame of one pixel to each side of the input image represented by a 3D array A, we can create a new 3D array B with dimensions A.length + 2 by A[0].length + 2 by A[0][0].length, and set the values of the pixels in the frame to the RGB values for yellow (255, 255, 0).

Here is the implementation of the frameIt method:

public static int[][][] frameIt(int[][][] A) {

int height = A.length;

int width = A[0].length;

int depth = A[0][0].length;

int[][][] B = new int[height + 2][width + 2][depth];

// Set the values for the corners of the frame

B[0][0] = new int[] {255, 255, 0, 0};

B[0][width + 1] = new int[] {255, 255, 0, 0};

B[height + 1][0] = new int[] {255, 255, 0, 0};

B[height + 1][width + 1] = new int[] {255, 255, 0, 0};

// Set the values for the top and bottom rows of the frame

for (int j = 1; j <= width; j++) {

B[0][j] = new int[] {255, 255, 0, 0};

B[height + 1][j] = new int[] {255, 255, 0, 0};

}

// Set the values for the left and right columns of the frame

for (int i = 1; i <= height; i++) {

B[i][0] = new int[] {255, 255, 0, 0};

B[i][width + 1] = new int[] {255, 255, 0, 0};

}

// Copy the original image into the center of the new array

for (int i = 0; i < height; i++) {

for (int j = 0; j < width; j++) {

for (int k = 0; k < depth; k++) {

B[i + 1][j + 1][k] = A[i][j][k];

}

}

}

return B;

}

Note that the RGB values for yellow are (255, 255, 0), but since the input array is using a 4-channel representation with an alpha channel (transparency), we are setting the alpha channel to 0 for all the yellow pixels. This means that the yellowframe will be fully opaque.

Hope this helps!

answered
User UrMi
by
7.8k points