Results 21 to 40 of 59
Thread: Need Help with timer (Java)
- 09-12-2010, 11:11 PM #21
Member
- Join Date
- Sep 2010
- Posts
- 60
- Rep Power
- 0
- 09-12-2010, 11:13 PM #22
Member
- Join Date
- Sep 2010
- Posts
- 60
- Rep Power
- 0
Its not working.
- 09-12-2010, 11:22 PM #23
What is the "it" that is not working?Its not working.
Did you write the 5 line program I suggested you write to get familiar with the method?
You need to find three spots in your program:I dont now how to implement the code into this 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.
- 09-12-2010, 11:34 PM #24
Member
- Join Date
- Sep 2010
- Posts
- 60
- Rep Power
- 0
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.
- 09-12-2010, 11:40 PM #25
Please explain. What do you get? What is wrong with it?I do not get the correct timing
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
- 09-12-2010, 11:44 PM #26
Member
- Join Date
- Sep 2010
- Posts
- 60
- Rep Power
- 0
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.
- 09-12-2010, 11:53 PM #27
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.
- 09-13-2010, 12:01 AM #28
Member
- Join Date
- Sep 2010
- Posts
- 60
- Rep Power
- 0
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 :(
- 09-13-2010, 12:03 AM #29
Where is that in your code?The timing should start right when the user clicks the grid button.
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?
- 09-13-2010, 12:43 AM #30
Member
- Join Date
- Sep 2010
- Posts
- 60
- Rep Power
- 0
Can you please fix this for meJava 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(); } }
thank you so much
- 09-13-2010, 01:04 AM #31
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.
- 09-13-2010, 01:05 AM #32
Member
- Join Date
- Sep 2010
- Posts
- 60
- Rep Power
- 0
I have been listening.
I dnt understand it
- 09-13-2010, 01:11 AM #33
- 09-13-2010, 01:13 AM #34
Member
- Join Date
- Sep 2010
- Posts
- 60
- Rep Power
- 0
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.
- 09-13-2010, 01:40 AM #35
Can you use the value of 'n' to determine if it is the first time the button has been pressed?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?
Also the variable running could be used.Last edited by Norm; 09-13-2010 at 01:42 AM.
- 09-13-2010, 01:42 AM #36
Member
- Join Date
- Sep 2010
- Posts
- 60
- Rep Power
- 0
value of n?
- 09-13-2010, 01:48 AM #37
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.
- 09-13-2010, 01:58 AM #38
Member
- Join Date
- Sep 2010
- Posts
- 60
- Rep Power
- 0
That is for the number of clicks
- 09-13-2010, 02:00 AM #39
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.
- 09-13-2010, 02:10 AM #40
Member
- Join Date
- Sep 2010
- Posts
- 60
- Rep Power
- 0
Similar Threads
-
New to Java need help with a timer
By kd0jzi in forum New To JavaReplies: 3Last Post: 03-20-2010, 03:20 AM -
Java Timer
By sasi25 in forum Advanced JavaReplies: 4Last Post: 01-30-2010, 09:19 AM -
Timer in java
By manhit45 in forum New To JavaReplies: 4Last Post: 12-14-2009, 02:27 PM -
How to cancel an individual timer in spite of canceling whole timer
By Java Tip in forum java.utilReplies: 0Last Post: 04-04-2008, 02:46 PM -
Help with timer in java
By barney in forum Advanced JavaReplies: 1Last Post: 08-01-2007, 10:24 AM


LinkBack URL
About LinkBacks


Bookmarks