Page 1 of 3 123 LastLast
Results 1 to 20 of 59
  1. #1
    acash229 is offline Member
    Join Date
    Sep 2010
    Posts
    60
    Rep Power
    0

    Default Need Help with timer (Java)

    Hey guys,

    I have got the code to work, but my timer does not work correctly. it shows like 49 minutes when its been only 15 seconds.
    Please help and tell me what is wrong with my timer.
    Thanks

    Code


    Java Code:
    import javax.swing.*;
    import javax.swing.border.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.Random;
    import java.util.*;
    import java.util.Date;
    import java.text.DateFormat;
    import java.text.SimpleDateFormat;
    
    public class JumpingPanel extends JFrame{
        public static final int BUTTON_WIDTH = 45;
        public static final int BUTTON_HEIGHT = 45;
    static int n=0;
        private static final Dimension screenSize =
        		Toolkit.getDefaultToolkit().getScreenSize(); // Dimension of the screen
    
        private JPanel middle; // The content panel of this frame
        private TargetPanel target; // The button array
    
        private static void resizeComponent(Component c) {
    	    Dimension d = c.getSize(null);
                c.setPreferredSize(new Dimension((int)(0.9*d.width), (int)(0.9*d.height)));
        }
    
    
       // The panel with 3x3 array of buttons
        private static class TargetPanel extends JPanel implements ActionListener {
                private JButton theMainButton; // Central button in button array
                private Random rand; // Random number generator used in relocating
    
                /** When we hit a button, if the central one, we relocate
                    the target panel
                */
            public void actionPerformed(ActionEvent ae) {
                JButton source = (JButton)ae.getSource();
                if (source == theMainButton) {
    
    		resizeComponent(this);
                    setLocation(rand.nextInt(screenSize.width-3*BUTTON_WIDTH),
                                rand.nextInt(screenSize.height-3*BUTTON_HEIGHT));
    
    n++;
    if(n>=3){
    
    Calendar calendar = new GregorianCalendar();
        String am_pm;
        int hour = calendar.get(Calendar.HOUR);
        int minute = calendar.get(Calendar.MINUTE);
        int second = calendar.get(Calendar.SECOND);
        if(calendar.get(Calendar.AM_PM) == 0)
          am_pm = "AM";
        else
          am_pm = "PM";
       
        JOptionPane.showMessageDialog(null, "You Clicked it " +n);
        JOptionPane.showMessageDialog(null,"Current Time : "+minute+ " minutes and " + second+ " seconds");
      }
    
    
    
                    
    validate();
                }
    
            }
            // Here is the target panel with the nine buttons
            public TargetPanel() {
                rand = new Random();
                setLayout(new GridLayout(3,3));
    
                for (int r = 0; r < 3; r++) {
                    for (int c = 0; c < 3; c++) {
                        JButton button = new JButton();
                        if (r == 1 && c == 1) {
                            theMainButton = button;
                            theMainButton.addActionListener(this);
    
                        }
                        button.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
                        add(button);
    
                    }
                }
                setMaximumSize(new Dimension(3*BUTTON_WIDTH,3*BUTTON_HEIGHT));
                Border border = BorderFactory.createLineBorder(Color.BLACK, 4);
                setBorder(border);
            }
        }
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            JumpingPanel frame = new JumpingPanel();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setTitle("The Clicking Game");
            frame.setMinimumSize(screenSize);
    
            frame.middle = (JPanel)frame.getContentPane();
            frame.middle.setLayout(new FlowLayout());
    
            frame.target = new TargetPanel();
            frame.middle.add(frame.target);
    
            frame.setVisible(true);
    	}
    }

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,330
    Rep Power
    25

    Default

    my timer does not work correctly. it shows like 49 minutes
    What does the time being displayed represent? Current time or what?

  3. #3
    acash229 is offline Member
    Join Date
    Sep 2010
    Posts
    60
    Rep Power
    0

    Default

    No its supposed to be a stopwatch.
    Once i click it starts from 0 and counts how long it takes till i click the button 3 times.

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,330
    Rep Power
    25

    Default

    Where and how to you save the starting time?

  5. #5
    acash229 is offline Member
    Join Date
    Sep 2010
    Posts
    60
    Rep Power
    0

    Default

    what do you mean??

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,330
    Rep Power
    25

    Default

    how long it takes
    How do you intent to compute "how long it takes"? Doesn't that mean you have to save the starting time?
    If you start a task at 2:30 and finish it at 2:45, how would you compute how long the task took?

  7. #7
    acash229 is offline Member
    Join Date
    Sep 2010
    Posts
    60
    Rep Power
    0

    Default

    No i need to to add the stop watch suntex:

    System.currentTimeMillis() method

    I just dont now how to

    Basically once i click the button in the middle first time i need the timer to go on.
    After i reach the certain amount of clicks i need the timer to stop and tell me how long it took.

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,330
    Rep Power
    25

    Default

    Is this the same problem as: Java Gave Question

    Please only use one thread for each problem
    Which thread do you want the answer on? Which thread is finished?

  9. #9
    acash229 is offline Member
    Join Date
    Sep 2010
    Posts
    60
    Rep Power
    0

    Default

    Please answer on this thread as it is the most updated thread.
    thanks

  10. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,330
    Rep Power
    25

    Default

    No its not.
    Zack has given you the answer on the other thread.

  11. #11
    acash229 is offline Member
    Join Date
    Sep 2010
    Posts
    60
    Rep Power
    0

    Default

    That is a code for the current time.
    I need to make a stopwatch so it shows how long it takes

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by acash229 View Post
    No i need to to add the stop watch suntex:

    System.currentTimeMillis() method

    I just dont now how to

    Basically once i click the button in the middle first time i need the timer to go on.
    After i reach the certain amount of clicks i need the timer to stop and tell me how long it took.
    Norm is telling you how to do this. You need to save the start time in a long variable when the start button is pressed. Then when the timer is stopped, you save the system time in another long variable and by subtracting the two and dividing by 1000, you should have how many seconds of time has elapsed.

  13. #13
    acash229 is offline Member
    Join Date
    Sep 2010
    Posts
    60
    Rep Power
    0

    Default

    lol its confusing.

  14. #14
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,330
    Rep Power
    25

    Default

    If you had a watch in front of you and wanted to time how long it took for a person to walk across the room, how would you do it? When the person started, you'd look at the watch and see the time (the starting time) and then you'd wait until the person got to the other side and then you'd look at your watch again and see the time(the ending time).
    Given those two times, you subtract the starting time from the ending time to get the time for the person to walk across the room.
    The key points are: remembering the start time, getting the end time and subtracting.

    In your program you don't have a watch, you use the method that Zack gave you on the other thread to get the times.

  15. #15
    acash229 is offline Member
    Join Date
    Sep 2010
    Posts
    60
    Rep Power
    0

    Default

    So basically take this:
    Calendar calendar = new GregorianCalendar();
    String am_pm;
    int hour = calendar.get(Calendar.HOUR);
    int minute = calendar.get(Calendar.MINUTE);
    int second = calendar.get(Calendar.SECOND);
    if(calendar.get(Calendar.AM_PM) == 0)
    am_pm = "AM";
    else
    am_pm = "PM";


    and then subtract initial time by final time.

    The above code shows the time that it currently is. So how do i put that in a variable and then subtract it by the final time?

    What is the variable that is holding my initial time though?

  16. #16
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,330
    Rep Power
    25

    Default

    The code you just posted has NOTHING to do with timing the duration of a task. Delete it or throw it out.
    Have you lost the link to the other thread? Three of us have been trying to explain to you how to compute the duration of a task.

  17. #17
    acash229 is offline Member
    Join Date
    Sep 2010
    Posts
    60
    Rep Power
    0

    Default

    Okay im going to throw this code piece away.

    Now what code should i use and where does it belong in the code.

    I understand what you guys are saying, but i am not good at exactly placing it the code piece.
    I need help in that

  18. #18
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,330
    Rep Power
    25

    Default

    At the start of the trial, save the current time by using an assignment statement to save the value of the method Zack posted.
    At the end of the trail, save the current time by the same method as using above.

    Use an assignment statement to save the difference between those two saved times.
    That will be the number of ms used in the trial.

  19. #19
    acash229 is offline Member
    Join Date
    Sep 2010
    Posts
    60
    Rep Power
    0

    Default

    assignment statement??

    What do you mean start of the trial.
    in the start of the actionPerformed class?

    and everytrime i just copy and put that code there it gives an error

  20. #20
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,330
    Rep Power
    25

    Default

    What do you call this type of statement:
    var = value; // assign value to var

    Perhaps you should write a small test program to see how to time something.
    It should have about 5 lines of code.
    1) save start time
    2) use Thread.sleep(1000); to wait some time
    3) get the end time
    4) compute the duration (end - start)
    5) print out the results.


    everytrime i just copy and put that code there it gives an error
    Please copy full text of error message and paste it here. Here is a sample:
    Java Code:
    TestSorts.java:138: cannot find symbol
    symbol  : variable var
    location: class TestSorts
             var = 2;
             ^

Page 1 of 3 123 LastLast

Similar Threads

  1. New to Java need help with a timer
    By kd0jzi in forum New To Java
    Replies: 3
    Last Post: 03-20-2010, 03:20 AM
  2. Java Timer
    By sasi25 in forum Advanced Java
    Replies: 4
    Last Post: 01-30-2010, 09:19 AM
  3. Timer in java
    By manhit45 in forum New To Java
    Replies: 4
    Last Post: 12-14-2009, 02:27 PM
  4. Replies: 0
    Last Post: 04-04-2008, 02:46 PM
  5. Help with timer in java
    By barney in forum Advanced Java
    Replies: 1
    Last Post: 08-01-2007, 10:24 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •