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

    Default

    It doesnt work. I dont now how to implement the code into this program

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

    Default

    Its not working.

  3. #23
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,620
    Rep Power
    23

    Default

    Its not working.
    What is the "it" that is not working?
    Did you write the 5 line program I suggested you write to get familiar with the method?

    I dont now how to implement the code into this program
    You need to find three spots in your program:
    1) a place to define the variable to hold the starting time that will be in scope for the next two steps
    2) the place in the code to save the current time when the "task" starts
    3) the place in the code when you know the task has ended.

    Do you know in your code where those places are?
    Last edited by Norm; 09-12-2010 at 11:25 PM.

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

    Default

    I added the statement, but I do not get the correct timing. Can you please just help me. I think theres only a minor problem im doing rong now but i cant seem to see it.
    Please help. I really need it.

    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(); 
    
        private JPanel middle; 
        private TargetPanel target;
    
        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)));
        }
    
    
      
        private static class TargetPanel extends JPanel implements ActionListener {
                private JButton theMainButton; 
                private Random rand;
            private long startTime;
            private long stopTime;
            private boolean running;
    
                
            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));
    
    this.startTime = System.currentTimeMillis();
            this.running = true;
    
    
    n++;
    if(n>=3){
    
    
    
        JOptionPane.showMessageDialog(null, "You Clicked it " +n);
        this.stopTime = System.currentTimeMillis();
            this.running = false;
    long elapsed;
            if (running) {
                elapsed = ((System.currentTimeMillis() - startTime) / 1000);
            }
            else {
                elapsed = ((stopTime - startTime) / 1000);
            }
            
    
            JOptionPane.showMessageDialog(null, "Completed in " +elapsed+ " seconds");
      }
    
    
    
                    
    validate();
                }
    
            }
           
            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);
            }
        }
    
        
        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);
    	}
    }
    Last edited by acash229; 09-12-2010 at 11:40 PM.

  5. #25
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,620
    Rep Power
    23

    Default

    I do not get the correct timing
    Please explain. What do you get? What is wrong with it?


    If you want anyone to look at your code, you should put it in code tags: http://www.java-forums.org/misc.php?do=bbcode#code

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

    Default

    Im sorry i fixed the placing,

    but now when i run the program i only get 0 seconds or 1 second.

    I dont know what i messed up in the code. Can you please look over the code.

  7. #27
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,620
    Rep Power
    23

    Default

    Can you add comments to the code to show where the timing should start and why it should start there and where the timing should end and why.

    Please describe when the timing should start? When the button is FIRST pressed? Or any time the button is pressed or When the frame is displayed?

    Your code sets the timer EVERY time theMainButton is pressed.

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

    Default

    The timing should start right when the user clicks the grid button.
    Once the user clicks the grid button 3 times, the timer should stop and then display on screen how long it took the user to click the button 3 times.

    Please Help with code :(

  9. #29
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,620
    Rep Power
    23

    Default

    The timing should start right when the user clicks the grid button.
    Where is that in your code?

    Is the problem that you only want to remember the starting time the FIRST time the button is clicked? If so do you have a variable with a value you could test to determine that?

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

    Default

    Java Code:
        private static class TargetPanel extends JPanel implements ActionListener {
                private JButton theMainButton; 
                private Random rand;
            private long startTime;
            private long stopTime;
            private boolean running;
    
                
            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));
    
    this.startTime = System.currentTimeMillis();
            this.running = true;
    
    
    n++;
    if(n>=3){
    
    
    
        JOptionPane.showMessageDialog(null, "You Clicked it " +n);
        this.stopTime = System.currentTimeMillis();
            this.running = false;
    long elapsed;
            if (running) {
                elapsed = ((System.currentTimeMillis() - startTime) / 1000);
            }
            else {
                elapsed = ((stopTime - startTime) / 1000);
            }
            
    
            JOptionPane.showMessageDialog(null, "Completed in " +elapsed+ " seconds");
      }
    
    
    
                    
    validate();
                }
    
            }
    Can you please fix this for me
    thank you so much

  11. #31
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    4

    Default

    Why are you avoiding answering Norm's questions? Believe it or not, he's being very helpful.

    We're not just going to take your code and "fix" it for you.

    You have to put in some effort, or you're going to get nowhere.

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

    Default

    I have been listening.
    I dnt understand it

  13. #33
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    4

    Default

    Quote Originally Posted by acash229 View Post
    I have been listening.
    I dnt understand it
    Then tell us that, instead of just posting the code and saying "fix this". What part of his questions don't you get? He's simply asking what part of your code handles the clicking of the grid button, as well as WHEN you want to set the startTime.

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

    Default

    I posted it on the top. THat little part of the code shows

    if (source == theMainButton)

    //that is when the clicked is clicked//

    right there i want the timer to start, right when it is clicked 3 times, I need it to stop timing and record it on the screen.

  15. #35
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,620
    Rep Power
    23

    Default

    you only want to remember the starting time the FIRST time the button is clicked? If so do you have a variable with a value you could test to determine that?
    Can you use the value of 'n' to determine if it is the first time the button has been pressed?
    Also the variable running could be used.
    Last edited by Norm; 09-13-2010 at 01:42 AM.

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

    Default

    value of n?

  17. #37
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,620
    Rep Power
    23

    Default

    Sorry, are we talking about different programs?
    The code posted above has a variable named: n. It starts at the value of 0 and is incremented every time the button is pressed.

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

    Default

    That is for the number of clicks

  19. #39
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,620
    Rep Power
    23

    Default

    Can you look at the value of n and determine if a click is the first click? Is That when you want to start the timer?

    You said:
    The timing should start right when the user clicks the grid button.

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

Page 2 of 3 FirstFirst 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
  •