|
| 1 | +package vip.floatationdevice.wordlehelper; |
| 2 | + |
| 3 | +import javax.swing.*; |
| 4 | + |
| 5 | +import java.awt.*; |
| 6 | +import java.awt.event.ActionEvent; |
| 7 | +import java.awt.event.ActionListener; |
| 8 | +import java.awt.event.KeyEvent; |
| 9 | + |
| 10 | +import static vip.floatationdevice.wordlehelper.Main.*; |
| 11 | + |
| 12 | +public class GUI extends JFrame |
| 13 | +{ |
| 14 | + /* |
| 15 | + * The perfect layout of the window |
| 16 | + * - but I can't get there |
| 17 | + * |
| 18 | + * +-------------------------------+ |
| 19 | + * | WordleHelper 1.1 [R][-][X] | |
| 20 | + * +-----------+-------------------+ |
| 21 | + * | _ _ _ _ _ | Possible words: | |
| 22 | + * | _ _ _ _ _ | xxxxx xxxxx xxxxx | |
| 23 | + * | _ _ _ _ _ | xxxxx xxxxx xxxxx | |
| 24 | + * | _ _ _ _ _ | xxxxx xxxxx xxxxx | |
| 25 | + * | _ _ _ _ _ | xxxxx xxxxx xxxxx | |
| 26 | + * | _ _ _ _ _ | xxxxx xxxxx xxxxx | |
| 27 | + * +-----------+-------------------+ |
| 28 | + * | Try {}/6 | {} words left [?] | |
| 29 | + * +-------------------------------+ |
| 30 | + * |
| 31 | + * Help: |
| 32 | + * Each line accepts first 5 letters and then 5 numbers from 0 to 2. |
| 33 | + * After the 5th number is typed the possible words will be calculated. |
| 34 | + * Redo by pressing the backspace key. |
| 35 | + */ |
| 36 | + |
| 37 | + //acceptable chars: 0-2, a-z, backspace |
| 38 | + char[] acceptableChars = "abcdefghijklmnopqrstuvwxyz012\b".toCharArray(); |
| 39 | + //possible words field |
| 40 | + private final JTextArea possibleWords=new JTextArea("Possible words will be shown here\nEnter your first 5 letters and then 5 numbers to see them\nPress the '?' button on the bottom left for help"); |
| 41 | + //result board, 6 lines, 5 letters each line |
| 42 | + private final JLabel[][] board=new JLabel[6][5]; |
| 43 | + //tries status |
| 44 | + private final JLabel tries=new JLabel("Try {}/6"); |
| 45 | + //'words left' status |
| 46 | + private final JLabel wordsLeft=new JLabel("{} words left"); |
| 47 | + //[?] button that shows the help message |
| 48 | + private final JButton help=new JButton("?"); |
| 49 | + //[R] button that resets the board, the tries and the possible words |
| 50 | + private final JButton reset=new JButton("R"); |
| 51 | + |
| 52 | + //wordle result letter background colors |
| 53 | + Color |
| 54 | + unset = new Color(0x121213), |
| 55 | + offTargeted = new Color(0x3a3a3c), |
| 56 | + displaced = new Color(0xb59f3b), |
| 57 | + hit = new Color(0x538d4e); |
| 58 | + |
| 59 | + //set every JLabel in the board with '_' |
| 60 | + private void initBoard() |
| 61 | + { |
| 62 | + for(int i=0;i<6;i++) |
| 63 | + for(int j=0;j<5;j++) |
| 64 | + { |
| 65 | + board[i][j]=new JLabel("_"); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + //global key event dispatcher |
| 70 | + KeyEventDispatcher keyEventDispatcher = new KeyEventDispatcher() |
| 71 | + { |
| 72 | + @Override |
| 73 | + public boolean dispatchKeyEvent(KeyEvent e) |
| 74 | + { |
| 75 | + if(e.getID()==KeyEvent.KEY_PRESSED) |
| 76 | + for(char c : acceptableChars) |
| 77 | + if(e.getKeyChar()==c) |
| 78 | + System.out.println(e.getKeyChar()+" key code: "+e.getKeyCode()); |
| 79 | + return false; |
| 80 | + } |
| 81 | + }; |
| 82 | + |
| 83 | + static boolean startupComplete = false; |
| 84 | + |
| 85 | + //constructor |
| 86 | + public GUI() |
| 87 | + { |
| 88 | + try{UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());}catch(Throwable ex){System.out.println("Error setting native LAF: "+ex);} |
| 89 | + JFrame startup=new JFrame(); |
| 90 | + if(!startupComplete) |
| 91 | + { |
| 92 | + //show a startup window |
| 93 | + startup.setSize(200,50); |
| 94 | + startup.setLocationRelativeTo(null); |
| 95 | + startup.setUndecorated(true); |
| 96 | + startup.setAlwaysOnTop(true); |
| 97 | + startup.setResizable(false); |
| 98 | + JLabel startupText = new JLabel("Starting WordleHelper..."); |
| 99 | + startupText.setHorizontalAlignment(SwingConstants.CENTER); |
| 100 | + startup.add(startupText); |
| 101 | + startup.setVisible(true); |
| 102 | + /*try { |
| 103 | + Thread.sleep(1000); //magic, don't touch |
| 104 | + }catch (InterruptedException e){}*/ |
| 105 | + } |
| 106 | + |
| 107 | + //set main window |
| 108 | + setTitle("WordleHelper 1.1"); |
| 109 | + setSize(640,480); |
| 110 | + setResizable(false); |
| 111 | + setLocationRelativeTo(null); |
| 112 | + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
| 113 | + setLayout(null); |
| 114 | + JPanel panel=new JPanel(null); |
| 115 | + panel.setBounds(0,0,640,480); |
| 116 | + panel.setBackground(Color.DARK_GRAY); |
| 117 | + add(panel); |
| 118 | + //set result board |
| 119 | + initBoard(); |
| 120 | + //set tries status |
| 121 | + tries.setBounds(10,330,100,20); |
| 122 | + tries.setForeground(Color.WHITE); |
| 123 | + //set words left status |
| 124 | + wordsLeft.setBounds(110,330,100,20); |
| 125 | + wordsLeft.setForeground(Color.WHITE); |
| 126 | + //set possible words field |
| 127 | + possibleWords.setBounds(270,10,360,420); |
| 128 | + possibleWords.setEditable(false); |
| 129 | + possibleWords.setLineWrap(true); |
| 130 | + //set color |
| 131 | + possibleWords.setBackground(Color.GRAY); |
| 132 | + possibleWords.setForeground(Color.WHITE); |
| 133 | + //set help and reset button |
| 134 | + help.setBounds(10,400,50,30); |
| 135 | + reset.setBounds(70,400,50,30); |
| 136 | + //set global key event listener |
| 137 | + //help.addKeyListener(boardKeyListener); |
| 138 | + KeyboardFocusManager.getCurrentKeyboardFocusManager() |
| 139 | + .addKeyEventDispatcher(keyEventDispatcher); |
| 140 | + //set help button's action listener |
| 141 | + help.addActionListener(new ActionListener() |
| 142 | + { |
| 143 | + @Override |
| 144 | + public void actionPerformed(ActionEvent e) |
| 145 | + { |
| 146 | + //debug |
| 147 | + System.out.println("help button pressed"); |
| 148 | + //show help dialog |
| 149 | + JOptionPane.showMessageDialog(GUI.this, |
| 150 | + "Each line accepts first 5 letters and then 5 numbers from 0 to 2.\n" + |
| 151 | + "After the 5th number is typed the possible words will be calculated.\n" + |
| 152 | + "Redo by pressing the backspace key.\n\n" + |
| 153 | + "· 0 means the letter is not in the word,\n" + |
| 154 | + "· 1 means the letter is at the right position,\n" + |
| 155 | + "· 2 means the letter is in the wrong position.", |
| 156 | + "Help", |
| 157 | + JOptionPane.INFORMATION_MESSAGE); |
| 158 | + } |
| 159 | + }); |
| 160 | + //set reset button's action listener |
| 161 | + reset.addActionListener(new ActionListener() |
| 162 | + { |
| 163 | + @Override |
| 164 | + public void actionPerformed(ActionEvent e) |
| 165 | + { |
| 166 | + System.out.println("resetting"); |
| 167 | + dispose(); |
| 168 | + new GUI(); |
| 169 | + } |
| 170 | + }); |
| 171 | + //add components |
| 172 | + panel.add(tries); |
| 173 | + panel.add(wordsLeft); |
| 174 | + panel.add(possibleWords); |
| 175 | + panel.add(help); |
| 176 | + panel.add(reset); |
| 177 | + for(int i=0;i<6;i++) |
| 178 | + for(int j=0;j<5;j++) |
| 179 | + { |
| 180 | + panel.add(board[i][j]); |
| 181 | + board[i][j].setBounds(j*50+10,i*50+10,50,50); |
| 182 | + board[i][j].setHorizontalAlignment(JLabel.CENTER); |
| 183 | + board[i][j].setBackground(unset); |
| 184 | + board[i][j].setForeground(Color.WHITE); |
| 185 | + board[i][j].setOpaque(true); |
| 186 | + } |
| 187 | + //show magic |
| 188 | + startup.dispose(); |
| 189 | + setVisible(true); |
| 190 | + startupComplete=true; |
| 191 | + } |
| 192 | + |
| 193 | + |
| 194 | + public static void main(String[] args) |
| 195 | + { |
| 196 | + new GUI().setVisible(true); |
| 197 | + } |
| 198 | +} |
0 commit comments