Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6611979
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:02:28+00:00 2026-05-25T20:02:28+00:00

Consider you have the following code in a Timer : It’s main goal is

  • 0

Consider you have the following code in a Timer:
It’s main goal is to count down the seconds and to show it on GUI, which is Swing based.
The Timer is part of the game and is used for a decision of who is the winner by taking the user who reached a solution first.

Action updateClockAction = new AbstractAction() {

    public void actionPerformed(ActionEvent e) {

        JLabel secLabel = m_GameApplet.GetJpanelStartNetGame().GetJlabelSeconds();
        secLabel.setFont(new java.awt.Font("Lucida Handwriting", 1, 36));
        secLabel.setForeground(Color.red);
        secLabel.setText(Integer.toString(m_TimerSeconds));
        if (m_TimerSeconds > 0) {
            m_TimerSeconds--;
        } else if (m_TimerSeconds == 0) {
            m_Timer.stop();
            m_GameApplet.GetJpanelStartNetGame().GetJlabelSeconds().setText("0");
            m_GameApplet.GetJpanelStartNetGame().GetJbuttonFinish().setVisible(false);
            //Checking whether time ended for both players and no solution was recieved
            if (!m_WasGameDecisived) {
                System.out.println("Tie - No one had a solution in the given time");
            }
        }
    }
};
m_Timer  = new Timer(1000, updateClockAction);

Now, I have two main packages in the client implementation
A.GUI – hold all the swing Jpanels etc.
B.LogicEngine

Now in LogicEngine I have a class that is called GameManager- that should manage and store information about the game.

My Current implementation in general is like this:
I have a JpanelMainGame which is on the GUI Package
JpanelMainGame contains JPanelGameBoard which has reference(or in other word contains) an instance of GameManger, which is on different package – The LogicEngine package.

So my questions are these:

  1. Where does all of the timer definition code above should be placed?

    A. In the JpanelMainGame? (JpanelMainGame should have a reference to it).

    B. In GameManger as a part of a data of the game

  2. In each solution you give, How should I access all the label info from within the anonymous inner class? As I can only access member from the outer class with command: OuterClass.this.member.

  3. Any comments about current design.

Thank you very much

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T20:02:28+00:00Added an answer on May 25, 2026 at 8:02 pm

    Well not to answer questions with questions, but…

    • How often is Timer used?
    • Is Timer coded to be general use, or specific to a certain class?

    If the Timer is general use, then I would say it belongs in the LogicEngine package tree somewhere. However, if Timer can only be used in a GUI element, then it belongs in the GUI package tree.

    As a general rule of thumb (not to bang on the Agile drum too hard), you should only code what makes sense now but not be afraid to change it later.

    Example: You are making an anonymous inner class of the type AbstractAction() in your code. This is just fine (although I shy away from anon classes) if the updateClockAction variable is used simply. But once your coding leads you to the need to cross the boundries of updateClockAction (via OuterClass.this.member) then I assert its time for a bit of refactoring: extract this inner class and make it a fully qualified class. This way, you can have the proper amount of control over the updateClockAction object’s internal state (using the cstr, getters, etc)

    EDIT: Added requested example

    public class Testing {
        private Timer timer; /* INIT this from somewhere.... */
    
        public void myFunction() {
        /* 60 Seconds */
        long countDownTimeSEC = 60; 
        /* convert to Miliseconds */
        long countDownTimeMS = 1000 * countDownTimeSEC; 
    
        /* Get ref to label */
        JLabel label = m_GameApplet.GetJpanelStartNetGame().GetJlabelSeconds(); 
    
        /* Set once */
        label.setFont(new java.awt.Font("Lucida Handwriting", 1, 36)); 
        label.setForeground(Color.red);
    
        /* Set initial time */
        label.setText(Long.toString(countDownTimeSEC)); 
    
        /* Get ref to button */
        JButton button = m_GameApplet.GetJpanelStartNetGame().GetJbuttonFinish(); 
    
        /* Set up post Count Down list */
        ArrayList<AbstractAction> actsWhenComplete = new ArrayList<AbstractAction>();
    
        /* instantiate countdown object */
        CountDownClockAction cdca = new CountDownClockAction(label, countDownTimeMS, actsWhenComplete);
        this.timer  = new Timer(1000, cdca);
    
        /* Now that we have a timer, add the post Count Down action(s) to the  post Count Down list */
        actsWhenComplete.add(new CountDownFinishAction(label, button, this.timer));
    
    
        /* Finally, kick off the timer */
        this.timer.start();
    }
    
    public static class CountDownClockAction extends AbstractAction {
        private static final long serialVersionUID = 1L;
        private final JLabel labelToUpdate;
        private final long startMS;
        private long currentMS;
        private long timeMark;
        private final ArrayList<AbstractAction> actionsToExeWhenComplete;
        private boolean actionsExedFlag;
    
        public CountDownClockAction(
                final JLabel labelToUpdate, 
                final long startMS, 
                ArrayList<AbstractAction> actionsToExeWhenComplete
        ) {
            super();
            this.labelToUpdate = labelToUpdate;
            this.startMS = startMS;
            this.currentMS = startMS;
            this.timeMark = 0;
            this.actionsExedFlag = false;
            this.actionsToExeWhenComplete = actionsToExeWhenComplete;
        }
    
        @Override
        public void actionPerformed(final ActionEvent e) {
            /* First time firing */
            if (this.timeMark == 0) 
                this.timeMark = System.currentTimeMillis();
    
            /* Although the Timer object was set to 1000ms intervals, 
             * the UpdateClockAction doesn't know this, nor should it
             * since > or < 1000ms intervals could happen to do the fact that
             * the JVM nor the OS have perfectly accurate timing, nor do they
             * have instantaneous code execution properties.
             * So, we should see what the *real* time diff is...
             */
            long timeDelta = System.currentTimeMillis() - this.timeMark;
    
            /* Allow for the label to be null */
            if (this.labelToUpdate != null)
                labelToUpdate.setText(Long.toString((long)(currentMS / 1000)));
    
            if (currentMS > 0) {
                currentMS -= timeDelta;
    
            } else if (currentMS <= 0 && this.actionsExedFlag == false) {
                /* Ensure actions only fired once */
                this.actionsExedFlag = true;
                /* Allow for the label to be null */            
                if (this.actionsToExeWhenComplete != null)
                    for (AbstractAction aa: this.actionsToExeWhenComplete)
                    aa.actionPerformed(e);
            }
    
            /* Finally, update timeMark for next calls */
            this.timeMark = System.currentTimeMillis();
        }
    }
    
    public static class CountDownFinishAction extends AbstractAction {
        private final JLabel labelToUpdate;
        private final JButton buttonToUpdate;
        private final Timer timerToStop;
    
        public CountDownFinishAction(
                JLabel labelToUpdate,
                JButton buttonToUpdate, 
                Timer timerToStop
        ) {
            super();
            this.labelToUpdate = labelToUpdate;
            this.buttonToUpdate = buttonToUpdate;
            this.timerToStop = timerToStop;
        }
    
        @Override
        public void actionPerformed(final ActionEvent e) {
            /* Perform actions, allowing for items to be null */
            if (this.labelToUpdate != null)
                this.labelToUpdate.setText("0");
    
            if (this.buttonToUpdate != null)
                    this.buttonToUpdate.setVisible(false);
    
                if (this.timerToStop != null)
                    this.timerToStop.stop();
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider you have the following code implementation of a timer in a net game:
I've got some basic questions about C++. Consider the following code in which I
Please consider the following piece of code: int main() { typedef boost::ptr_vector<int> ptr_vector; ptr_vector
Consider the following code, I have a textblock that is an on/off button in
Consider following assumptions: I have Java 5.0 Web Application for which I'm considering to
Consider I have the following text in a UILabel (a long line of dynamic
Consider I have the following interface: public interface A { public void b(); }
From couple of days i am thinking of a following scenario Consider I have
Lets consider that I have a public property called AvatarSize like the following, public
Consider the following situation: We have two Localizable.string files, one in en.lproj and one

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.