I am looking for someone that can help me online for an hour or so maybe a few times for the next few weeks that can help me understand what I am supposed to do for my class programs. I am not looking for someone to do them for me but to be a guide and tutor.

Recommended Answers

All 17 Replies

I might suggest picking yourself up a copy of 'Beginning Programming with Java for Dummies"...and if you're feeling particularily sassy, O'Reilly Java In A Nutshell. It's all the reference you'll ever need (except for javax.swing.* and java.awt.* - which are briefly covered in the Dummies book).

Perhaps I could be of some assistance, though. Just post the issue you're having here, on this thread, and I'll help you best I can.

I am looking for someone that can help me online for an hour or so maybe a few times for the next few weeks that can help me understand what I am supposed to do for my class programs. I am not looking for someone to do them for me but to be a guide and tutor.

I am an Architect who is 10 years experienced in Software Development. Its very good that you are seeking help for class programs rather than getting them done as that's the right thing to do. I would be willing to be guide and tutor for $15 an hour. You can choose the time between 6pm and 10 pm EST and the days you want.

Thanks,
Bharat.
Phone 203 428 4124

Pick up a book and start reading it. Then, when you have a problem try to figure it out yourself. If you can't after some time get it solved, then post back to a forum. Paying for a tutor is a terrible idea. As a programmer you need to solve problems on your own. Learn how to look up information and search google.

I am an Architect who is 10 years experienced in Software Development. Its very good that you are seeking help for class programs rather than getting them done as that's the right thing to do. I would be willing to be guide and tutor for $15 an hour. You can choose the time between 6pm and 10 pm EST and the days you want.

Thanks,
Bharat.
Phone 203 428 4124

By any definition, that's not the right thing to do. If they're school/college/uni class programs, (and not just program classes) maybe you should consider a different course.

Still; I earnt a lot doing other people's schoolwork for money, and on the upside, It taught me and my clients valuable business skills.

Matt

The problem I am having is getting the last part working. The timer is the part I am having trouble with which is the last code I submitted.

import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.UIManager;
import java.awt.Dimension;
import java.awt.Toolkit;
public class ScoreboardApp 
{
  public ScoreboardApp()
  {
    Frame frame = new ScoreboardFrame();
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    Dimension frameSize = frame.getSize();
    if (frameSize.height > screenSize.height)
    {
      frameSize.height = screenSize.height;
    }
    if (frameSize.width > screenSize.width)
    {
      frameSize.width = screenSize.width;
    }
    frame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
    frame.addWindowListener(new WindowAdapter()
      {
        public void windowClosing(WindowEvent e)
        {
          System.exit(0);
        }
      });
    frame.setVisible(true);
  }
  public static void main(String[] args)
  {
    try
    {
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    }
    catch(Exception e)
    {
      e.printStackTrace();
    }
    new ScoreboardApp();
  }
}
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.GridBagLayout;
import java.awt.BorderLayout;
import javax.swing.JPanel;
import java.awt.Color;
import javax.swing.JTextField;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.Font;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.*;
import java.text.*;
public class ScoreboardFrame extends JFrame
{
  JMenuItem menuFileExit = new JMenuItem();
  JMenu menuFile = new JMenu();
  JMenuBar menuBar1 = new JMenuBar();
  BorderLayout borderLayout1 = new BorderLayout();
  JPanel panelScoreboard = new JPanel();
  GridBagLayout gridBagLayout1 = new GridBagLayout();
  JTextField txtHomeScore = new JTextField();
  JLabel jLabel1 = new JLabel();
  JLabel jLabel2 = new JLabel();
  JTextField txtGuests = new JTextField();
  JTextField txtTime = new JTextField();
  JPanel panelButtons = new JPanel();
  JButton btnTimeControl = new JButton();
  JButton btnHomeAdd = new JButton();
  JButton btnHomeSub = new JButton();
  JButton btnGuestsAdd = new JButton();
  JButton btnGuestsSub = new JButton();
  DecimalFormat df = new DecimalFormat("00");
  int homeScore = 0;
  int guestsScore = 0;
  public ScoreboardFrame()
  {
    try
    {
      jbInit();
    }
    catch(Exception e)
    {
      e.printStackTrace();
    }
  }
  private void jbInit() throws Exception
  {
    this.setJMenuBar(menuBar1);
    this.getContentPane().setLayout(borderLayout1);
    this.setSize(new Dimension(534, 300));
    this.setTitle("Basketball Scoreboard");
    menuFile.setText("File");
    panelScoreboard.setBackground(new Color(227, 82, 227));
    panelScoreboard.setLayout(gridBagLayout1);
    txtHomeScore.setText("00");
    txtHomeScore.setPreferredSize(new Dimension(100, 75));
    txtHomeScore.setFont(new Font("SansSerif", 1, 60));
    txtHomeScore.setForeground(Color.red);
    txtHomeScore.setEditable(false);
    jLabel1.setText("Home");
    jLabel1.setFont(new Font("Dialog", 1, 20));
    jLabel1.setForeground(Color.white);
    jLabel2.setText("Guests");
    jLabel2.setForeground(Color.white);
    jLabel2.setFont(new Font("Dialog", 1, 20));
    txtGuests.setText("00");
    txtGuests.setCaretColor(Color.red);
    txtGuests.setForeground(Color.red);
    txtGuests.setEditable(false);
    txtGuests.setPreferredSize(new Dimension(100, 75));
    txtGuests.setFont(new Font("SansSerif", 1, 60));
    txtTime.setText("10:00");
    txtTime.setFont(new Font("SansSerif", 1, 60));
    txtTime.setForeground(Color.red);
    txtTime.setEditable(false);
    btnTimeControl.setText("Start/Stop");
    btnTimeControl.setForeground(Color.red);
    btnTimeControl.addActionListener(new ActionListener()
      {
        public void actionPerformed(ActionEvent e)
        {
          btnTimeControl_actionPerformed(e);
        }
      });
    btnHomeAdd.setText("Home +");
    btnHomeAdd.addActionListener(new ActionListener()
      {
        public void actionPerformed(ActionEvent e)
        {
          btnHomeAdd_actionPerformed(e);
        }
      });
    btnHomeSub.setText("Home -");
    btnHomeSub.addActionListener(new ActionListener()
      {
        public void actionPerformed(ActionEvent e)
        {
          btnHomeSub_actionPerformed(e);
        }
      });
    btnGuestsAdd.setText("Guests +");
    btnGuestsAdd.addActionListener(new ActionListener()
      {
        public void actionPerformed(ActionEvent e)
        {
          btnGuestsAdd_actionPerformed(e);
        }
      });
    btnGuestsSub.setText("Guests -");
    btnGuestsSub.addActionListener(new ActionListener()
      {
        public void actionPerformed(ActionEvent e)
        {
          btnGuestsSub_actionPerformed(e);
        }
      });
    menuFileExit.setText("Exit");
    menuFileExit.addActionListener(new ActionListener()
      {
        public void actionPerformed(ActionEvent ae)
        {
          fileExit_ActionPerformed(ae);
        }
      });
    menuFile.add(menuFileExit);
    menuBar1.add(menuFile);
    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));
    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));
    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));
    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));
    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));
    this.getContentPane().add(panelScoreboard, BorderLayout.CENTER);
    panelButtons.add(btnHomeAdd, null);
    panelButtons.add(btnHomeSub, null);
    panelButtons.add(btnTimeControl, null);
    panelButtons.add(btnGuestsAdd, null);
    panelButtons.add(btnGuestsSub, null);
    this.getContentPane().add(panelButtons, BorderLayout.SOUTH);
  }
  void fileExit_ActionPerformed(ActionEvent e)
  {
    System.exit(0);
  }
  void btnTimeControl_actionPerformed(ActionEvent e)
  {
  }
  void btnHomeAdd_actionPerformed(ActionEvent e)
  {
    homeScore++;
    txtHomeScore.setText(df.format(homeScore));
  }
  void btnHomeSub_actionPerformed(ActionEvent e)
  {
    homeScore--;
    txtHomeScore.setText(df.format(homeScore));
  }
  void btnGuestsAdd_actionPerformed(ActionEvent e)
  {
    guestsScore++;
    txtGuests.setText(df.format(guestsScore));
  }
  void btnGuestsSub_actionPerformed(ActionEvent e)
  {
    guestsScore--;
    txtGuests.setText(df.format(guestsScore));
  }
}
import javax.swing.JTextField;

public class ScoreboardTimer extends Thread
{
   private int counter; // the number of seconds remaining on the timer
   private boolean isRunning; // toggle variable that controls whether the counter is decremented or not
   private JTextField timerDisplay; // reference to the GUI object displaying the timer
   // getters and setters for the variables
   public boolean toggleRunning()
   {
      // toggles the value of isRunning variable; called from click handler on/off button on frame
   }
   public void run()
   {
      while (counter > 0)
      {
         // delay one second with sleep() method
         // if clock is running, decrement counter and update display
      }
   }
}

you don't need to extend thread to make a timer, there's already two (javax.swing.Timer and java.utils.Timer) personally i prefer java.utils.Timer, but more often than not i use javax.swing.Timer to avoid conlict issues in swing applications.

if you absolutely must use a thread, you don't need to extend it... I had some code to use a Thread as a Timer; (incedently - you can't call some old Thread methods like you could in previous versions without experiencing crashes..) the method doesn't extend thread, just creates a thread and listens to its run() calls.

But, i've lost that code, and can only find some code that's very wasteful, it creates a new Thread instance every 10th of a second, and eats up the stack like some kind of.. stack monster.

Read this page:

http://www.velocityreviews.com/forums/t149589-a-good-example-of-timer-thread.html

it was actually quite difficult to find on google, i'd say 99 times out of ten people use the Timer objects rather than writing thier own.

Matt

Using the java.utils.timer class is fine. It is highly recommended NOT to use the javax.swing.timer class. It will mess up your swing ui.

really? i use it on jframes that don't really have uis (just a big Canvas/Cavnas3D); what does it mess up specifically?

When using the javax.swing.Timer class if the process kicked off by the timer is long (as in longer then several milla seconds), it is posible to see the Swing UI freeze for the amount of time it takes the process to run.

Which IMO is a bad thing.

hm it might not automatically give a dedicated thread to processes; have you tried using an actionlistener that isn't the JFrame's action listener? that shouldn't make a difference tho.

Matt

That is true. However the purpose of the javax.swing.Timer class is to work with the event-dispatching thread. The main thread that does all GUI updates. There are couple reasons NOT to use this timer for anything except very specific GUI updates.

The gui can be frozen by long process' that are started by the javax.swing.Timer. Even if a new thread is spawned by the active fired by the Timer, there can be noticable delay in the GUI.

The second reason has to do more with application design and technique then anything else. Why use the javax.swing.Timer for anything except specific GUI releasted tasks? If that Timer is being used to make a call to something like a Order Processing system at midnight every night... There is something wrong. Clearly it is a business rule that the Order Processing system get called everynight at midnight. Therefore the logic for firing the event for calling the Order Processing system needs to be in the Business Object / Service layer of the application. NOT in the GUI layer of the application, which clearly the javax.swing package is used ONLY for creating GUIs.

So what it comes down to is use either Timer class you want. If the Timer is used for very specific GUI behavior, the javax.swing.Timer should be used. All other behavior that needs a Timer should use the java.util.Timer class.

So with all that said any suggestions on changing the code to make the timer count down?

How about something like:

public class ScoreBoardThread implements Runnable
{
  private int aSeconds = 0;
  private boolean aState = false;

  public ScoreBoardThread(Integer seconds)
  {
    aSeconds = seconds;
  }

  public void run()
  {
    while(aSeconds >= 0 && aOn)
    {
      try
      {
        Thread.sleep(1000);
        aSeconds = aSeconds -1;
        // Fire an event here.  Letting the Timers listeners that the time has been updated.
      }
      catch(InterruptedException e)
      {
         // I don't know what to do with these exceptions.  I've never recieved one.  Sorry.
       }
  }

  public void setSeconds(int seconds)
  {
    aSeconds = seconds;
  }

  public int getSeconds()
  {
    return aSeconds;
  }

  public void on()
  {
    aOn = true;
    this.notify();
  }

  public void off()
  {
    aOn = false;
    this.wait();
  }
}

Now i'm not exactly sure about the wait and notify methods if they will work. I think they will. :)

BTW, its best NOT to have a reference to the GUI in your timer. It should fire events (Maybe called ScoreBoardTimerEvent...). I can show you code for that if you would like. But there should be plenty of example on the net on how to do that. Finally then you would want to make the textfield or label that displays the time to be a listener of the timer and everytime the Timer fires an event then the textfield or label will call the getSeconds method on the timer, get the seconds and then format the time as needed.

BTW x2, IMO it is better to implement Runnable then to extend thread. Threads job is to start new threads of execution. What behavior is really overriding the basic behavior of thread? Typically people are using the behavior of thread. That is where there is a Runnable interface.

What do I need to do to make this work correctly?

import javax.swing.JTextField;

public class ScoreboardTimer extends Thread
{
   private int timer; // the number of seconds remaining on the timer
   private boolean isRunning; // toggle variable that controls whether the counter is decremented or not
   private JTextField timerDisplay; // reference to the GUI object displaying the timer
   // getters and setters for the variables
   public void toggleisRunning()
{
if (isRunning == false)
{
    isRunning = true;
    // toggles the value of isRunning variable; called from click handler on/off button on frame
   }
else isRunning = false;
}
   public void run()
   {
      while (timer > 0)
      {
         try
         {
  sleep(100);
  }
  catch (InterruptedException e) { }
   timer++;
      }

        // delay one second with sleep() method
         // if clock is running, decrement counter and update display
      }
   }

Well, it looks like quite a bit needs to be done actually.

There needs to be a way to set the time that the clock needs to be set to. Maybe a setTime(int seconds) method would be nice.

The timer++ needs to be changed to timer-- since you want to decrement time, not increment time.

There needs to be a setJTextField(JTextField jTextField) method so you can update the time on the JTextField. (Now its not recommended to have a reference to a GUI Object here.)

The sleep(100) needs to be changed to sleep(1000). The sleep method takes milliseconds. It takes 1000 milliseconds to make 1 second.

It would be nice to get the state of the timer at some point to. So maybe a isRunning() method that returns a boolean would work.

There is no check on the while(timer > 0) to see if the timer is actually running. So even if someone tells the timer to stop via the toggleIsRunning method, the timer will keep running.

It would be most likely better to do a setRunning(boolean value) method then doing the toggleIsRunning() method. That way whom ever is calling the class knows exactly if they are requesting the timer to start or stop running. There isn't any guess work.

Whew, i guess that is about it. But again, i would recommend looking into the wait() and notify() methods on Thread too. AND I would again recommend to implement Runnable instead of extending Thread.

So what exact changes would you make? I am not a Java guy and am stuck on this problem and haven't a clue as to where to go or what to add or subtract to make this work.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.