Results 1 to 2 of 2
  1. #1
    acash229 is offline Member
    Join Date
    Sep 2010
    Posts
    60
    Rep Power
    0

    Default Help with fixing timer

    Hey guys
    i fixed the timer but the elapsedtime is kind of messed up.
    It is showing strange timings
    please help

    Here is the code
    Java Code:
    import javax.swing.*;
    import javax.swing.border.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.Random;
    
    
    
    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;
                
    
                public void actionPerformed(ActionEvent ae) {
                JButton source = (JButton)ae.getSource();
                float elapsedTimeMillis = 0;
                double elapsedTimesec1;
                
                
    
                
    
                  
          if (source == theMainButton) {
       
          resizeComponent(this);
       
          setLocation(rand.nextInt(screenSize.width-3*BUTTON_WIDTH),
       
          rand.nextInt(screenSize.height-3*BUTTON_HEIGHT));
       
          long start = System.currentTimeMillis(); // get the starting milli-seconds
       
          n++;
       
          if(n>=3){
       
          JOptionPane.showMessageDialog(null, "You Clicked it " +n);
       
          long elapsedTimeSec = System.currentTimeMillis(); // get the stoping milli-seconds,
      
          elapsedTimesec1 =(double)(elapsedTimeSec - start)/100.0; // calculate the lasting time in seconds.
      
          JOptionPane.showMessageDialog(null, "Took you: " + elapsedTimesec1 + " seconds."); // showing the result.
      
          }
    
                    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);
    	}
    }
    thank you

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

Similar Threads

  1. THIS PROGRAM IS DRIVING ME CRAZY!!! help fixing it
    By syntrax in forum New To Java
    Replies: 2
    Last Post: 12-18-2009, 05:27 AM
  2. [SOLVED] I need help fixing an error
    By leonardjr in forum New To Java
    Replies: 8
    Last Post: 02-21-2009, 09:33 PM
  3. help with fixing my assignment.please...error
    By Jin-enigma-PL in forum New To Java
    Replies: 4
    Last Post: 02-05-2009, 01:12 AM
  4. Replies: 0
    Last Post: 04-04-2008, 03:46 PM
  5. Exception Error need help fixing
    By skinnybones in forum New To Java
    Replies: 2
    Last Post: 12-03-2007, 08:14 PM

Posting Permissions

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