Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: game timer

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default game timer

    i need a 120 seconds countdown......hi, just improved a simple word game, its just that we can't show its countdown timer,,,how can i have a game timer for this


    import java.awt.*;
    import javax.swing.*;

    public class TextTwist extends javax.swing.JFrame
    implements java.awt.event.ActionListener
    {

    // hard code, should be picked from a Problem class
    private String[] letters = {"U","Y","I","I","V","R","E","S","N","T"};

    // hard code, should be picked from a Problem class
    private String[] solutions = {"ITS", "NET", "NIT", "RUN", "SET", "SIN", "SIR", "SIT", "SUN",
    "TEN", "TER", "TIE", "TIN", "TIR", "TRY", "UNI", "URN", "USE", "VET", "VIN", "YEN", "YET",
    "NEST", "NETS", "NUTS", "REIN", "RENT", "REST", "RITE", "RUNS", "RUST", "SENT", "SINE",
    "SITE", "TENS", "TIES", "TIRE", "UNIT", "USER", "VEIN", "VENT", "VEST", "VETS", "VINE", "VISE",
    "ENTRY", "RENTS", "TIRES", "TRIES", "UNITE", "UNITY", "URINE", "VENTS", "VINES", "VIRUS", "VISIT",
    "INSERT", "INVERT", "INVEST", "INVITE", "UNITES", "VIRTUE", "UNIVERSITY"};

    // required designer variables
    private javax.swing.JButton[] letterButtons;
    private javax.swing.JButton enterButton;
    private javax.swing.JButton clearButton;
    private javax.swing.JLabel bufferLabel;
    private javax.swing.JLabel CountdownTimer;
    private javax.swing.JLabel[] solutionLabels;

    // these are the GUI components

    public TextTwist()
    {
    // needed for the Windows GUI
    initializeComponent();
    }


    private void CountdownTimer(){

    }

    private void initializeComponent()
    {
    this.letterButtons = new javax.swing.JButton[10];

    for (int i=0;i<this.letterButtons.length;i++)
    {
    this.letterButtons[i] = new javax.swing.JButton();
    }


    this.solutionLabels = new javax.swing.JLabel[solutions.length];
    for (int i=0;i<this.solutions.length;i++)
    {
    this.solutionLabels[i] = new javax.swing.JLabel();
    }

    this.setLayout(null);

    javax.swing.JButton button = null;
    for (int i=0;i<letterButtons.length;i++)
    {
    button = this.letterButtons[i];
    button.setBounds(500+80*i, 300, 65, 65);
    button.setActionCommand("letterButton"+i);
    button.setText(letters[i]);
    button.setFocusable(false);
    button.addActionListener(this);
    button.setFont(new Font("Arial Black", Font.BOLD, 30));
    button.setForeground(Color.black);
    button.setBackground(Color.pink);
    }

    //
    button = enterButton = new javax.swing.JButton();
    button.setBounds(690, 400, 100, 60);
    button.setActionCommand("enterButton");
    button.setText("Enter");
    button.setFocusable(false);
    button.addActionListener(this);
    enterButton.setFont(new Font("Arial Black", Font.PLAIN, 20));
    enterButton.setForeground(Color.PINK);
    enterButton.setBackground(Color.GRAY);

    button = clearButton = new javax.swing.JButton();
    button.setBounds(900, 400, 100, 60);
    button.setActionCommand("clearButton");
    button.setText("Clear");
    button.setFocusable(false);
    button.addActionListener(this);
    clearButton.setFont(new Font("Arial Black", Font.PLAIN, 20));
    clearButton.setForeground(Color.PINK);
    clearButton.setBackground(Color.GRAY);



    //
    // bufferLabel
    //
    javax.swing.JLabel label = null;
    label = this.bufferLabel = new javax.swing.JLabel();
    label.setBounds(750,60,600,150);
    label.setFont(new Font("Arial Black", Font.PLAIN, 40));
    label.setForeground(Color.black);
    label.setBackground(Color.GRAY);

    for (int i=0;i<solutionLabels.length;i++)
    {
    label = this.solutionLabels[i];
    int row=i%10;
    int column=i/10;
    label.setBounds(70+column*65, 50+row*65, 250, 25);
    label.setFont(new Font("calibri", Font.PLAIN, 15));
    label.setForeground(Color.BLACK);
    for (int letterCount=0;letterCount<solutions[i].length();letterCount++)
    {
    label.setText( label.getText() + "-" );
    label.setFont(new Font("Calibri",Font.ITALIC, 20));
    label.setForeground(Color.black);
    }
    }


    // basic Window Frame
    //
    this.setBounds(0, 0, 1000,1000);


    for (int i=0;i<letterButtons.length;i++)
    {
    this.add(this.letterButtons[i]);
    }
    this.add(this.enterButton);
    this.add(this.clearButton);
    this.add(this.bufferLabel);
    for (int i=0;i<solutionLabels.length;i++)
    {
    this.add(this.solutionLabels[i]);
    }



    this.setTitle("Text Twist");
    this.setDefaultCloseOperation(javax.swing.JFrame.E XIT_ON_CLOSE);
    this.setVisible(true);
    }

    public void actionPerformed(java.awt.event.ActionEvent ev)
    {
    String command = ev.getActionCommand();
    if (command.equals("clearButton"))
    {
    clearButton_click(clearButton, ev);
    }
    else if (command.equals("enterButton"))
    {
    enterButton_click(enterButton, ev);
    }
    else if (command.startsWith("letterButton"))
    {
    int i = Integer.parseInt(command.substring(command.length( )-1,command.length()));
    letterButton_click(letterButtons[i], ev);
    }

    }

    private void letterButton_click(Object sender, java.awt.event.ActionEvent ev)
    {
    javax.swing.JButton button = (javax.swing.JButton) sender;
    String letter = button.getText();
    button.setEnabled(false);
    bufferLabel.setText(bufferLabel.getText() + letter);
    }

    private void clearButton_click(Object sender, java.awt.event.ActionEvent ev)
    {
    clearBuffer();
    }

    private void clearBuffer()
    {
    for (int i=0;i<letterButtons.length;i++)
    {
    letterButtons[i].setEnabled(true);
    }
    bufferLabel.setText("");
    }

    private void enterButton_click(Object sender, java.awt.event.ActionEvent ev)
    {
    for (int i=0;i<solutions.length;i++)
    {
    if (bufferLabel.getText().equals(solutions[i]))
    {
    solutionLabels[i].setText(solutions[i]);
    clearBuffer();
    }
    }
    }


    public static void main(String[] args) throws Exception
    {
    new TextTwist();
    }

    } // class TextTwist
    Last edited by powder; October 21st, 2012 at 08:14 AM.


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: game timer

    Consider

    • Using a Swing Timer to do your counting for you. There's an excellent tutorial on its use which can be found here: How to Use Swing Timers
    • Using [code=java] [/code] tags around your code that you've posted in the forum so that it retains its formatting and is readable.


    Welcome to the javaprogrammingforums by the way!

  3. #3
    Junior Member
    Join Date
    Oct 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: game timer

    thank you,,,

  4. #4
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: game timer

    You're welcome. Consider also using the AWT/Swing layout managers to help you size and position your GUI and its components for you. It may easier to use null layout and absolute positioning via setBounds(...), but in the long run, it's actually much easier to use and especially maintain a Swing GUI if you use layouts. Also, this will allow your GUI to size itself correctly on different platforms (something that your current GUI does not do).

    Oh, an example of using code tags as I described above:

    import java.awt.*;
    import javax.swing.*;
     
    public class TextTwist extends javax.swing.JFrame
    implements java.awt.event.ActionListener
    {
     
       // hard code, should be picked from a Problem class
       private String[] letters = {"U","Y","I","I","V","R","E","S","N","T"};
     
       // hard code, should be picked from a Problem class
       private String[] solutions = {"ITS", "NET", "NIT", "RUN", "SET", "SIN", "SIR", "SIT", "SUN",
          "TEN", "TER", "TIE", "TIN", "TIR", "TRY", "UNI", "URN", "USE", "VET", "VIN", "YEN", "YET", 
          "NEST", "NETS", "NUTS", "REIN", "RENT", "REST", "RITE", "RUNS", "RUST", "SENT", "SINE",
          "SITE", "TENS", "TIES", "TIRE", "UNIT", "USER", "VEIN", "VENT", "VEST", "VETS", "VINE", "VISE",
          "ENTRY", "RENTS", "TIRES", "TRIES", "UNITE", "UNITY", "URINE", "VENTS", "VINES", "VIRUS", "VISIT",
          "INSERT", "INVERT", "INVEST", "INVITE", "UNITES", "VIRTUE", "UNIVERSITY"};
     
       // required designer variables
       private javax.swing.JButton[] letterButtons;
       private javax.swing.JButton enterButton;
       private javax.swing.JButton clearButton;
       private javax.swing.JLabel bufferLabel;
       private javax.swing.JLabel CountdownTimer;
       private javax.swing.JLabel[] solutionLabels;
     
       // these are the GUI components
     
       public TextTwist()
       {
          // needed for the Windows GUI
          initializeComponent();
       }
     
     
       private void CountdownTimer(){
     
       }
     
       private void initializeComponent()
       {
          this.letterButtons = new javax.swing.JButton[10];
     
          for (int i=0;i<this.letterButtons.length;i++)
          {
             this.letterButtons[i] = new javax.swing.JButton();
          }
     
     
          this.solutionLabels = new javax.swing.JLabel[solutions.length];
          for (int i=0;i<this.solutions.length;i++)
          {
             this.solutionLabels[i] = new javax.swing.JLabel();
          }
     
          this.setLayout(null);
     
          javax.swing.JButton button = null;
          for (int i=0;i<letterButtons.length;i++)
          {
             button = this.letterButtons[i];
             button.setBounds(500+80*i, 300, 65, 65);
             button.setActionCommand("letterButton"+i);
             button.setText(letters[i]);
             button.setFocusable(false);
             button.addActionListener(this);
             button.setFont(new Font("Arial Black", Font.BOLD, 30));
             button.setForeground(Color.black);
             button.setBackground(Color.pink);
          }
     
          // 
          button = enterButton = new javax.swing.JButton();
          button.setBounds(690, 400, 100, 60);
          button.setActionCommand("enterButton");
          button.setText("Enter");
          button.setFocusable(false);
          button.addActionListener(this);
          enterButton.setFont(new Font("Arial Black", Font.PLAIN, 20));
          enterButton.setForeground(Color.PINK);
          enterButton.setBackground(Color.GRAY);
     
          button = clearButton = new javax.swing.JButton();
          button.setBounds(900, 400, 100, 60);
          button.setActionCommand("clearButton");
          button.setText("Clear");
          button.setFocusable(false);
          button.addActionListener(this);
          clearButton.setFont(new Font("Arial Black", Font.PLAIN, 20));
          clearButton.setForeground(Color.PINK);
          clearButton.setBackground(Color.GRAY);
     
     
     
          // 
          // bufferLabel
          // 
          javax.swing.JLabel label = null;
          label = this.bufferLabel = new javax.swing.JLabel();
          label.setBounds(750,60,600,150);
          label.setFont(new Font("Arial Black", Font.PLAIN, 40));
          label.setForeground(Color.black);
          label.setBackground(Color.GRAY);
     
          for (int i=0;i<solutionLabels.length;i++)
          {
             label = this.solutionLabels[i];
             int row=i%10;
             int column=i/10;
             label.setBounds(70+column*65, 50+row*65, 250, 25);
             label.setFont(new Font("calibri", Font.PLAIN, 15));
             label.setForeground(Color.BLACK);
             for (int letterCount=0;letterCount<solutions[i].length();letterCount++)
             {
                label.setText( label.getText() + "-" );
                label.setFont(new Font("Calibri",Font.ITALIC, 20));
                label.setForeground(Color.black);
             }
          }
     
     
          // basic Window Frame
          // 
          this.setBounds(0, 0, 1000,1000);
     
     
          for (int i=0;i<letterButtons.length;i++)
          {
             this.add(this.letterButtons[i]);
          }
          this.add(this.enterButton);
          this.add(this.clearButton);
          this.add(this.bufferLabel);
          for (int i=0;i<solutionLabels.length;i++)
          {
             this.add(this.solutionLabels[i]);
          }
     
     
     
          this.setTitle("Text Twist");
          this.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
          this.setVisible(true);
       }
     
       public void actionPerformed(java.awt.event.ActionEvent ev)
       {
          String command = ev.getActionCommand();
          if (command.equals("clearButton"))
          {
             clearButton_click(clearButton, ev);
          }
          else if (command.equals("enterButton"))
          {
             enterButton_click(enterButton, ev);
          }
          else if (command.startsWith("letterButton"))
          {
             int i = Integer.parseInt(command.substring(command.length( )-1,command.length()));
             letterButton_click(letterButtons[i], ev);
          }
     
       }
     
       private void letterButton_click(Object sender, java.awt.event.ActionEvent ev)
       {
          javax.swing.JButton button = (javax.swing.JButton) sender;
          String letter = button.getText();
          button.setEnabled(false);
          bufferLabel.setText(bufferLabel.getText() + letter);
       }
     
       private void clearButton_click(Object sender, java.awt.event.ActionEvent ev)
       {
          clearBuffer();
       }
     
       private void clearBuffer()
       {
          for (int i=0;i<letterButtons.length;i++)
          {
             letterButtons[i].setEnabled(true);
          }
          bufferLabel.setText("");
       }
     
       private void enterButton_click(Object sender, java.awt.event.ActionEvent ev)
       {
          for (int i=0;i<solutions.length;i++)
          {
             if (bufferLabel.getText().equals(solutions[i]))
             {
                solutionLabels[i].setText(solutions[i]);
                clearBuffer();
             }
          }
       }
     
     
       public static void main(String[] args) throws Exception
       {
          new TextTwist();
       }
     
    } // class TextTwist

    Notice how much easier the posted code is when these are used.

    Best of luck!

Similar Threads

  1. How to put a timer?!?
    By mrprogrammer in forum What's Wrong With My Code?
    Replies: 7
    Last Post: August 21st, 2012, 10:44 AM
  2. how to set timer
    By jack_nutt in forum Java Theory & Questions
    Replies: 3
    Last Post: June 23rd, 2011, 08:55 PM
  3. MeteorDodger Game JPanel Help (Timer?)
    By bglueck in forum What's Wrong With My Code?
    Replies: 10
    Last Post: May 14th, 2011, 11:07 AM
  4. Timer Class help
    By Deadbob in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 22nd, 2010, 11:18 PM
  5. Timer?
    By TimW in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: October 27th, 2009, 07:43 AM