clean: improve code structure

This commit is contained in:
Martin Prokoph
2025-09-13 17:18:06 +02:00
parent f881427a67
commit b17e88f96d
3 changed files with 46 additions and 33 deletions

View File

@@ -0,0 +1,22 @@
package eu.midnightdust.yaytris;
import javax.swing.UIManager;
public class Main {
/**
* The application's entry point.
* Initializes the UI library to match the system style.
* Also loads saved settings, translations, and highscores from JSON.
*
* @param args command line arguments will be ignored
*/
public static void main(String[] args) {
try {
System.setProperty("java.awt.headless", "false");
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception | Error e) { System.out.printf("%s: %s\n", "Error setting system look and feel", e); }
Tetris.init();
}
}

View File

@@ -1,12 +1,12 @@
package eu.midnightdust.yaytris; package eu.midnightdust.yaytris;
import eu.midnightdust.yaytris.game.Space; import eu.midnightdust.yaytris.game.Space;
import eu.midnightdust.yaytris.game.Tetromino;
import eu.midnightdust.yaytris.ui.ScoreMenu; import eu.midnightdust.yaytris.ui.ScoreMenu;
import eu.midnightdust.yaytris.ui.TetrisUI; import eu.midnightdust.yaytris.ui.TetrisUI;
import eu.midnightdust.yaytris.util.GravityTimerTask; import eu.midnightdust.yaytris.util.GravityTimerTask;
import eu.midnightdust.yaytris.util.sound.SoundUtil; import eu.midnightdust.yaytris.util.sound.SoundUtil;
import javax.swing.*;
import java.time.LocalTime; import java.time.LocalTime;
import java.util.Random; import java.util.Random;
import java.util.Timer; import java.util.Timer;
@@ -20,24 +20,13 @@ public class Tetris {
private static TimerTask gravityTask; private static TimerTask gravityTask;
private static LocalTime startTime; private static LocalTime startTime;
/** public static void init() {
* The application's entry point.
* Initializes the UI library to match the system style.
* Also loads saved settings, translations, and highscores from JSON.
*
* @param args command line arguments will be ignored
*/
public static void main(String[] args) {
try {
System.setProperty("java.awt.headless", "false");
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception | Error e) { System.out.printf("%s: %s\n", "Error setting system look and feel", e); }
Settings.load(); Settings.load();
Translation.load(Settings.language.locale); Translation.load(Settings.language.locale);
HighScores.load(); HighScores.load();
timer = new Timer("Tetris falling pieces"); Tetris.timer = new Timer("Tetris falling pieces");
space = new Space(); Tetris.space = new Space();
ui = new TetrisUI(); Tetris.ui = new TetrisUI();
} }
/** /**
@@ -72,7 +61,7 @@ public class Tetris {
/** /**
* Starts a new game of Tetris :D * Starts a new game of Tetris :D
* This involves starting our gravity task, playing music and spawning the first {@link eu.midnightdust.yaytris.game.Tetromino} * This involves starting our gravity task, playing music and spawning the first {@link Tetromino}
* *
* @see Space#spawnTetromino() * @see Space#spawnTetromino()
*/ */
@@ -97,7 +86,8 @@ public class Tetris {
timer.purge(); timer.purge();
if (ui.getMenuPanel() instanceof ScoreMenu) ((ScoreMenu) ui.getMenuPanel()).gameOver(); if (ui.getMenuPanel() instanceof ScoreMenu) ((ScoreMenu) ui.getMenuPanel()).gameOver();
ui.transferFocus(); ui.transferFocus();
if (HighScores.diffToMap(Settings.difficulty).values().stream().noneMatch(hs -> hs > space.getScore())) ui.showHighscoreDialog(space.getScore()); if (HighScores.diffToMap(Settings.difficulty).values().stream().noneMatch(hs -> hs > space.getScore()))
ui.showHighscoreDialog(space.getScore());
} }
/** /**
@@ -110,6 +100,7 @@ public class Tetris {
if (ui.getMenuPanel() instanceof ScoreMenu) ((ScoreMenu) ui.getMenuPanel()).updateScore(score); if (ui.getMenuPanel() instanceof ScoreMenu) ((ScoreMenu) ui.getMenuPanel()).updateScore(score);
updateLevel(score); updateLevel(score);
} }
/** /**
* Updates the elapsed time * Updates the elapsed time
* *
@@ -118,6 +109,7 @@ public class Tetris {
public static void updateTime() { public static void updateTime() {
if (ui.getMenuPanel() instanceof ScoreMenu) ((ScoreMenu) ui.getMenuPanel()).updateTime(startTime); if (ui.getMenuPanel() instanceof ScoreMenu) ((ScoreMenu) ui.getMenuPanel()).updateTime(startTime);
} }
/** /**
* Updates the displayed level * Updates the displayed level
* *
@@ -136,5 +128,4 @@ public class Tetris {
if (ui.getMenuPanel() instanceof ScoreMenu) ((ScoreMenu) ui.getMenuPanel()).updateLevel(newLevel); if (ui.getMenuPanel() instanceof ScoreMenu) ((ScoreMenu) ui.getMenuPanel()).updateLevel(newLevel);
} }
} }
} }

View File

@@ -64,7 +64,7 @@ public class TetrisUI extends JFrame implements KeyListener {
private void rescale() { private void rescale() {
this.setSize((int) (400 * guiScale), (int) (320 * guiScale)); this.setSize((int) (400 * guiScale), (int) (320 * guiScale));
titleLabel.setBounds(scale(225), scale(7), scale(110), scale(30)); titleLabel.setBounds(scale(225), scale(7), scale(110), scale(30));
titleLabel.setIcon(new ImageIcon(new ImageIcon(titleImage).getImage().getScaledInstance(scale(110), scale(30), Image.SCALE_DEFAULT))); titleLabel.setIcon(new ImageIcon(titleImage.getScaledInstance(scale(110), scale(30), Image.SCALE_DEFAULT)));
gamePanel.setBounds(scale(10), scale(10), scale(150), scale(282)); gamePanel.setBounds(scale(10), scale(10), scale(150), scale(282));
this.setLocationRelativeTo(null); this.setLocationRelativeTo(null);
} }
@@ -165,7 +165,7 @@ public class TetrisUI extends JFrame implements KeyListener {
*/ */
public void showHighscoreDialog(int score) { public void showHighscoreDialog(int score) {
String playerName = JOptionPane.showInputDialog(null, t("dialog.highscore.action"), t("dialog.highscore.title"), JOptionPane.PLAIN_MESSAGE); String playerName = JOptionPane.showInputDialog(null, t("dialog.highscore.action"), t("dialog.highscore.title"), JOptionPane.PLAIN_MESSAGE);
HighScores.addScore(playerName, score); if (playerName != null) HighScores.addScore(playerName, score);
} }
/** /**