Skip to main content
gamedev.net
🔒 Locked

[java] Making an "Egg Timer" Program

Started by guyver23 3 replies 4.8k views
Original Post
guyver23
guyver23
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! =)
Haffa
Haffa
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);
5MinuteGaming
5MinuteGaming
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]
guyver23
guyver23
Thanks guys! ^_^

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.