Solved

Simple Timer

Posted on 2007-11-28
17
253 Views
Last Modified: 2010-04-21
I am using a mouseListener to detect mouse movement on a page (code attached).  I would like a simple timer that counts down from X seconds, but resets every time the mouse is moved.  For example, it counts down from 45 seconds, but resets back to 45 and starts the countdown again when the mouse moves.  The applet code is attached as a snippet.  

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
 
/*
  <applet code="MouseEvents" width=300 height=100>
  </applet>
*/
 
public class MouseEvents extends Applet
	implements MouseListener, MouseMotionListener {
	
	String msg = " ";
	int mouseX = 0, mouseY = 0;
 
	public void init() {
		addMouseListener(this);
		addMouseMotionListener(this);
	}
 
	public void mouseClicked(MouseEvent me) {
		mouseX= 0;
		mouseY = 10;
		repaint();
	}
 
	public void mouseEntered(MouseEvent me) {
		mouseX = 0;
		mouseY = 10;
		repaint();
	}
 
	public void mouseExited(MouseEvent me) {
		mouseX = 0;
		mouseY = 10;
		repaint();
	}
 
	public void mousePressed(MouseEvent me) {
		mouseX = me.getX();
		mouseY = me.getY();
		repaint();
	}
 
	public void mouseReleased(MouseEvent me) {
		mouseX = me.getX();
		mouseY = me.getY();
		repaint();
	}
 
	public void mouseDragged(MouseEvent me) {
		mouseX = me.getX();
		mouseY = me.getY();
		showStatus("Dragging mouse at " + mouseX + ", " + mouseY);
		repaint();
	}
 
	public void mouseMoved(MouseEvent me) {
		showStatus("Moving mouse at " + me.getX() + ", " + me.getY());
	}
 
	public void paint(Graphics g) {
		g.drawString(msg, mouseX, mouseY);
	}
}

Open in new window

0
Comment
Question by:gameness
  • 8
  • 8
17 Comments
 
LVL 13

Accepted Solution

by:
Bart Cremers earned 500 total points
ID: 20367649
You can use a Timer for this:
import javax.swing.*;
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
 
/*
  <applet code="MouseEvents" width=300 height=100>
  </applet>
*/
 
public class MouseEvents extends Applet
    implements MouseListener, MouseMotionListener, ActionListener {
    Timer timer;
    int mouseX = 0, mouseY = 0;
    private int count;
 
    public void init() {
        addMouseListener(this);
        addMouseMotionListener(this);
 
        timer = new Timer(1000, this);
        timer.start();
        count = 45;
    }
 
    public void mouseClicked(MouseEvent me) {
        mouseX = 0;
        mouseY = 10;
        repaint();
    }
 
    public void mouseEntered(MouseEvent me) {
        mouseX = 0;
        mouseY = 10;
        repaint();
    }
 
    public void mouseExited(MouseEvent me) {
        mouseX = 0;
        mouseY = 10;
        repaint();
    }
 
    public void mousePressed(MouseEvent me) {
        mouseX = me.getX();
        mouseY = me.getY();
        repaint();
    }
 
    public void mouseReleased(MouseEvent me) {
        mouseX = me.getX();
        mouseY = me.getY();
        repaint();
    }
 
    public void mouseDragged(MouseEvent me) {
        mouseX = me.getX();
        mouseY = me.getY();
        showStatus("Dragging mouse at " + mouseX + ", " + mouseY);
        repaint();
    }
 
    public void mouseMoved(MouseEvent me) {
        count = 45;
        timer.restart();
        showStatus("Moving mouse at " + me.getX() + ", " + me.getY());
        repaint();
    }
 
    public void paint(Graphics g) {
        g.drawString(String.valueOf(count), mouseX, mouseY);
    }
 
    public void actionPerformed(ActionEvent e) {
        count = Math.max(0, --count);
        repaint();
    }
}

Open in new window

0
 

Author Closing Comment

by:gameness
ID: 31411487
Thank you!!!!!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 20367744
What does the countdown do?
0
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 

Author Comment

by:gameness
ID: 20368505
Ultimately, I would like the user to be redirected to another page after X seconds of inactivity have passed.  This timer works great...  but, of course, when I compile it using the older JDK so it will run on MSJVM (like in my last issue), I get an error :(

This MSJVM is a JOKE!
0
 
LVL 13

Expert Comment

by:Bart Cremers
ID: 20368540
Why do you want it to run on MSJVM. Microsoft will stop spreading it anyway on december 31 this year. Most computers these days have a Sun JRE on the system anyway, so you could easily go for a Sun 1.5 or 1.6
0
 
LVL 13

Expert Comment

by:Bart Cremers
ID: 20368546
0
 

Author Comment

by:gameness
ID: 20368568
Bart,

I know.  If it were up to me, all our machines would be using the Sun JRE.  However, one application that the company is using will not work properly on the current Sun JRE -- it only works in MSJVM.  Well, I should rephrase that:

The applet seems to work fine, but the JavaScript button that is supposed to export the applet data to an Excel sheet throws an error when running on Sun JRE.
0
 
LVL 13

Expert Comment

by:Bart Cremers
ID: 20368667
Here's a 1.1 implementation:

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;

/*
  <applet code="MouseEvents" width=300 height=100>
  </applet>
*/

public class MouseEvents extends Applet
    implements MouseListener, MouseMotionListener, ActionListener {
    Timer timer;
    int mouseX = 0, mouseY = 0;
    private int count;

    public void init() {
        addMouseListener(this);
        addMouseMotionListener(this);

        timer = new Timer(1000, this);
        timer.start();
        count = 45;
    }

    public void mouseClicked(MouseEvent me) {
        mouseX = 0;
        mouseY = 10;
        repaint();
    }

    public void mouseEntered(MouseEvent me) {
        mouseX = 0;
        mouseY = 10;
        repaint();
    }

    public void mouseExited(MouseEvent me) {
        mouseX = 0;
        mouseY = 10;
        repaint();
    }

    public void mousePressed(MouseEvent me) {
        mouseX = me.getX();
        mouseY = me.getY();
        repaint();
    }

    public void mouseReleased(MouseEvent me) {
        mouseX = me.getX();
        mouseY = me.getY();
        repaint();
    }

    public void mouseDragged(MouseEvent me) {
        mouseX = me.getX();
        mouseY = me.getY();
        showStatus("Dragging mouse at " + mouseX + ", " + mouseY);
        repaint();
    }

    public void mouseMoved(MouseEvent me) {
        count = 45;
        timer.restart();
        showStatus("Moving mouse at " + me.getX() + ", " + me.getY());
        repaint();
    }

    public void paint(Graphics g) {
        g.drawString(String.valueOf(count), mouseX, mouseY);
    }

    public void actionPerformed(ActionEvent e) {
        count = Math.max(0, --count);
        repaint();
    }

    private class Timer extends Thread {
        private final int interval;
        private final ActionListener listener;
        private volatile boolean run = true;

        public Timer(int interval, ActionListener listener) {
            this.interval = interval;
            this.listener = listener;
        }

        public void restart() {
            interrupt();
        }

        public void stopTimer() {
            run = false;
            interrupt();
        }

        public void run() {
            while(run) {
                try {
                    Thread.sleep(interval);
                    listener.actionPerformed(new ActionEvent(this, 0, null));
                } catch (InterruptedException e) {
                    interrupted();
                }
            }
        }
    }
}
0
 

Author Comment

by:gameness
ID: 20368719
Bart,

You're awesome!  Thank you.  Here's a simple one for you:  how do I redirect the user to a different URL once the timer reaches 0?
0
 
LVL 13

Expert Comment

by:Bart Cremers
ID: 20368764
           try {
                getAppletContext().showDocument(new URL("http://www.google.com"));
            } catch (MalformedURLException e1) {
                // Ignore
            }



http://tns-www.lcs.mit.edu/manuals/java-api-1.1beta2/api/java.applet.AppletContext.html#showDocument(java.net.URL,%20java.lang.String)
0
 

Author Comment

by:gameness
ID: 20368805
I get two errors:
----------------------
MouseEvents.java:79: cannot resolve symbol
symbol  : class URL
location: class MouseEvents
                                getAppletContext().showDoc
www.google.com"));

MouseEvents.java:81: cannot resolve symbol
symbol  : class MalformedURLException
location: class MouseEvents
                        catch (MalformedURLException e1)
                               ^
2 errors
----------------------
I may be putting it in the wrong place...  I'm putting it within an IF statement:
public void actionPerformed(ActionEvent e) {
        count = Math.max(0, --count);
        repaint();

            if (count == 0) try
                  {
                        getAppletContext().showDocument(new URL("http://www.google.com"));
                  }
                  catch (MalformedURLException e1)
                  {
                        // Ignore
                  }
    }
0
 
LVL 13

Expert Comment

by:Bart Cremers
ID: 20368845
Looks like a copy/paste problem to me. The compiler output shows some strange code.
0
 

Author Comment

by:gameness
ID: 20368878
I just tried again, and typed it this time.  Are the strange symbols you're talking about the ^ ?  I am compiling from a CMD window, I'm not sure if that has anything to do with it.  It's the only compiler I have (other than the current JDK).  Let me mess with it a bit.
0
 

Author Comment

by:gameness
ID: 20368888
Am I missing an "import" line?
0
 
LVL 13

Expert Comment

by:Bart Cremers
ID: 20369028
No extra imports are needed here.
0
 
LVL 13

Expert Comment

by:Bart Cremers
ID: 20369046
Wait! These imports are need:

import java.net.URL;
import java.net.MalformedURLException;
0
 

Author Comment

by:gameness
ID: 20369096
It worked!  Thank you!  I'm about 98% done with this darn thing.  I really appreciate the help, and you're taking the time to explain things.  
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
numbers ascending pyramid 101 196
eclipse buid path vs tomcat lib path 10 24
Adding multiple JVM environments to RedHat 6 7 23
Java Restore security prompts not working 10 13
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question