From ad329447a9e199ee641575ea5126abd8b1ac98d1 Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Fri, 26 Jun 2020 14:54:31 +0000 Subject: [PATCH] Fix missing ImageIcon for PlayButton and repeated EndScreen Spawning (#3) * Fix multiple EndScreen Instances spawning * Removed `Play Again` icon Co-authored-by: delvh --- src/main/dev/lh/ui/Endscreen.java | 8 -------- src/main/dev/lh/ui/GameWindow.java | 9 +++++++-- src/main/dev/lh/ui/StartScreen.java | 8 +++++--- 3 files changed, 12 insertions(+), 13 deletions(-) diff --git a/src/main/dev/lh/ui/Endscreen.java b/src/main/dev/lh/ui/Endscreen.java index e489274..238765e 100644 --- a/src/main/dev/lh/ui/Endscreen.java +++ b/src/main/dev/lh/ui/Endscreen.java @@ -40,14 +40,6 @@ public class Endscreen extends JDialog { contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5)); contentPanel.setLayout(new BorderLayout(0, 0)); getContentPane().add(contentPanel, BorderLayout.CENTER); - addWindowListener(new java.awt.event.WindowAdapter() { - - @Override - public void windowClosing(java.awt.event.WindowEvent windowEvent) { - Thread.getAllStackTraces().forEach((thread, stackTraceElement) -> thread.interrupt()); - System.exit(0); - } - }); JButton btnNewButton = new JButton("Play again"); btnNewButton.setMnemonic(KeyEvent.VK_ENTER); diff --git a/src/main/dev/lh/ui/GameWindow.java b/src/main/dev/lh/ui/GameWindow.java index 294423f..484043f 100755 --- a/src/main/dev/lh/ui/GameWindow.java +++ b/src/main/dev/lh/ui/GameWindow.java @@ -25,6 +25,7 @@ public class GameWindow extends JFrame { private static final long serialVersionUID = 1L; private Snake s = new Snake(7); private FoodFactory foodFactory = FoodFactory.getInstance(); + private Timer timer; /** * @param title the title of the frame @@ -84,7 +85,8 @@ public class GameWindow extends JFrame { } }); - Timer timer = new Timer(50, + timer = new Timer( + 50, evt -> { s.nextFrame(); if (System.currentTimeMillis() >= foodFactory.getTimeOfNextFood()) newFood(); repaint(); }); timer.start(); @@ -107,5 +109,8 @@ public class GameWindow extends JFrame { * * @since Snake 1.1 */ - public void close() { dispose(); } + public void close() { + timer.stop(); + dispose(); + } } diff --git a/src/main/dev/lh/ui/StartScreen.java b/src/main/dev/lh/ui/StartScreen.java index 2b9f374..81f3d6c 100755 --- a/src/main/dev/lh/ui/StartScreen.java +++ b/src/main/dev/lh/ui/StartScreen.java @@ -4,7 +4,9 @@ import java.awt.EventQueue; import java.awt.Font; import java.awt.event.KeyEvent; -import javax.swing.*; +import javax.swing.JButton; +import javax.swing.JFrame; +import javax.swing.JPanel; import javax.swing.border.EmptyBorder; import dev.lh.Main; @@ -59,10 +61,9 @@ public class StartScreen extends JFrame { JButton buPlay = new JButton("Start Game"); buPlay.setBounds(158, 197, 190, 131); - buPlay.setIcon(new ImageIcon(StartScreen.class.getResource("/com/sun/javafx/webkit/prism/resources/mediaPlayDisabled.png"))); + buPlay.setText("Play Again"); buPlay.setMnemonic(KeyEvent.VK_ENTER); buPlay.setFont(new Font("Times New Roman", Font.PLAIN, 16)); - buPlay.addActionListener(a -> { Main.startGame(); @@ -70,6 +71,7 @@ public class StartScreen extends JFrame { dispose(); System.gc(); }); + contentPane.add(buPlay); contentPane.setLayout(null); } catch (Exception e) {