944,080 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 1842
  • Java RSS
Jul 23rd, 2006
0

Basketball timer help needed

Expand Post »
I am having trouble getting my timer class working with my scoreboard frame and application. Any constructive help is appreciated.

Java Syntax (Toggle Plain Text)
  1. public class ScoreboardTimer extends Thread
  2. {
  3. private int counter; // the number of seconds remaining on the timer
  4. private boolean isRunning; // toggle variable that controls whether the counter is decremented or not
  5. private JTextField timerDisplay; // reference to the GUI object displaying the timer
  6. // getters and setters for the variables
  7. public boolean toggleRunning()
  8. {
  9. // toggles the value of isRunning variable; called from click handler on/off button on frame
  10. }
  11. public void run()
  12. {
  13. while (counter > 0)
  14. {
  15. // delay one second with sleep() method
  16. // if clock is running, decrement counter and update display
  17. }
  18. }
  19. }

Java Syntax (Toggle Plain Text)
  1. import javax.swing.JFrame;
  2. import javax.swing.JMenu;
  3. import javax.swing.JMenuBar;
  4. import javax.swing.JMenuItem;
  5. import java.awt.Dimension;
  6. import java.awt.event.ActionEvent;
  7. import java.awt.event.ActionListener;
  8. import java.awt.GridBagLayout;
  9. import java.awt.BorderLayout;
  10. import javax.swing.JPanel;
  11. import java.awt.Color;
  12. import javax.swing.JTextField;
  13. import java.awt.GridBagConstraints;
  14. import java.awt.Insets;
  15. import java.awt.Font;
  16. import javax.swing.JLabel;
  17. import javax.swing.JButton;
  18. import javax.swing.*;
  19. import java.text.*;
  20. public class ScoreboardFrame extends JFrame
  21. {
  22. JMenuItem menuFileExit = new JMenuItem();
  23. JMenu menuFile = new JMenu();
  24. JMenuBar menuBar1 = new JMenuBar();
  25. BorderLayout borderLayout1 = new BorderLayout();
  26. JPanel panelScoreboard = new JPanel();
  27. GridBagLayout gridBagLayout1 = new GridBagLayout();
  28. JTextField txtHomeScore = new JTextField();
  29. JLabel jLabel1 = new JLabel();
  30. JLabel jLabel2 = new JLabel();
  31. JTextField txtGuests = new JTextField();
  32. JTextField txtTime = new JTextField();
  33. JPanel panelButtons = new JPanel();
  34. JButton btnTimeControl = new JButton();
  35. JButton btnHomeAdd = new JButton();
  36. JButton btnHomeSub = new JButton();
  37. JButton btnGuestsAdd = new JButton();
  38. JButton btnGuestsSub = new JButton();
  39. DecimalFormat df = new DecimalFormat("00");
  40. int homeScore = 0;
  41. int guestsScore = 0;
  42. public ScoreboardFrame()
  43. {
  44. try
  45. {
  46. jbInit();
  47. }
  48. catch(Exception e)
  49. {
  50. e.printStackTrace();
  51. }
  52. }
  53. private void jbInit() throws Exception
  54. {
  55. this.setJMenuBar(menuBar1);
  56. this.getContentPane().setLayout(borderLayout1);
  57. this.setSize(new Dimension(534, 300));
  58. this.setTitle("Basketball Scoreboard");
  59. menuFile.setText("File");
  60. panelScoreboard.setBackground(new Color(227, 82, 227));
  61. panelScoreboard.setLayout(gridBagLayout1);
  62. txtHomeScore.setText("00");
  63. txtHomeScore.setPreferredSize(new Dimension(100, 75));
  64. txtHomeScore.setFont(new Font("SansSerif", 1, 60));
  65. txtHomeScore.setForeground(Color.red);
  66. txtHomeScore.setEditable(false);
  67. jLabel1.setText("Home");
  68. jLabel1.setFont(new Font("Dialog", 1, 20));
  69. jLabel1.setForeground(Color.white);
  70. jLabel2.setText("Guests");
  71. jLabel2.setForeground(Color.white);
  72. jLabel2.setFont(new Font("Dialog", 1, 20));
  73. txtGuests.setText("00");
  74. txtGuests.setCaretColor(Color.red);
  75. txtGuests.setForeground(Color.red);
  76. txtGuests.setEditable(false);
  77. txtGuests.setPreferredSize(new Dimension(100, 75));
  78. txtGuests.setFont(new Font("SansSerif", 1, 60));
  79. txtTime.setText("10:00");
  80. txtTime.setFont(new Font("SansSerif", 1, 60));
  81. txtTime.setForeground(Color.red);
  82. txtTime.setEditable(false);
  83. btnTimeControl.setText("Start/Stop");
  84. btnTimeControl.setForeground(Color.red);
  85. btnTimeControl.addActionListener(new ActionListener()
  86. {
  87. public void actionPerformed(ActionEvent e)
  88. {
  89. btnTimeControl_actionPerformed(e);
  90. }
  91. });
  92. btnHomeAdd.setText("Home +");
  93. btnHomeAdd.addActionListener(new ActionListener()
  94. {
  95. public void actionPerformed(ActionEvent e)
  96. {
  97. btnHomeAdd_actionPerformed(e);
  98. }
  99. });
  100. btnHomeSub.setText("Home -");
  101. btnHomeSub.addActionListener(new ActionListener()
  102. {
  103. public void actionPerformed(ActionEvent e)
  104. {
  105. btnHomeSub_actionPerformed(e);
  106. }
  107. });
  108. btnGuestsAdd.setText("Guests +");
  109. btnGuestsAdd.addActionListener(new ActionListener()
  110. {
  111. public void actionPerformed(ActionEvent e)
  112. {
  113. btnGuestsAdd_actionPerformed(e);
  114. }
  115. });
  116. btnGuestsSub.setText("Guests -");
  117. btnGuestsSub.addActionListener(new ActionListener()
  118. {
  119. public void actionPerformed(ActionEvent e)
  120. {
  121. btnGuestsSub_actionPerformed(e);
  122. }
  123. });
  124. menuFileExit.setText("Exit");
  125. menuFileExit.addActionListener(new ActionListener()
  126. {
  127. public void actionPerformed(ActionEvent ae)
  128. {
  129. fileExit_ActionPerformed(ae);
  130. }
  131. });
  132. menuFile.add(menuFileExit);
  133. menuBar1.add(menuFile);
  134. panelScoreboard.add(txtHomeScore, new GridBagConstraints(0, 1, 1, 1, 1.0, 1.0, GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(4, 4, 4, 4), 1, 1));
  135. panelScoreboard.add(jLabel1, new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(4, 4, 4, 4), 1, 1));
  136. panelScoreboard.add(jLabel2, new GridBagConstraints(4, 0, 1, 1, 1.0, 1.0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(4, 4, 4, 4), 1, 1));
  137. panelScoreboard.add(txtGuests, new GridBagConstraints(4, 1, 1, 1, 1.0, 1.0, GridBagConstraints.NORTHEAST, GridBagConstraints.NONE, new Insets(4, 4, 4, 4), 1, 1));
  138. panelScoreboard.add(txtTime, new GridBagConstraints(3, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
  139. this.getContentPane().add(panelScoreboard, BorderLayout.CENTER);
  140. panelButtons.add(btnHomeAdd, null);
  141. panelButtons.add(btnHomeSub, null);
  142. panelButtons.add(btnTimeControl, null);
  143. panelButtons.add(btnGuestsAdd, null);
  144. panelButtons.add(btnGuestsSub, null);
  145. this.getContentPane().add(panelButtons, BorderLayout.SOUTH);
  146. }
  147. void fileExit_ActionPerformed(ActionEvent e)
  148. {
  149. System.exit(0);
  150. }
  151. void btnTimeControl_actionPerformed(ActionEvent e)
  152. {
  153. }
  154. void btnHomeAdd_actionPerformed(ActionEvent e)
  155. {
  156. homeScore++;
  157. txtHomeScore.setText(df.format(homeScore));
  158. }
  159. void btnHomeSub_actionPerformed(ActionEvent e)
  160. {
  161. homeScore--;
  162. txtHomeScore.setText(df.format(homeScore));
  163. }
  164. void btnGuestsAdd_actionPerformed(ActionEvent e)
  165. {
  166. guestsScore++;
  167. txtGuests.setText(df.format(guestsScore));
  168. }
  169. void btnGuestsSub_actionPerformed(ActionEvent e)
  170. {
  171. guestsScore--;
  172. txtGuests.setText(df.format(guestsScore));
  173. }
  174. }


Java Syntax (Toggle Plain Text)
  1. import java.awt.Frame;
  2. import java.awt.event.WindowAdapter;
  3. import java.awt.event.WindowEvent;
  4. import javax.swing.UIManager;
  5. import java.awt.Dimension;
  6. import java.awt.Toolkit;
  7. public class ScoreboardApp
  8. {
  9. public ScoreboardApp()
  10. {
  11. Frame frame = new ScoreboardFrame();
  12. Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
  13. Dimension frameSize = frame.getSize();
  14. if (frameSize.height > screenSize.height)
  15. {
  16. frameSize.height = screenSize.height;
  17. }
  18. if (frameSize.width > screenSize.width)
  19. {
  20. frameSize.width = screenSize.width;
  21. }
  22. frame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
  23. frame.addWindowListener(new WindowAdapter()
  24. {
  25. public void windowClosing(WindowEvent e)
  26. {
  27. System.exit(0);
  28. }
  29. });
  30. frame.setVisible(true);
  31. }
  32. public static void main(String[] args)
  33. {
  34. try
  35. {
  36. UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
  37. }
  38. catch(Exception e)
  39. {
  40. e.printStackTrace();
  41. }
  42. new ScoreboardApp();
  43. }
  44. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
korbynlehr is offline Offline
18 posts
since May 2006
Jul 24th, 2006
0

Re: Basketball timer help needed

hope this will get you going

Java Syntax (Toggle Plain Text)
  1. import java.util.Timer;
  2. import java.util.TimerTask;
  3.  
  4. /**
  5.  * Work of peter_budo :o)
  6.  * Simple demo that uses java.util.Timer to schedule
  7.  * the specified task for repeated fixed-delay execution,
  8.  * beginning after the specified delay
  9.  */
  10.  
  11. public class CountDown
  12. {
  13. Timer timer;
  14. int runTime = 0;
  15. int setTime = 600; // time set up for 10min
  16.  
  17. public CountDown(int seconds)
  18. {
  19. timer = new Timer();
  20. timer.schedule(new CountDownTask(), seconds*1000, 1000);
  21. }
  22.  
  23. class CountDownTask extends TimerTask
  24. {
  25. public void run()
  26. {
  27. int convert;
  28. runTime++;
  29.  
  30. if( runTime < setTime)
  31. {
  32. convert = setTime - runTime;
  33. System.out.println( (int) convert/60 + ":" + convert%60);
  34. }
  35. else
  36. {
  37. timer.cancel(); //Terminate the timer thread
  38. }
  39. }
  40. }
  41.  
  42. public static void main(String args[])
  43. {
  44. new CountDown(1);
  45. System.out.print("Task scheduled.\n10:00\n");
  46. }
  47. }

for mor info on schedule Timer go to Sun Java

What you have to do is just sort displaying zeros befor time smaller then 10 in minutes or seconds
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 874
Code tags enforcer
peter_budo is offline Offline
6,659 posts
since Dec 2004
Jul 24th, 2006
0

Re: Basketball timer help needed

I may be missing something but this does not get my timer in the application to work. Can you please expand upon your answer for the ignorance I am obviously showing?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
korbynlehr is offline Offline
18 posts
since May 2006
Jul 25th, 2006
0

Re: Basketball timer help needed

Quote originally posted by korbynlehr ...
I may be missing something but this does not get my timer in the application to work. Can you please expand upon your answer for the ignorance I am obviously showing?
the answer is simple, you got source code for functional timer from me ( hope you test it, but have to say, it does work, it is simple cammand line program) and it is up to you implement it in your application
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 874
Code tags enforcer
peter_budo is offline Offline
6,659 posts
since Dec 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Data Models and Pattern Matching
Next Thread in Java Forum Timeline: What's a Java sheet?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC