Results 1 to 6 of 6
  1. #1
    ale626 is offline Member
    Join Date
    May 2012
    Posts
    27
    Rep Power
    0

    Default Timer + Mouse Click

    I'm trying to make a mouse click counter and i ran into a few problems. I want to have a countdown from 3 to 0 then give the user 10 seconds to click and at the bottom to count the clicks, but once the countdown reaches 0 i want it to stop displaying. i still have to add in a reset for the start/reset button, but ill worry about that later. Also If someone could suggest improvements perhaps the actionlisteners and mouseevent. Thanks !

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class Gui extends JFrame{
    
    	private String clickCount;
    	private JLabel clickArea;
    	private JLabel statusBar;
    	private JButton startButton;
    	private Timer timer;
    	private int count = 0;
    	private int countDown = 3;
    	
    	public Gui(){
    		super("Click Count");
    		
    		statusBar = new JLabel("Start when ready!");
    		statusBar.setOpaque(true);
    		statusBar.setBackground(Color.CYAN);
    		add(statusBar, BorderLayout.SOUTH);
    		
    		clickArea = new JLabel();
    		clickArea.setHorizontalAlignment(SwingConstants.CENTER);
    		add(clickArea, BorderLayout.CENTER);
    		startButton = new JButton("Start/Reset");
    		startButton.setFont(new Font("Dialog", 1, 24));
    		startButton.setPreferredSize(new Dimension(400,100));
    		startButton.addActionListener(new ActionListener(){
    			public void actionPerformed(ActionEvent event){
    				count = 0;
    				countDown = 3;
    				timer.start();
    			}
    		});
    		add(startButton, BorderLayout.NORTH);
    		
    		
    		
    		timer = new Timer(1000,new ActionListener(){
    			public void actionPerformed(ActionEvent evt){
    				if(countDown <= 0){
    					addMouseListener(new MouseAdapter(){
    						public void mouseReleased(MouseEvent event){
    							count++;
    							if(countDown >= 0){
    								clickCount = String.format("you clicked %d ", count);
    								statusBar.setText(clickCount);
    							} else{
    								clickArea.setText("CLICK!");
    							}
    						}
    					});
    				}
    				clickArea.setText(String.valueOf(countDown));
    				countDown--;
    				if(countDown == -10){
    					timer.stop();
    				}
    				
    				}
    		});
    	}
    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,236
    Rep Power
    19

    Default Re: Timer + Mouse Click

    Moved from New to Java

    I don't think you want to keep adding another and yet another MouseListener every time the Timer fires. also, I wouldn't expect that adding a MouseListener directly to the top level window would help.

    Here's a suggestion: write a class that doesn't extend JFrame. Construct a JPanel, JLabel, JButtons etc. and add listeners to the component that should listen for events. Then add that to a JFrame for display.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    ale626 is offline Member
    Join Date
    May 2012
    Posts
    27
    Rep Power
    0

    Default Re: Timer + Mouse Click

    THANKS!!! i was confused by your answer when i read it at first. I began writing a reply to it and as i was doing so i decided to follow your advice and started doing it piece by piece and i came to a good result!
    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Font;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.SwingConstants;
    import javax.swing.Timer;
    
    
    public class Components {
    	
    	private JFrame frame;
    	private String clickCount;
    	private JLabel clickArea;
    	private JLabel statusBar;
    	private JButton startButton;
    	private Timer timer;
    	private int count = 0;
    	private int countDown = 3;
    	private boolean stop = false;
    	
    	Components(){
    		
    		frame = new JFrame();
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setSize(400,400);
    		frame.setVisible(true);
    		
    		statusBar = new JLabel("Start when ready!");
    		statusBar.setOpaque(true);
    		statusBar.setBackground(Color.CYAN);
    		frame.add(statusBar, BorderLayout.SOUTH);
    		
    		clickArea = new JLabel();
    		frame.add(clickArea, BorderLayout.CENTER);
    		clickArea.setHorizontalAlignment(SwingConstants.CENTER);
    		clickArea.setFont(new Font("Dialog", 1, 100));
    		clickArea.addMouseListener(new MouseAdapter(){
    			public void mouseReleased(MouseEvent event){
    				count++;
    				if(countDown <= 0 && countDown >= -10 && stop == false){
    					clickCount = String.format("you clicked %d ", count);
    					statusBar.setText(clickCount);
    				}				
    			}
    		});
    		
    		startButton = new JButton("Start/Reset");
    		startButton.setFont(new Font("Dialog", 1, 24));
    		startButton.setPreferredSize(new Dimension(400,100));
    		startButton.addActionListener(new ActionListener(){
    			public void actionPerformed(ActionEvent event){
    				stop = false;
    				count = 0;
    				statusBar.setText("you clicked zero times");
    				countDown = 3;
    				timer.start();
    			}
    		});
    		frame.add(startButton, BorderLayout.NORTH);
    		
    		timer = new Timer(1000, new ActionListener(){
    			public void actionPerformed(ActionEvent event) {
    				
    				
    				
    				if(countDown == 0){
    					count = 0;							
    				}				
    				if(countDown>=0){
    					clickArea.setText(String.valueOf(countDown));
    				}else{
    					clickArea.setText("CLICK!");
    				}
    				countDown--;
    				if(countDown == -10){
    					clickArea.setText("STOP!");
    					stop = true;
    					timer.stop();
    				}
    			}
    			
    		});
    	}
    }
    its not completely done yet, but i feel i can touch it up tomorrow after i sleep thanks!!

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,236
    Rep Power
    19

    Default Re: Timer + Mouse Click

    Whenever possible, all content should be added before a top level window is made visible.

    When due to unusual requirements this is not possible, it is necessary to call revalidate() and repaint() after adding/removing components to/from a Container.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    ale626 is offline Member
    Join Date
    May 2012
    Posts
    27
    Rep Power
    0

    Default Re: Timer + Mouse Click

    So should i just make my jframe visible at the end of my code?

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,236
    Rep Power
    19

    Default Re: Timer + Mouse Click

    What do you think?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Applet+Mouse click
    By Verbiage in forum New To Java
    Replies: 6
    Last Post: 04-18-2012, 09:00 PM
  2. Mouse Click not working.
    By itcmanish in forum AWT / Swing
    Replies: 1
    Last Post: 01-12-2012, 03:25 PM
  3. Mouse click interrupting animation
    By rolledback in forum Java 2D
    Replies: 17
    Last Post: 06-16-2011, 11:01 PM
  4. change color on mouse click
    By hannerz06 in forum New To Java
    Replies: 3
    Last Post: 03-31-2010, 09:46 PM
  5. mouse click alert
    By amir in forum AWT / Swing
    Replies: 1
    Last Post: 08-05-2008, 10:42 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •