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 12 of 12

Thread: JAVA Applet Timer

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

    Default JAVA Applet Timer

    Hello! I'd like to start off with saying I am a real beginner with JAVA. I am creating a mouse-click game where you need to click the button as many times as you can within 15 seconds. I have the button and the score working, all I need is a timer that can count down from 15. I know there are swing timers and there are util timers and it would be prefered if I could use a util timer. Here is the code i have so far:

    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    import java.util.Timer;
    import java.util.TimerTask;

    public class MouseCickGame extends Applet implements ActionListener
    {
    int counter = 0;
    Button Click = new Button ("Click Me!");
    Label Score = new Label (" Score: ");
    Label Timer = new Label ();

    public void init ()
    {
    GridLayout grid = new GridLayout (3, 3);
    setLayout (grid);
    add (Click);
    Click.addActionListener (this);
    add (Score);
    add (Timer);
    }


    public void actionPerformed (ActionEvent ae)
    {
    if (ae.getSource () == Click)
    {
    counter += 1;
    }


    Score.setText (" Score: " + counter);
    }
    }

    I need to know how I can implement a timer into this code then have the button dissapear and the score displayed somewhere on the applet. Thanks!


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,301
    My Mood
    Hungover
    Thanks
    142
    Thanked 620 Times in 531 Posts

    Default Re: JAVA Applet Timer

    Why is it preferred to use a util Timer instead of a Swing Timer? Why are you using AWT over Swing in the first place?

    But if you insist, the API is your best friend: Timer (Java Platform SE 6)
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. The Following User Says Thank You to KevinWorkman For This Useful Post:

    iMofro (May 31st, 2012)

  4. #3
    Junior Member
    Join Date
    May 2012
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: JAVA Applet Timer

    Well I preferred the util timer mostly because i figured it would be easier for me to comprehend, but if you say the swing timer is more efficient and easier to use than i suppose that would be another possibility. Thanks for your help!

  5. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,301
    My Mood
    Hungover
    Thanks
    142
    Thanked 620 Times in 531 Posts

    Default Re: JAVA Applet Timer

    They are probably similar in efficiency. I personally like Swing Timers, but it's really up to you. But either way, I wouldn't impose rules like "I have to use this instead of this" before you understand both things.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #5
    Junior Member
    Join Date
    May 2012
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: JAVA Applet Timer

    Basically the intention of this timer is to make a game where you click the button however many times you can within 15 seconds, after the 15 seconds the button is supposed to disappear. Could you possible help me with the coding because I seem to be having issues with the timer.

  7. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,301
    My Mood
    Hungover
    Thanks
    142
    Thanked 620 Times in 531 Posts

    Default Re: JAVA Applet Timer

    Quote Originally Posted by iMofro View Post
    Basically the intention of this timer is to make a game where you click the button however many times you can within 15 seconds, after the 15 seconds the button is supposed to disappear. Could you possible help me with the coding because I seem to be having issues with the timer.
    What issues are you experiencing?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  8. #7
    Junior Member
    Join Date
    May 2012
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: JAVA Applet Timer

    Okay I have a new update. I have a timer working but now the game itself is not showing. All the buttons and labels I am attempting to add are not showing. Here is my code:

    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
     
    public class TimerDisplay extends JLabel implements ActionListener
    {
        GridLayout grid = new GridLayout (3, 3);
        int count = 14;
        int counter = 0;
        JButton Click = new JButton ("Click Me!");
        JLabel Score = new JLabel ("                                         Score: ");
        JPanel p = new JPanel ();
        public TimerDisplay ()
        {
            super ("Remaining Time: 15 secs", JLabel.CENTER);
            p.setLayout (grid);
            p.add (Click);
            Click.addActionListener (this);
            p.add (Score);
            Timer t = new Timer (1000, this);
            t.start ();
            this.add (p);
            p.setVisible (true);
            Click.setVisible(true);
            Score.setVisible(true);
        }
     
     
        public void actionPerformed (ActionEvent ae)
        {
            this.setText ("Remaining Time: " + count-- + " secs");
            if (ae.getSource () == Click)
            {
                counter += 1;
            }
            if (count == -1)
            {
                count = 0;
                remove (Click);
            }
            Score.setText ("                                         Score: " + counter);
        }
     
     
        public static void main (String arg[])
        {
            JFrame f = new JFrame ();
            f.setSize (500, 500);
            f.addWindowListener (new WindowAdapter ()
            {
                public void windowClosing (WindowEvent e)
                {
                    System.exit (0);
                }
            }
            );
            TimerDisplay c = new TimerDisplay ();
            f.getContentPane ()
                .setLayout (new BorderLayout ());
            f.getContentPane ()
                .add (c, BorderLayout.CENTER);
            f.show ();
        }
    }

  9. #8
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,301
    My Mood
    Hungover
    Thanks
    142
    Thanked 620 Times in 531 Posts

    Default Re: JAVA Applet Timer

    Why do you extend JLabel? I see you adding Components to a JPanel, but when do you add that JPanel to the JFrame?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  10. #9
    Junior Member
    Join Date
    May 2012
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: JAVA Applet Timer

    Iv tried extending JFrame but it always screws up the timer. As far as im aware of, code "this.add (p)" should add the panel to the frame shouldnt it? What would you recommend I alter to the code?

  11. #10
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,301
    My Mood
    Hungover
    Thanks
    142
    Thanked 620 Times in 531 Posts

    Default Re: JAVA Applet Timer

    Quote Originally Posted by iMofro View Post
    Iv tried extending JFrame but it always screws up the timer. As far as im aware of, code "this.add (p)" should add the panel to the frame shouldnt it? What would you recommend I alter to the code?
    No, this.add(p) is going to add the panel to whatever you've extended, which in this case is a JLabel, which doesn't make any sense.

    I don't think you need to extend anything, actually.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  12. #11
    Junior Member
    Join Date
    May 2012
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: JAVA Applet Timer

    But if im not extending anything what am i adding tha panel to? What is the alternate code to add the panel if im not extending anything? Would it make sense to at least extend JFrame? The only reason im extending JLabel is because thats what the timer is running off of.

  13. #12
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,301
    My Mood
    Hungover
    Thanks
    142
    Thanked 620 Times in 531 Posts

    Default Re: JAVA Applet Timer

    You don't need to extend anything, including JFrame.

    You need to create a JFrame, then add a JPanel to it (or use its existing content pane), then add a JLabel to that JPanel. You then need to set up a Timer and an ActionListener that sets the text of the JLabel upon firing. The Timer doesn't run off of any Component, and you're not changing the default behavior of any Components, so you don't need to extend anything.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. How to Use Timer in Java
    By neo_2010 in forum Java SE API Tutorials
    Replies: 2
    Last Post: August 6th, 2013, 09:49 AM
  2. Get Java input on a timer
    By johnsmith1992 in forum Threads
    Replies: 6
    Last Post: December 19th, 2011, 06:09 PM
  3. How to convert thread.sleep() into a timer in an Applet
    By all_pro in forum Java Theory & Questions
    Replies: 2
    Last Post: April 14th, 2011, 07:45 AM
  4. How to Use Timer in Java
    By neo_2010 in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: September 1st, 2009, 12:10 PM
  5. How to Use Timer in Java
    By neo_2010 in forum Java Programming Tutorials
    Replies: 0
    Last Post: September 1st, 2009, 12:10 PM