forked from IF-LK-2020/wordle
letter rendering
This commit is contained in:
parent
c56b0d755e
commit
755f2d5a9d
57
src/GUI.java
57
src/GUI.java
|
@ -1,7 +1,10 @@
|
|||
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;
|
||||
|
@ -13,6 +16,9 @@ public class GUI extends JPanel {
|
|||
|
||||
private Letter[][] letters;
|
||||
|
||||
private boolean running;
|
||||
private float marginRatio = 0.2f;
|
||||
|
||||
public GUI() {
|
||||
frame = new JFrame();
|
||||
frame.setSize(1000, 800);
|
||||
|
@ -28,13 +34,60 @@ public class GUI extends JPanel {
|
|||
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.PLAIN, 80));
|
||||
g2d.drawString("test", 100, 100);
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,19 +1,36 @@
|
|||
import java.awt.Color;
|
||||
|
||||
public class Letter {
|
||||
|
||||
public static byte WHITE = -1;
|
||||
public static byte GRAY = 0;
|
||||
public static byte YELLOW = 1;
|
||||
public static byte GREEN = 2;
|
||||
public static final byte WHITE = -1;
|
||||
public static final byte GRAY = 0;
|
||||
public static final byte YELLOW = 1;
|
||||
public static final byte GREEN = 2;
|
||||
|
||||
private char letter;
|
||||
private byte color;
|
||||
|
||||
public Letter() {
|
||||
letter = 'A';
|
||||
color = (byte) ((byte) (Math.random() * 4) - 1);
|
||||
}
|
||||
|
||||
public void setColor(byte color) {
|
||||
this.color = color;
|
||||
}
|
||||
|
||||
public byte getColor() {
|
||||
return color;
|
||||
public Color getColor() {
|
||||
switch (color) {
|
||||
case WHITE:
|
||||
return new Color(200, 200, 200);
|
||||
case GRAY:
|
||||
return new Color(100, 100, 100);
|
||||
case YELLOW:
|
||||
return new Color(220, 220, 0);
|
||||
case GREEN:
|
||||
return new Color(0, 220, 0);
|
||||
}
|
||||
return Color.BLACK;
|
||||
}
|
||||
|
||||
public void setLetter(char letter) {
|
||||
|
|
Loading…
Reference in New Issue