From 359156b9a020f3c02112edfe8894871c467a374f Mon Sep 17 00:00:00 2001
From: Diego Imbert <imbert.diego05000@gmail.com>
Date: Sun, 20 Sep 2020 21:29:51 +0200
Subject: [PATCH] Had a bit of fun adding support for diagonal highlighting
 (works with run.sh as well) You can now pass "enhanced" as an argument when
 lauching the program to use the custom EnhancedCrosswordGUI class. The class
 is the same as CrosswordGUI but functions were edited to support diagonal
 highlighting.

---
 tp2/run.sh                        |  2 +-
 tp2/src/Crossword.java            |  9 +++-
 tp2/src/EnhancedCrosswordGUI.java | 76 +++++++++++++++++++++++++++++++
 tp2/src/Main.java                 | 12 +++++
 4 files changed, 96 insertions(+), 3 deletions(-)
 create mode 100644 tp2/src/EnhancedCrosswordGUI.java

diff --git a/tp2/run.sh b/tp2/run.sh
index d96abba..44bffe5 100755
--- a/tp2/run.sh
+++ b/tp2/run.sh
@@ -1 +1 @@
-java -classpath out/ Main
+java -classpath out/ Main "$@"
diff --git a/tp2/src/Crossword.java b/tp2/src/Crossword.java
index f4fa0e8..53a6d8f 100644
--- a/tp2/src/Crossword.java
+++ b/tp2/src/Crossword.java
@@ -7,6 +7,9 @@ import java.nio.file.Files;
 
 public class Crossword
 {
+   private boolean USE_ENHANCED_GUI = false;
+   public void useEnhancedGUI(boolean b) { USE_ENHANCED_GUI = b; }
+
    public final static String DEFAULT_FILE_PATH = "WORDS.txt";
 
    private char array[][]; 	// Tableaux 2D contenant les caractères
@@ -154,8 +157,10 @@ public class Crossword
 		if ((PositionY<0) || (EndY>=rows)) {
 			System.out.println("Error: Incorrect y-coordinates for Word");
 			return;		
-		}
-		EnhancedCrosswordGUI.display(array, PositionY, PositionX, EndY, EndX);  
+      }
+      if (USE_ENHANCED_GUI)
+         EnhancedCrosswordGUI.display(array, PositionY, PositionX, EndY, EndX);  
+      else CrosswordGUI.display(array, PositionY, PositionX, EndY, EndX);
    }
    
 }
diff --git a/tp2/src/EnhancedCrosswordGUI.java b/tp2/src/EnhancedCrosswordGUI.java
new file mode 100644
index 0000000..9274710
--- /dev/null
+++ b/tp2/src/EnhancedCrosswordGUI.java
@@ -0,0 +1,76 @@
+import java.awt.Color;
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+
+import javax.swing.JTextField;
+
+/**
+ * This class is behaves exactly like CrosswordGUI 
+ * except that it supports diagonal highlighting.
+ * 
+ * It was created because CrosswordGUI was asked not to be modified.
+ */
+class EnhancedCrosswordGUI extends CrosswordGUI {
+    public enum Directions {
+        HORIZONTAL, VERTICAL, DIAGONAL_TOP, DIAGONAL_BOTTOM
+    };
+
+    public static void display(char array[][], int y1, int x1, int y2, int x2) {
+        final EnhancedCrosswordPanel panel = new EnhancedCrosswordPanel(array);
+        if (y1 == y2 && x1 <= x2)
+            panel.highlight(y1, x1, 1 + x2 - x1, Directions.HORIZONTAL);
+        else if (x1 == x2 && y1 <= y2)
+            panel.highlight(y1, x1, 1 + y2 - y1, Directions.VERTICAL);
+        else if (x1 <= x2 && y1 <= y2)
+            panel.highlight(y1, x1, 1 + x2 - x1, Directions.DIAGONAL_BOTTOM);
+        else if (x1 <= x2 && y1 >= y2)
+            panel.highlight(y1, x1, 1 + x2 - x1, Directions.DIAGONAL_TOP);
+
+        // We call the private method displayPanel(panel) via reflection
+        Method displayPanelMethod;
+        try {
+            displayPanelMethod = CrosswordGUI.class.getDeclaredMethod("displayPanel", CrosswordPanel.class);
+            displayPanelMethod.setAccessible(true);
+            displayPanelMethod.invoke(null, panel);
+        } catch (Exception e) {
+            System.err.println("Couldn't reflect method displayPanel :(");
+            e.printStackTrace();
+            return;
+        }
+    }
+
+    static public class EnhancedCrosswordPanel extends CrosswordPanel {
+        public EnhancedCrosswordPanel(char[][] array) {
+            super(array);
+        }
+
+        public void highlight(int startY, int startX, int length, Directions dir) {
+            // We get around accessing private member textFields using reflection
+            JTextField textFields[][];
+            {
+                try {
+                    Field f = CrosswordPanel.class.getDeclaredField("textFields");
+                    f.setAccessible(true);
+                    textFields = (JTextField[][]) f.get(this);
+
+                } catch (Exception e) {
+                    System.err.println("Couldn't reflect member textFields[][] :(");
+                    e.printStackTrace();
+                    return;
+                }
+            }
+
+            int incrementX = 0, incrementY = 0;
+            if (dir.equals(Directions.HORIZONTAL)) incrementX = 1;
+            else if (dir.equals(Directions.VERTICAL)) incrementY = 1;
+            else if (dir.equals(Directions.DIAGONAL_TOP)) { incrementX = 1; incrementY = -1; }
+            else if (dir.equals(Directions.DIAGONAL_BOTTOM)) { incrementX = 1; incrementY = 1; }
+
+            for (int i = 0; i < length; i++)
+                textFields[startY + (incrementY * i)][startX + (incrementX * i)]
+                    .setBackground(Color.green);
+                    
+            repaint();
+        }
+    }
+}
\ No newline at end of file
diff --git a/tp2/src/Main.java b/tp2/src/Main.java
index 49f3dcb..10e8dfd 100644
--- a/tp2/src/Main.java
+++ b/tp2/src/Main.java
@@ -1,4 +1,5 @@
 import java.io.*;
+import java.util.Arrays;
 import java.util.Scanner;
 
 /*** Methode Main : Lire le fichier donné et créer un tableau 2D qui contint le characters du fichier. 
@@ -10,12 +11,16 @@ public class Main {
 
     public static void main(String args[]) throws IOException {
 		Main mainInstance = new Main();
+		boolean useEnhancedGUI = mainInstance.checkEnhancedGUIArgs(args);
 		File crosswordFile = new File(mainInstance.promptFilePath());
+
 		if (!crosswordFile.exists()) {
 			System.err.printf("Error: The file %s doesn't exist\n", crosswordFile.getName());
 			return;
 		}
 		Crossword crossword = new Crossword(crosswordFile);
+		crossword.useEnhancedGUI(useEnhancedGUI);
+
 		crossword.display();
 
 		String wordInput;
@@ -44,4 +49,11 @@ public class Main {
 		String input = consoleScanner.nextLine();
 		return input;
 	}
+	private boolean checkEnhancedGUIArgs(String args[]) {
+		if (Arrays.asList(args).contains("enhanced")) return true;
+		else {
+			System.out.println("Pro Tip: you can pass \"enhanced\" as an argument to support diagonal highlighting");
+			return false;
+		}
+	}
 }
\ No newline at end of file
-- 
GitLab