wordle/src/GUI.java

94 lines
2.4 KiB
Java
Raw Normal View History

2022-02-12 21:01:25 +01:00
import java.awt.BasicStroke;
import java.awt.Color;
2022-02-11 20:43:46 +01:00
import java.awt.Font;
2022-02-10 11:35:20 +01:00
import java.awt.Graphics;
2022-02-11 20:43:46 +01:00
import java.awt.Graphics2D;
import java.awt.RenderingHints;
2022-02-12 21:01:25 +01:00
import java.util.Iterator;
2022-02-10 11:12:17 +01:00
2022-02-10 11:35:20 +01:00
import javax.swing.JFrame;
import javax.swing.JPanel;
2022-02-10 11:12:17 +01:00
2022-02-10 11:35:20 +01:00
public class GUI extends JPanel {
2022-02-10 11:12:17 +01:00
2022-02-10 11:35:20 +01:00
private static final long serialVersionUID = 1L;
private JFrame frame;
private Letter[][] letters;
2022-02-12 21:01:25 +01:00
private boolean running;
private float marginRatio = 0.2f;
2022-02-10 11:35:20 +01:00
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();
}
}
2022-02-12 21:01:25 +01:00
new Thread(new Runnable() {
@Override
public void run() {
running = true;
while (running) {
repaint();
try {
Thread.sleep(16);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
2022-02-10 11:35:20 +01:00
}
@Override
protected void paintComponent(Graphics g) {
2022-02-11 20:43:46 +01:00
Graphics2D g2d = (Graphics2D) g;
2022-02-12 21:01:25 +01:00
if (letters == null) {
return;
}
2022-02-11 20:43:46 +01:00
g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
2022-02-12 21:01:25 +01:00
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);
}
}
2022-02-10 11:35:20 +01:00
}
2022-02-10 11:12:17 +01:00
}