summaryrefslogtreecommitdiffstats
path: root/src/se
diff options
context:
space:
mode:
Diffstat (limited to 'src/se')
-rw-r--r--src/se/liu/gusso230/tetris/BoardTester.java24
-rw-r--r--src/se/liu/gusso230/tetris/TetrisComponent.java51
-rw-r--r--src/se/liu/gusso230/tetris/TetrisViewer.java15
-rw-r--r--src/se/liu/gusso230/tetris/TetrisViewerOld.java32
4 files changed, 106 insertions, 16 deletions
diff --git a/src/se/liu/gusso230/tetris/BoardTester.java b/src/se/liu/gusso230/tetris/BoardTester.java
index 5f37036..42f2d01 100644
--- a/src/se/liu/gusso230/tetris/BoardTester.java
+++ b/src/se/liu/gusso230/tetris/BoardTester.java
@@ -1,24 +1,28 @@
package se.liu.gusso230.tetris;
+import javax.swing.*;
+import java.awt.event.ActionEvent;
+
public class BoardTester {
public static void main(String[] args) {
Board board = new Board(5, 5);
- board.randomize();
-
board.setFalling(new TetrominoMaker().getPoly(0));
board.setFallingX(0);
board.setFallingY(0);
- BoardToTextConverter converter = new BoardToTextConverter();
-
- System.out.println(converter.toText(board));
- System.out.println("-".repeat(board.getWidth()));
- board.getFalling().rotateClockwise();
- System.out.println(converter.toText(board));
- System.out.println("-".repeat(board.getWidth()));
-
TetrisViewer viewer = new TetrisViewer(board);
viewer.show();
+
+ final Action doOneStep = new AbstractAction() {
+ @Override public void actionPerformed(final ActionEvent actionEvent) {
+ board.randomize();
+ viewer.update();
+ }
+ };
+
+ final Timer clockTimer = new Timer(1000, doOneStep);
+ clockTimer.setCoalesce(true);
+ clockTimer.start();
}
}
diff --git a/src/se/liu/gusso230/tetris/TetrisComponent.java b/src/se/liu/gusso230/tetris/TetrisComponent.java
new file mode 100644
index 0000000..8fa1ed8
--- /dev/null
+++ b/src/se/liu/gusso230/tetris/TetrisComponent.java
@@ -0,0 +1,51 @@
+package se.liu.gusso230.tetris;
+
+import javax.swing.*;
+import java.awt.*;
+import java.util.EnumMap;
+import java.util.Map;
+
+public class TetrisComponent extends JComponent {
+ private static final int TILE_SIZE = 28;
+ private static final int TILE_GAP = 4;
+ private static final EnumMap<SquareType, Color> squareTypeColors = getDefaultColors();
+
+ private Board board;
+
+ public TetrisComponent(final Board board) {
+ this.board = board;
+ }
+
+ @Override protected void paintComponent(final Graphics g) {
+ super.paintComponent(g);
+ final Graphics2D g2d = (Graphics2D) g;
+
+ for (int y = 0; y < board.getHeight(); y++) {
+ for (int x = 0; x < board.getHeight(); x++) {
+ g.setColor(getColor(x, y));
+ g.fillRect(x * (TILE_SIZE + TILE_GAP), y * (TILE_SIZE + TILE_GAP), TILE_SIZE, TILE_SIZE);
+ }
+ }
+ }
+
+ public Color getColor(int x, int y) {
+ return squareTypeColors.get(board.getSquareAt(x, y));
+ }
+
+ public Dimension getPreferredSize() {
+ return new Dimension(board.getWidth(), board.getHeight());
+ }
+
+ public static EnumMap<SquareType, Color> getDefaultColors() {
+ EnumMap<SquareType, Color> map = new EnumMap<>(SquareType.class);
+ map.put(SquareType.EMPTY, Color.WHITE);
+ map.put(SquareType.I, Color.BLUE);
+ map.put(SquareType.O, Color.GREEN);
+ map.put(SquareType.T, Color.CYAN);
+ map.put(SquareType.S, Color.RED);
+ map.put(SquareType.Z, Color.ORANGE);
+ map.put(SquareType.J, Color.YELLOW);
+ map.put(SquareType.L, Color.PINK);
+ return map;
+ }
+} \ No newline at end of file
diff --git a/src/se/liu/gusso230/tetris/TetrisViewer.java b/src/se/liu/gusso230/tetris/TetrisViewer.java
index d98298f..a386ee1 100644
--- a/src/se/liu/gusso230/tetris/TetrisViewer.java
+++ b/src/se/liu/gusso230/tetris/TetrisViewer.java
@@ -4,20 +4,23 @@ import javax.swing.*;
import java.awt.*;
public class TetrisViewer {
- private Board board;
+ private TetrisComponent tetrisComponent;
public TetrisViewer(Board board) {
- this.board = board;
+ tetrisComponent = new TetrisComponent(board);
}
public void show() {
JFrame frame = new JFrame("Tetris");
- JTextArea gameTextArea = new JTextArea(board.getHeight(), board.getWidth());
- gameTextArea.setText(new BoardToTextConverter().toText(board));
frame.setLayout(new BorderLayout());
- frame.add(gameTextArea, BorderLayout.CENTER);
- gameTextArea.setFont(new Font("Monospaced", Font.PLAIN, 20));
+ frame.add(tetrisComponent, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
+
+ update();
+ }
+
+ public void update() {
+ tetrisComponent.repaint();
}
}
diff --git a/src/se/liu/gusso230/tetris/TetrisViewerOld.java b/src/se/liu/gusso230/tetris/TetrisViewerOld.java
new file mode 100644
index 0000000..5b0966b
--- /dev/null
+++ b/src/se/liu/gusso230/tetris/TetrisViewerOld.java
@@ -0,0 +1,32 @@
+package se.liu.gusso230.tetris;
+
+import javax.swing.*;
+import java.awt.*;
+
+public class TetrisViewerOld {
+ private Board board;
+ private BoardToTextConverter boardConverter;
+
+ private JTextArea gameTextArea;
+
+ public TetrisViewerOld(Board board, BoardToTextConverter boardConverter) {
+ this.board = board;
+ this.boardConverter = boardConverter;
+ gameTextArea = new JTextArea(board.getHeight(), board.getWidth());
+ gameTextArea.setFont(new Font("Monospaced", Font.PLAIN, 20));
+ }
+
+ public void show() {
+ JFrame frame = new JFrame("Tetris");
+ frame.setLayout(new BorderLayout());
+ frame.add(gameTextArea, BorderLayout.CENTER);
+ frame.pack();
+ frame.setVisible(true);
+
+ update();
+ }
+
+ public void update() {
+ gameTextArea.setText(boardConverter.toText(board));
+ }
+}