commit 1f2408de36af3de4fd72e5887f6a1a311068bf08
parent a0b1d4fbb4eb9c91a50ebdd263c3592920608336
Author: adglas21 <adamglasheen99@gmail.com>
Date: Tue, 12 Nov 2024 12:08:46 +0000
implemented timer
Diffstat:
1 file changed, 51 insertions(+), 3 deletions(-)
diff --git a/TicTacToeClient/src/main/java/com/groupproject/tictactoeclient/ContentPanes/MainContentPanel.java b/TicTacToeClient/src/main/java/com/groupproject/tictactoeclient/ContentPanes/MainContentPanel.java
@@ -13,17 +13,26 @@ import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SpringLayout;
+import javax.swing.Timer;
/**
*
- * @author William
+ * @author William and Adam
* Contains the content visible in the main program.
*/
+
+
public class MainContentPanel extends CustomPanel {
+
TicTacToeClient client;
JList<String> openGamesList;
+ private JLabel gameTimerLabel;
+ private Timer timer;
+ private int remainingTime;
+
+
public MainContentPanel(TicTacToeClient client) {
this.client = client;
@@ -40,12 +49,12 @@ public class MainContentPanel extends CustomPanel {
JScrollPane openGamesScrollPane = new JScrollPane(openGamesList);
TicTacToeGrid grid = new TicTacToeGrid();
JLabel gameStatusLabel = new JLabel();
- JLabel gameTimerLabel = new JLabel();
+ gameTimerLabel = new JLabel();
JButton viewScoreButton = new JButton("View Personal Score");
// set mock data for labels:
gameStatusLabel.setText("Opponents turn");
- gameTimerLabel.setText("12m 32s");
+ gameTimerLabel.setText("15m 0s");
// Add buttons to panel
@@ -100,6 +109,9 @@ public class MainContentPanel extends CustomPanel {
@Override
public void actionPerformed(ActionEvent e) {
client.proxy.newGame(Integer.parseInt(client.UID));
+
+ //start countdown when create game
+ startTimer();
}
});
}
@@ -113,4 +125,40 @@ public class MainContentPanel extends CustomPanel {
this.revalidate();
this.repaint();
}
+
+
+ private void startTimer()
+ {
+ //remaining time is 15 minutes
+ remainingTime = 1 * 60;
+
+ //create timer that updates every 1 seconds i.e countdown clock
+ timer = new Timer(1000, new ActionListener(){
+ @Override
+ public void actionPerformed(ActionEvent e) {
+
+ //countdown time
+ remainingTime--;
+
+ int minutes = remainingTime / 60;
+ int seconds = remainingTime % 60;
+
+ String time = String.format("%02d:%02d", minutes, seconds);
+
+ gameTimerLabel.setText(time);
+
+ //need to implement when no one else joins the game
+ //and the countdown is finished that the game is then deleted
+ //at the moment it just prints game finished
+ if(remainingTime <= 0) {
+ timer.stop();
+ gameTimerLabel.setText("Finished");
+ }
+ }
+ });
+ timer.start();
+ }
+
+
+
}
\ No newline at end of file