Jump to content
  • Advertisement
Sign in to follow this  
guyver23

[java] Making an "Egg Timer" Program

This topic is 4485 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello! I am interested in creating a small Java application that accepts a time in minutes and beeps/plays a sound when the time has ellapsed. Here is what I know about Java: * All the general fundamentals of Java (for, if-else, etc.) * How to make and use most Swing components (JFrame, sliders, trees, etc.) I do not know much at all about using threads that are Swing-safe, and I do not know how to load and play a sound. Could someone please tell me if there is an easy solution to my needs (I will learn the sound API if I have to, but it seems silly since this is a trivial need). Thanks! =)

Share this post


Link to post
Share on other sites
Advertisement
You can use the java.awt.Toolkit class to beep the pc speaker. An easy way to schedule the sound would be to use java.util.Timer.

The following code would play a beep after 5 seconds:

TimerTask task = new TimerTask() {
public void run() {
Toolkit.getDefaultToolkit().beep();
}};
Timer t = new Timer();
t.schedule(task, 5000);

Share this post


Link to post
Share on other sites
I've always wanted to make a quick egg timer myself. So I took Haffa's use of the java.util.Timer and java.util.TimerTask and made it into a nice quick command line class file.





/*
* EggTimer.java
*
* Permission is granted by the author to modify, or copy/paste
* this program for any use. No Guarrentees!
*/


import java.awt.*;
import java.util.*;
import java.net.*;
import java.applet.*;
import java.io.*;

/**
* This is a java application to simulate and Egg Timer.
*
* @author: 5MinuteGaming (http://fairmi73.tripod.com/) (webm5mingames@yahoo.com)
*/

public class EggTimer extends TimerTask {

private static AudioClip buzzer = null;
private static long stime = 0;

public static void main(String args[]) {

if( args.length != 2 ) {
System.err.println(" Usage: java EggTimer <url | filename> <time in seconds>" );
}else{

try {
buzzer = java.applet.Applet.newAudioClip( (new File(args[0])).toURL() );

stime = (long)(Double.parseDouble(args[1]) * 1000);

if( stime < 0 ) {
throw new NumberFormatException();
}

Timer t = new Timer();
t.schedule(new EggTimer(), stime);

}catch( MalformedURLException murle) {
System.err.println( "URL Error: There was a problem resolving the audio clip URL." );
System.err.println( murle.getMessage() );
System.exit(1);
}catch( NumberFormatException nfe ) {
System.err.println( "Number Error: " + nfe.getMessage() );
System.exit(1);
}catch( Exception e ) {
System.err.println( "Unknown Error: There was an error in initialization" );
System.exit(1);
}

}
}


public void run() {
try{
buzzer.play();
}catch( Exception e ) {
System.err.println( "Unknown Error: Error attempting to play the audio file." );
System.exit(1);
}
}

}







Still need to paly with it a bit to get it to run in the background and end once the time is exceeded. But it's tested and it works.

Edit: Ok got it to work in the background on Windows using a batch file. The command line I used was as follows: start javaw EggTimer "chimes.wav" 5

Still have a problem getting it to shutdown anyone got any ideas about that?

[Edited by - 5MinuteGaming on April 6, 2006 12:20:18 PM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Made one over a year ago.

Screenshot:
http://kaioa.com/k/pt2.png

Double clickable jar:
http://kaioa.com/k/pt2d.jar

Usage:
-1234=12 minutes 34 seconds countdown
-return again for stop
-click or keypress (if focussed) for stopping the alert
-rightclick (or whatever) for context menu (where you can toggle sound, clamp on screen and always on top [1.5+ only])
-there are also 3 command line switches: -nosound, -noclamp and -noalwaysontop

I used TimerTask for scheduling a time check every 50msec. If more than a second passed I update the display and trigger the alert if necessary.

I did it like that for preventing drift. Eg if you schedule it once a second (for updating the display) and assume that always a second passed, you'll get some small error which adds up all the time.

Say you get 1050 msec instead of 1000 msec, then you would end up with 2835 seconds instead of 2700 (for 45 minutes). So... I went with checking the time ~20 times a second instead. Seems ineffective, but its *so* little to do and basically creates zero cpu usage and yet its accurate enough.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!