diff --git a/tp2/run.sh b/tp2/run.sh index d96abba3f9be3cab2b7a8cc0c7efbf43cb20d6bb..44bffe576695c0f866acb2d6c17d906c624dae49 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 f4fa0e863042c0d4841e569025b7f0641fea102b..53a6d8fcdaf33cb3dde1c9b9dfda01ef27354c00 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 0000000000000000000000000000000000000000..92747101f47beb375861dad8396f87b42139b65b --- /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 49f3dcbdd65d530d714967f74790a0a05dac4fc4..10e8dfdca0bbd9faed5e0465fd7b2f5f5bab6104 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