import java.awt.BasicStroke; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.RenderingHints; import java.util.Iterator; import javax.swing.JFrame; import javax.swing.JPanel; public class GUI extends JPanel { private static final long serialVersionUID = 1L; private JFrame frame; private Letter[][] letters; private boolean running; private float marginRatio = 0.2f; public GUI() { frame = new JFrame(); frame.setSize(1000, 800); frame.setTitle("Wordle"); frame.setVisible(true); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(this); letters = new Letter[5][6]; for (int x = 0; x < letters.length; x++) { for (int y = 0; y < letters[0].length; y++) { letters[x][y] = new Letter(); } } new Thread(new Runnable() { @Override public void run() { running = true; while (running) { repaint(); try { Thread.sleep(16); } catch (InterruptedException e) { e.printStackTrace(); } } } }).start(); } @Override protected void paintComponent(Graphics g) { Graphics2D g2d = (Graphics2D) g; if (letters == null) { return; } g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); g2d.setFont(new Font(null, Font.BOLD, 48)); g2d.setStroke(new BasicStroke(4)); int framew = getSize().width; int frameh = getSize().height; g2d.setColor(Color.DARK_GRAY); g2d.fillRect(0, 0, framew, frameh); int letterSize = frameh / 10; int margin = (int) (letterSize * marginRatio); int offsetX = framew / 2 - (5 * letterSize + 4 * margin) / 2; int offsetY = frameh / 2 - (6 * letterSize + 5 * margin) / 2; for (int x = 0; x < letters.length; x++) { for (int y = 0; y < letters[0].length; y++) { int posX = offsetX + x * (letterSize + margin); int posY = offsetY + y * (letterSize + margin); g2d.setColor(letters[x][y].getColor()); g2d.fillRect(posX, posY, letterSize, letterSize); g2d.setColor(Color.BLACK); g2d.drawRect(posX, posY, letterSize, letterSize); String letter = "" + letters[x][y].getLetter(); int letterWidth = g2d.getFontMetrics().stringWidth(letter); g2d.drawString(letter, posX + letterSize / 2 - letterWidth / 2, posY + letterSize / 2 + 20); } } } }