Advertisement
uriahheep

Untitled

Jun 8th, 2020
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.03 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class FlipImageApp {
  4.  
  5.     public static void main(String[] args) {
  6.         Scanner scanner = new Scanner(System.in);
  7.         int[][] image = { { 1,  34, 23, 23 }, { 3, 4, 345,  23 }, { 5, 6, 234, 34 },
  8.                 { 5, 6, 6, 67 }, { 3, 5, 67, 4 } };
  9.  
  10.         printImage(image);
  11.         String answer = "y";
  12.         while (answer.equals("y")) {
  13.  
  14.             System.out.println(
  15.                     "Press 1 to swap the image horizontally: \n\nPress 2 to swap the image vertically: \n\nPress 0 to terminate the program: ");
  16.             int decision = scanner.nextInt();
  17.             if (decision == 1) {
  18.  
  19.                 System.out.println("Horizontal swap: \n\n ----------------");
  20.                 flipHorizontally(image);
  21.                 printImage(image);
  22.             } else if (decision == 2) {
  23.  
  24.                 System.out.println("Vertical swap: \n\n ----------------");
  25.                 flipVertically(image);
  26.                 printImage(image);
  27.             } else if (decision == 0) {
  28.                 System.out.println("Thanks for using this program!");
  29.                 return;
  30.             } else {
  31.                 System.out.println("Please Pick 1 or 2.");
  32.             }
  33.             System.out.println("Again? (y/n)");
  34.             answer = scanner.next();
  35.             if (!answer.equals("y")) {
  36.                 System.out.println("Thanks for using this program!");
  37.             }
  38.         }
  39.  
  40.     }
  41.  
  42.     public static void printImage(int[][] image) {
  43.         for (int i = 0; i < image.length; i++) {
  44.             for (int j = 0; j < image[i].length; j++) {
  45.                 System.out.print(image[i][j] + "   ");
  46.             }
  47.             System.out.println("\n\n");
  48.         }
  49.         System.out.println("-------------------\n");
  50.     }
  51.  
  52.     public static void flipHorizontally(int[][] image) {
  53.         int temp;
  54.         for (int i = 0; i < image.length; i++) {
  55.             for (int j = 0; j < image[i].length / 2; j++) {
  56.                 temp = image[i][j];
  57.                 image[i][j] = image[i][image[i].length - 1 - j];
  58.                 image[i][image[i].length - 1 - j] = temp;
  59.             }
  60.         }
  61.     }
  62.  
  63.     public static void flipVertically(int[][] image) {
  64.         int temp;
  65.         for (int i = 0; i < image.length / 2; i++) {
  66.             for (int j = 0; j < image[i].length; j++) {
  67.                 temp = image[i][j];
  68.                 image[i][j] = image[image.length - 1 - i][j];
  69.                 image[image.length - 1 - i][j] = temp;
  70.             }
  71.         }
  72.     }
  73.  
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement