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

Thread: About swing Timer in my Math game

  1. #1
    Member
    Join Date
    Mar 2014
    Posts
    36
    My Mood
    Confused
    Thanks
    20
    Thanked 1 Time in 1 Post

    Default About swing Timer in my Math game

    I make math game which the question is randomed and, the player must input the correct answer. I make the game with JOptionpane and it works well. Now, I want to add timer in my game, I want to add the time limit when the player try to input the answer for the question at Input Dialog. I already browse the tutorial and some reference in my book about javax.swing.Timer. I just can't understand it. I need someone who can guide me.

    I just only need the time limit methodology only, and if the player can't input the answer before the time limit, the program won't end but the program will continue to next procedure.

    I'm sorry my english isn't good enough.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: About swing Timer in my Math game

    What did you find when you searched "joptionpane with timer" or similar?

    Do you have to use JOptionPane, or are you ready to advance to a JFrame with JDialogs?

    Your English is better than good enough. No worries.

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

    noobies (May 22nd, 2014)

  4. #3
    Member
    Join Date
    Mar 2014
    Posts
    36
    My Mood
    Confused
    Thanks
    20
    Thanked 1 Time in 1 Post

    Default Re: About swing Timer in my Math game

    I don't ready to advance JFrame. I guess I have to use just JOptionPane for now plus timer of course. Hahaha

  5. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: About swing Timer in my Math game

    That's fine. Search around a bit for examples or advice about using a javax.swing.Timer, attempt to code it, and come back if/when you need help. Since your code is likely mostly contained in a main() method, you may be able to use a java.util.Timer, but you'll have to reveal more about the existing design to say for sure.

  6. The Following User Says Thank You to GregBrannon For This Useful Post:

    noobies (May 22nd, 2014)

  7. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: About swing Timer in my Math game

    I'm not sure how a Timer would help here. JOptionPane's dialogs are modal. I don't know if you can "cancel" a dialog that is being shown.
    That would mean that the program would have to wait for the user's input before the dialog window returns to the application. If that is how it must work, then comparing the start time and end time from the system's current time would tell you if the user took too long.

    The technical question is: Can a JOptionPane dialog window be closed by the program?
    If you don't understand my answer, don't ignore it, ask a question.

  8. The Following User Says Thank You to Norm For This Useful Post:

    noobies (May 22nd, 2014)

  9. #6
    Member
    Join Date
    Mar 2014
    Posts
    36
    My Mood
    Confused
    Thanks
    20
    Thanked 1 Time in 1 Post

    Default Re: About swing Timer in my Math game

    I deleted the code because it solved
    Last edited by noobies; May 22nd, 2014 at 11:06 PM.

  10. #7
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: About swing Timer in my Math game

    Why are you still using variables like d1, d2, . . ., dx instead of an array? Having a collection of variables that can be iterated with a loop greatly simplifies the if logic. The long chains of if and if/else statements are just (or could be just) unnecessary.

  11. The Following User Says Thank You to GregBrannon For This Useful Post:

    noobies (May 22nd, 2014)

  12. #8
    Member
    Join Date
    Mar 2014
    Posts
    36
    My Mood
    Confused
    Thanks
    20
    Thanked 1 Time in 1 Post

    Default Re: About swing Timer in my Math game

    I tried the java.util.Timer:

     
    package timertest;
     
    import java.util.*;
     
    public class TimerTest {
     
        private static int count = 0;
     
        public static void main(String[] arg) {
            Scanner sc = new Scanner(System.in);
            Timer time = new Timer();  // Create a Timer object
            TimerTask timeTask = new TimerTask() {
                // This is what we want the Timer to do once a second.
                public void run() {
                    count += 1;
                    System.out.println(count);
                    if (count == 3) {
                        System.exit(0);
                    }
    //                System.out.print("answer: ");         <---- IF I ENABLED THIS, IT WON'T WORK, WHY?
    //                byte jwb = sc.nextByte();
                }
            };
     
            time.schedule(timeTask, 1000, 1000);
        }
    }

    What I want is if I enabled the scanner, the scanner will run along with the timer's count. When the count is at 3, the scanner will ask the input, and the program will go into next procedure.

  13. #9
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: About swing Timer in my Math game

    Focusing on the new feature you're trying to learn with a simple study program is an excellent approach.

    What do you mean "IT WON'T WORK?" If you're getting errors, post them.

  14. The Following User Says Thank You to GregBrannon For This Useful Post:

    noobies (May 22nd, 2014)

  15. #10
    Member
    Join Date
    Mar 2014
    Posts
    36
    My Mood
    Confused
    Thanks
    20
    Thanked 1 Time in 1 Post

    Default Re: About swing Timer in my Math game

    I already said the problem after the code section:

    What I want is, if I enabled the scanner, the scanner will run along with the timer's count. When the count is at 3, the scanner will ask the input, and the program will go into next procedure.

    After I enabled the code which I marked as a comment in the code section (the scanner that ask the user's input), the program didn't work like I wanted.

  16. #11
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: About swing Timer in my Math game

    No compiler errors? Believe me, there's an error, and it's the primary reason your code is not working as you'd like.

    If you're not seeing the error, that's a bigger problem, and that's why I ask. If you're not seeing the error, then you don't know what needs fixing, you'll never learn how to fix it, and you won't learn how to avoid the error in the future. Without the benefit of seeing the error, understanding it, fixing it, and avoiding it next time, the benefit of this study will be less, possibly nil, and you'll repeat the error until you've gone through the process I've described.

    Let's start over: Post the error.

  17. The Following User Says Thank You to GregBrannon For This Useful Post:

    noobies (May 22nd, 2014)

  18. #12
    Member
    Join Date
    Mar 2014
    Posts
    36
    My Mood
    Confused
    Thanks
    20
    Thanked 1 Time in 1 Post

    Default Re: About swing Timer in my Math game

    This is the output:
     
    run:
    1  <-- This is the timer's count
    answer:
    The time didn't increase until I repeatedly inputed the answer, what I want is the timer continually count and doesn't wait the user input the answer.

  19. #13
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: About swing Timer in my Math game

    Compiler errors. Do you have any of those?

  20. The Following User Says Thank You to GregBrannon For This Useful Post:

    noobies (May 22nd, 2014)

  21. #14
    Member
    Join Date
    Mar 2014
    Posts
    36
    My Mood
    Confused
    Thanks
    20
    Thanked 1 Time in 1 Post

    Default Re: About swing Timer in my Math game

    Nope, no error has shown.
    But, it's not what I want.

  22. #15
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: About swing Timer in my Math game

    With the commented lines uncommented, you should be getting this compiler error:
    File: C:\Java Source\TimerTest.java  [line: 22]
    Error: Cannot refer to a non-final variable sc inside an inner class defined in a different method
    You need to figure out why you're not seeing the compiler error.

  23. The Following User Says Thank You to GregBrannon For This Useful Post:

    noobies (May 22nd, 2014)

  24. #16
    Member
    Join Date
    Mar 2014
    Posts
    36
    My Mood
    Confused
    Thanks
    20
    Thanked 1 Time in 1 Post

    Default Re: About swing Timer in my Math game

    No, the compiler not shown any error like that, I swear it
    I paste the code above again and run it and not shown any errors.

  25. #17
    Member
    Join Date
    Mar 2014
    Posts
    36
    My Mood
    Confused
    Thanks
    20
    Thanked 1 Time in 1 Post

    Default Re: About swing Timer in my Math game

    I solved the timer thing (the time limit is 3 second), I add this in Matimatika class:

     
    public String getInput(String input) {
            TimerTask task = new TimerTask() {
                public void run() {
                    if (jwb == 0) {
                        System.out.println("No answer");
                        System.exit(0);
                    }
                }
            };
            Timer timer = new Timer();
            timer.schedule(task, 3 * 1000);
            String jawaban = JOptionPane.showInputDialog(input);
            timer.cancel();
            return jawaban;
        }

    There is still some problem, I'll ask the problem tomorrow. I want going home from my college for now.

  26. #18
    Member
    Join Date
    Mar 2014
    Posts
    36
    My Mood
    Confused
    Thanks
    20
    Thanked 1 Time in 1 Post

    Default Re: About swing Timer in my Math game

    So.. This is the problem:
    There are 3 question in this game and every question is limited by time. If the user failed to to input the answer the first question before the time limit, the user must continue into second question and sam time limit and so on..
    The game's has one class called Matimatika
    the question set in here:

    first the set method for the operators and digits:

     
     package projek7_gui;
     
    import javax.swing.JOptionPane;
    import java.util.*;
     
    public class Matimatika {
     
        ArrayList list = new ArrayList();
        ArrayList list2 = new ArrayList();
        ArrayList list3 = new ArrayList();
        ArrayList easySort = new ArrayList();
        ArrayList normSort = new ArrayList();
        ArrayList hardSort = new ArrayList();
        Scanner input = new Scanner(System.in);
        Random acak = new Random();
        byte skor, skor2, skor3;
        double d1, d2, d3, d4, d5, d6, op, op2, op3, jwb;
     
         void set() {
     
            d1 = rand.nextInt(20) + 1;
            d2 = rand.nextInt(20) + 1;
            d3 = rand.nextInt(20) + 1;
            op = 0;
        }
     
     void easy() {
            String jawaban, a;
            int answer = 0;
            set();
    if (op == 0) {
                a = d1 + " + " + d2 + " - " + d3;
     String b = (getInput(a));
                answer = Integer.parseInt(b);
                jwb = d1 + d2 - d3;
            }
    if (answer == jwb) {
                JOptionPane.showMessageDialog(null, "True!");
                skor += 10;
            } else {
                JOptionPane.showMessageDialog(null, "Goblok");
            }
            JOptionPane.showMessageDialog(null, "Your score: " + skor, "Score", JOptionPane.PLAIN_MESSAGE);
        }
     
     void Wizard() {
     for (int i = 0; i < nama.length; i++) {
                                        list.add(new String(nama[i]));
                                        skor = 0;
                                        JOptionPane.showMessageDialog(null, "Turn " + (i + 1) + "!\nPlayer " + nama[i], "Get Ready Dumbass!", JOptionPane.INFORMATION_MESSAGE);
                                        for (int j = 0; j < 3; j++) {
                                            easy();
                                        }
                                        easySort.add(new Byte(skor));
     
                                    }
     
     public String getInput(String input) {
     
            TimerTask task = new TimerTask() {
                public void run() {
                    if (jwb == 0) {
                        JOptionPane.showMessageDialog(null, "Time Limit", "Time's over!", JOptionPane.WARNING_MESSAGE);
                    }
                }
            };
            Timer timer = new Timer();
            timer.schedule(task, 2 * 1000);
            String jawaban = JOptionPane.showInputDialog(input);
            timer.cancel();
            return jawaban;
        }

    I simplify the code. The main program will just call the Wizard() method.
    So.. the summary:
    set() is to set the digits and operators (I simplify the op == 0)
    easy() is to make the questions

    the wizard is to call the easy() and loop 3 times for the questions

    getInput(String input) is to make the dialog input will close after the time limit. (Time limit is 2 seconds)

    THE PROBLEM:
    When I ran the program, and it shown first question and i test it with no input at all. The warning message has shown but the first question dialog box is still shown.
    And I tested it again, if I input the answers for first question and the second question shown BUT the time limit isn't working! After 2 seconds it not shown any warning message in the second question.
    How to fix that? I hope you can understand the meaning of my terrible english here...

  27. #19
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: About swing Timer in my Math game

    i test it with no input at all. The warning message has shown but the first question dialog box is still shown.
    The dialog windows are modal. That means that the code's execution stops moving until the user enters something and presses Enter so that the dialog can return to the calling code and allow execution to continue. See post#5
    If you don't understand my answer, don't ignore it, ask a question.

  28. The Following User Says Thank You to Norm For This Useful Post:

    noobies (May 22nd, 2014)

  29. #20
    Member
    Join Date
    Mar 2014
    Posts
    36
    My Mood
    Confused
    Thanks
    20
    Thanked 1 Time in 1 Post

    Default Re: About swing Timer in my Math game

    Oh, I see...
    So basically the dialog must wait the user.
    So, How about if the time is over, the program will write down automatically the false answer, so the dialog will close and continue into next question.
    Can the program do that?

  30. #21
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: About swing Timer in my Math game

    What you describe cannot be done without changing the JOptionPane to a JDialog, a technique you would have found by searching the web as I suggested in Post #2. There's actually a JOptionPane method for doing that which you can learn more about by reading the JOptionPane API. However, if you're unwilling, unable, etc. to use the JDialog as you said in Post #3, then what you describe is not possible.

  31. The Following User Says Thank You to GregBrannon For This Useful Post:

    noobies (May 22nd, 2014)

  32. #22
    Member
    Join Date
    Mar 2014
    Posts
    36
    My Mood
    Confused
    Thanks
    20
    Thanked 1 Time in 1 Post

    Default Re: About swing Timer in my Math game

    Yes, I see.
    Alright then I'll change the GUI with JDialog. Thanks then! I marked it into solved for now.
    Thanks for the help.

Similar Threads

  1. Swing TIMER
    By newtolearningjava in forum What's Wrong With My Code?
    Replies: 14
    Last Post: April 13th, 2014, 04:01 PM
  2. [SOLVED] both class javax.swing.Timer in javax.swing and class java.util.Timer in java.util match
    By stresstedout in forum What's Wrong With My Code?
    Replies: 13
    Last Post: April 10th, 2014, 07:32 PM
  3. Timer implementation in swing
    By portem1 in forum Algorithms & Recursion
    Replies: 1
    Last Post: January 19th, 2011, 08:14 AM
  4. Anyone familiar with the swing timer?
    By BigJoe in forum AWT / Java Swing
    Replies: 5
    Last Post: December 28th, 2010, 12:15 PM
  5. Help - Swing Timer, 2 KeyEvents
    By Gheta in forum AWT / Java Swing
    Replies: 2
    Last Post: July 29th, 2009, 02:46 PM