Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
public class HelloWorld{
public static void main(String []args){
Pixel[][] arr = {
{{0, 0, 100},{0,1,100},{0,2,100}{0,3,100}},
{{0, 0, 100},{0,1,100},{0,2,100}{0,3,100}},
{{0, 0, 100},{0,1,100},{0,2,100}{0,3,100}},
{{0, 0, 100},{0,1,100},{0,2,100}{0,3,100}}
};
printArr(arr);
}
public static void printArr(Pixel[][] arr){
for(int i = 0, i < arr.length; i++){
for(int j = 0; j < arr[0].length; j++){
System.out.print(arr[i][j].toString() + " ");
}
System.out.println();
}
}
}
public class Pixel
{
private int red;
private int green;
private int blue;
public Pixel(int myRed, int myGreen, int myBlue)
{
red = myRed;
green = myGreen;
blue = myBlue;
}
public String toString()
{
return "(" + red + ", " + green + ", " + blue + ")";
}
}
public class AlterImage
{
/** Converts 3 arrays of color values into one array
* of Pixel objects
*
* @param reds the red color values
* @param greens the green color values
* @param blues the blue color values
* @return the Pixel array generated with information
* in the red, green and blue arrays
*
* Precondition: The three color arrays are all the same
* size and contain int values in the range 0-255.
* Postcondition: The returned array is the same size as the
* 3 color arrays.
*/
public Pixel[][] generatePixelArray(int[][] reds, int[][] greens, int[][] blues)
{ /* to be implemented in part (a) */
Pixel[][] result = new Pixel[reds.length][reds[0].length];
for(int r = 0; r < result.length; r++){
for(int c = 0; c < result[0].length; c++){
result[r][c] = new Pixel(reds[r][c], greens[r][c], blues[r][c]);
}
}
return result;
}
/** Flips a 2D array of Pixel objects either
* horizontally or vertically
*
* @param image the 2D array of Pixel objects to be processed
* @param horiz true: flip horizontally, false: flip vertically
* @return the processed image
*/
public Pixel[][] flipImage(Pixel[][] image, boolean horiz)
{
Pixel[][] result = new Pixel[image.length][image[0].length];
if(horiz){
for(int i = 0; i < image.length; i++){
for(int j = 0; j < image[0].length; j++){
result[image.length - i - 1][j] = image[i][j];
}
}
}
else{
for(int i = 0; i < image.length; i++){
for(int j = 0; j < image[0].length; j++){
result[i][image[0].length - j -1] = image[i][j];
}
}
return result;
}
}
Advertisements
We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.
AcceptLearn more