From 560846a61718210c95397fde8807bff370914a9f Mon Sep 17 00:00:00 2001 From: hemantsharma98 Date: Fri, 30 Apr 2021 00:36:29 +0530 Subject: [PATCH 1/2] function for timmer --- src/main/java/game/Board.java | 78 +++++++++++++++++++++++++++++++++-- 1 file changed, 74 insertions(+), 4 deletions(-) diff --git a/src/main/java/game/Board.java b/src/main/java/game/Board.java index 513a720..76b27c9 100644 --- a/src/main/java/game/Board.java +++ b/src/main/java/game/Board.java @@ -6,7 +6,10 @@ package game; import java.awt.Color; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; import java.io.IOException; +import java.text.DecimalFormat; import java.util.ArrayList; import java.util.Arrays; import javax.swing.*; @@ -31,6 +34,18 @@ public class Board extends JPanel { Field collisionField; private boolean isSelected = false; + + JFrame movesFrame = new JFrame("No. of Moves"); /**(); @@ -151,6 +166,9 @@ private class BoardListener implements java.awt.event.ActionListener { /* * Executed when a field is pressed */ + int noOfMovesBlack = 0; /** Date: Fri, 30 Apr 2021 00:40:08 +0530 Subject: [PATCH 2/2] timer added --- src/main/java/game/Board.java | 21 ++++++++++++++++- src/main/java/game/Main.java | 44 +++++++---------------------------- 2 files changed, 28 insertions(+), 37 deletions(-) diff --git a/src/main/java/game/Board.java b/src/main/java/game/Board.java index 76b27c9..b84b5e1 100644 --- a/src/main/java/game/Board.java +++ b/src/main/java/game/Board.java @@ -416,7 +416,26 @@ public void actionPerformed(ActionEvent e) { } private void timer(){ - + timer = new Timer(1000,new ActionListener(){ + @Override + public void actionPerformed(ActionEvent e) { + second--; + if(second==-1){ + second=59; + minute--; + timeLabel.setText(dfminutes+":"+dfseconds); + } + else{ + timeLabel.setText(dfminutes+":"+dfseconds); + } + if(minute==0 && second==0){ + timer.stop(); + System.out.println("Time out!! Game Over."); + System.exit(0); + } + } + + }); } diff --git a/src/main/java/game/Main.java b/src/main/java/game/Main.java index 33c7654..6e0f694 100644 --- a/src/main/java/game/Main.java +++ b/src/main/java/game/Main.java @@ -1,3 +1,4 @@ + /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates @@ -5,48 +6,25 @@ */ package game; -import java.awt.BorderLayout; import java.awt.EventQueue; import java.io.IOException; import java.util.logging.Level; import java.util.logging.Logger; -import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; -import javax.swing.JOptionPane; -import javax.swing.JPanel; /** * * @author gersc */ public class Main extends JFrame { - public Main() { - /// \ref T7_1 Popup will ask users to choose the number of players - int playerChoice = getNumberOfPlayers(); - - if(playerChoice == 1) { - startOnePlayer(); - } - else { - startTwoPlayer(); - } - - } - - - public void startOnePlayer() { - System.out.println("TODO"); - } - - - public void startTwoPlayer() { try { this.add( new Board()); } catch (IOException ex) { Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); } + //this.setSize(10000,10000); this.pack(); this.setResizable(false); this.setTitle( "CHESS" ); @@ -54,23 +32,17 @@ public void startTwoPlayer() { this.setLocationRelativeTo( null ); } - /// \ref T7_2 Popup for users to choose number of players - //will return 1 for One player, 2 for Two player - public int getNumberOfPlayers() { - String[] options = {"1 Player", "2 Players"}; - int playerChoice = JOptionPane.showOptionDialog(null, "Please choose number of Players", - "How Many Players?", - JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null, options, options[0]); - return playerChoice + 1; + public void newGame() throws Exception{ + Main main = new Main(); + main.setVisible(true); } - + public static void main(String[] args) { - EventQueue.invokeLater(() -> { Main main = new Main(); - main.setVisible( true ); + main.setVisible(true); }); } - + }