import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.Timer;


@SuppressWarnings("serial")
public class OnClickLesson extends JFrame   {


    private int value = 0, clicked = 0, countdown = 1;
    private Timer timer = new Timer(1000, null);
    public OnClickLesson() {

        timer = new Timer(1000, new countDownTimer());  

        addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {        
                if(value%2 == 0) {
                    clicked++;

                    if(clicked == 2) {
                        System.out.println("even");
                        timer.start();
                    }
                }

                else if(value%2 == 1) {
                    System.out.println("odd");
                    timer.start();
                }
            }
        });

        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        pack();
        setSize(400,600);
        setLocationRelativeTo(null);
        setVisible(true);
    }

    private class countDownTimer implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {

            if (countdown == 0) {
                timer.stop();
                value++;    
            }           

            else {
                System.out.println("Countdown " + countdown--);
            }
        }
    }

    public static void main(String[] args) {    
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new OnClickLesson();
            }
        });
    }
}

Recommended Answers

All 11 Replies

Please give a proper description of what should happen ve what actually happens.

I have a variable value which tracks whether the number is odd or even.

I intialized a int value = 0;

The variable "value" is now 0, After clicking 2 times, the system will print out "even" and after one second, the variable "value" will be incremented by 1;

The variable "value" is now 1, it's odd number so it will print out, "odd" and after one second, the variable value will be incremented by 1;

The variable "value" is now 2, it's even number so it will again allow the user to onClick on the JFrame 2 times. After clicking 2 times, the system will print out "even" and after one second, the variable "value" will be incremented by 1;

The variable "value" is now 3, it's odd number so it will print out, "odd" and after one second, the variable value will be incremented by 1;

and so on...

This goes on until I close the program.

Is that the desired or the actual behaviour? What's wrong with it?

that is my desired behaviour. but my actual behaviour after running my codes is that, after I clicked 2 times on the JFrame, it prints out "even" and the countdown starts and after that it never prints out "odd" even though I already increment the "value" in my Timer class

Countdown starts at 1 and decrements in the timer, but seems never to be reset, so it hits zero exactly once, then continues -1, -2, -3 etc. Keep going 2^32 times and you should get back to zero again!

so I have to reset the timer after I stop it? like this?

if (countdown == 0) {
    timer.stop();
    value++;    
    timer.restart();
}

No. The Timer looks OK. It's the countdown variable that looks wrong. Trying printing it inside the Timer's actionPerformed and you'll see what I mean.

sorry i still don't get what you mean.

I printed it out

private class countDownTimer implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {

            if (countdown == 0) {
                timer.stop();
                value++;

            }           
            else {
                System.out.println("Countdown " + countdown--);
            }
            System.out.println(countdown);
        }
    }

and my output is this

even
Countdown 1
0
0

Is it possible that you can suggest what should I change to achieve my desired results?

After printing "even" are you clicking twice more to get to the "odd" case? (I notice you don't mention that in your descritpion of the desired functionality)

(I just ran your code) - as well as the countdown variable, you also have a problem with clicked. You start the "even" timer when clicked == 2, but that only happens once because clicked goes on to 3, 4, 5, etc.

Put a big print statement at the start of both actionPerformed methods. Print all the relevant variables (value, clicked, countdown). Then run then code one click at a time and see how your variables are changing. That way you will be able to see where it's deviating from the desired behaviour.

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.