Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class FlipImageApp {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int[][] image = { { 1, 34, 23, 23 }, { 3, 4, 345, 23 }, { 5, 6, 234, 34 },
- { 5, 6, 6, 67 }, { 3, 5, 67, 4 } };
- printImage(image);
- String answer = "y";
- while (answer.equals("y")) {
- System.out.println(
- "Press 1 to swap the image horizontally: \n\nPress 2 to swap the image vertically: \n\nPress 0 to terminate the program: ");
- int decision = scanner.nextInt();
- if (decision == 1) {
- System.out.println("Horizontal swap: \n\n ----------------");
- flipHorizontally(image);
- printImage(image);
- } else if (decision == 2) {
- System.out.println("Vertical swap: \n\n ----------------");
- flipVertically(image);
- printImage(image);
- } else if (decision == 0) {
- System.out.println("Thanks for using this program!");
- return;
- } else {
- System.out.println("Please Pick 1 or 2.");
- }
- System.out.println("Again? (y/n)");
- answer = scanner.next();
- if (!answer.equals("y")) {
- System.out.println("Thanks for using this program!");
- }
- }
- }
- public static void printImage(int[][] image) {
- for (int i = 0; i < image.length; i++) {
- for (int j = 0; j < image[i].length; j++) {
- System.out.print(image[i][j] + " ");
- }
- System.out.println("\n\n");
- }
- System.out.println("-------------------\n");
- }
- public static void flipHorizontally(int[][] image) {
- int temp;
- for (int i = 0; i < image.length; i++) {
- for (int j = 0; j < image[i].length / 2; j++) {
- temp = image[i][j];
- image[i][j] = image[i][image[i].length - 1 - j];
- image[i][image[i].length - 1 - j] = temp;
- }
- }
- }
- public static void flipVertically(int[][] image) {
- int temp;
- for (int i = 0; i < image.length / 2; i++) {
- for (int j = 0; j < image[i].length; j++) {
- temp = image[i][j];
- image[i][j] = image[image.length - 1 - i][j];
- image[image.length - 1 - i][j] = temp;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement