Not taking my mouse clicks fast enough

Ok well i want to make this simple game, and basically all it does is count how many times you clicked, and i made it, but its running too slow. The problem is that its not counting all the clicks, it might have something to do with the way i coded it but idk…

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.applet.*;

public class Countdown extends Applet implements Runnable, MouseListener
{
 Thread th;  
 int i = 200;
 int count = -2;
public void init()
{
    setSize(300,300);
    setBackground(Color.black);
    addMouseListener(this);
}
public void paint(Graphics g)
{
 g.setColor(Color.gray);
 g.drawString("SECONDS LEFT "+i,30,30);
 if(i == 0)
 {
 g.setColor(Color.blue);
 g.drawString("YOU CLICKED "+count+" TIMES!",10,200);
 }
 if(i <= 0)
 {
     g.setColor(Color.red);
     g.drawRect(10,5,150,150);
     stop();
 }
}
public void run()
    {
        while(th == Thread.currentThread())
        {
            i--;
            repaint();
            try{Thread.sleep(50); }
            catch(InterruptedException e){}
        }
    }
    public void start()
    {
        
        if(th == null) th = new Thread(this);
        th.start();
    }
    public void stop()
    {  
     if(th != null) th = null;
    }
    public void mousePressed(MouseEvent e) {
   
    }

    public void mouseReleased(MouseEvent e) {
      
    }

    public void mouseEntered(MouseEvent e) {
       
    }

    public void mouseExited(MouseEvent e) {
       
    }

    public void mouseClicked(MouseEvent e) {
       count++;
       }

ps. im not new to java its just i dont know why its not getting my clicks fast enought and i want to know what i can do…

mouseClicked is only triggered if you click at the very same spot. So… put the count++ into mousePressed.

  • agrees with oNyx *

If you just hold down the mouse button, mousePressed will be called more than once. So it will count holding down mouse presses as more than one click.

Why does the count variable start at -2 instead of at 0?

You may want to look into the MouseEvent.getClickCount() method for detecting double clicks (or triple clicks or n-clicks). I don’t think it would help you directly for this example though because each click should have a seperate MouseEvent anyways.

as I see it solution would be to do:

if (!clicked) {
clicks++;
clicked = true;
}

in mousePressed() and

clicked = false;

in mouseReleased()

Thats isn’t true, there’s no auto-repeat like there is for keyPressed on some platforms.

You can simply use:


public void mousePressed(MouseEvent e) {
    count++;
}

As I suspect by now you’ve found out :slight_smile:

Kev

PS: “YOU CLICKED 125 TIMES!”