Skip to content
Snippets Groups Projects
Commit 359156b9 authored by Diego Imbert's avatar Diego Imbert
Browse files

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.
parent 0c28b97a
No related branches found
No related tags found
No related merge requests found
java -classpath out/ Main
java -classpath out/ Main "$@"
......@@ -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
......@@ -155,7 +158,9 @@ public class Crossword
System.out.println("Error: Incorrect y-coordinates for Word");
return;
}
if (USE_ENHANCED_GUI)
EnhancedCrosswordGUI.display(array, PositionY, PositionX, EndY, EndX);
else CrosswordGUI.display(array, PositionY, PositionX, EndY, EndX);
}
}
......
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
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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment