import javax.sound.sampled.*;
import java.io.*; //Zum einlesen der Tastatur über BufferedReader und InputStreamReader
class Eieruhr{
int sekunden,minuten;
public static int beenden;
Thread zählerThread;
void eingabe() {
System.out.println( );
System.out.println("Die akustische Eieruhr [made by BMo]" );
System.out.println( );
System.out.println( );
System.out.println("Geben Sie die gewuenschte Zeitdauer ein " );
System.out.println( );
System.out.print("[Minuten] " );
minuten=readint();
System.out.print("[Sekunden] " );
sekunden=readint();
System.out.println( );
if (minuten==0&&sekunden==0) {
System.out.println("Error --- Programm beendet" );
System.exit(0);
}
}
void zeit() {
zählerThread = new Thread();
try {
zählerThread.sleep(1000);//Pause für 1Sekunde aus anderen Programmen von mir erdacht
} catch (InterruptedException e){}
}
void ablauf() {
eingabe();
do {
zeit();
sekunden-=1;
if (sekunden==-1 ) {
minuten-=1;
sekunden=59;
}
anzeige();
} while (minuten!=00||sekunden!=00);
ende();
}
void anzeige() {
if ( sekunden<=9&&minuten<=9 ) {
System.out.print("\rNoch 0"+minuten+" Minuten und 0"+sekunden+" Sekunden" );
}
if ( sekunden>=10&&minuten<=9 ) {
System.out.print("\rNoch 0"+minuten+" Minuten und "+sekunden+" Sekunden");
}
if ( sekunden<=9&&minuten>=10 ) {
System.out.print("\rNoch "+minuten+" Minuten und 0"+sekunden+" Sekunden" );
}
if ( sekunden>=10&&minuten>=10 ) {
System.out.print("\rNoch "+minuten+" Minuten und "+sekunden+" Sekunden" );
}
}
void ende() {
System.out.println( );
System.out.println("Fertig ");
System.out.println(" [zum Beenden Enter druecken]" );
}
/** Creates a new instance of Eieruhr */
public Eieruhr() {
}
public static void main ( String[]args ) {
Eieruhr zeitablauf=new Eieruhr();
zeitablauf.ablauf();
zeitablauf.beenden=1;
//______________________________________________________________________________
//Sound
try{
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File("C:/WINDOWS/Media/EE_REV.wav"));
AudioFormat af = audioInputStream.getFormat();
int size = (int) (af.getFrameSize() * audioInputStream.getFrameLength());
byte[] audio = new byte[size];
DataLine.Info info = new DataLine.Info(Clip.class, af, size);
audioInputStream.read(audio, 0, size);
Clip clip = (Clip) AudioSystem.getLine(info);
clip.open(af, audio, 0, size);
Beender ende = new Beender();
ende.start();
do {
clip.start();
while (clip.isActive()){}
clip.stop();
} while (zeitablauf.beenden==1);
clip.close();
System.exit(0);
}
//-----------------------------------------------------------------------------
//Wenn Sound nicht vorhanden ist (Windows NT)
catch(Exception e){
try {
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File("C:/WINNT/Media/Der Microsoft-Sound.wav"));
AudioFormat af = audioInputStream.getFormat();
int size = (int) (af.getFrameSize() * audioInputStream.getFrameLength());
byte[] audio = new byte[size];
DataLine.Info info = new DataLine.Info(Clip.class, af, size);
audioInputStream.read(audio, 0, size);
Clip clip = (Clip) AudioSystem.getLine(info);
clip.open(af, audio, 0, size);
Beender ende = new Beender();
ende.start();
do {
clip.start();
while (clip.isActive()){}
clip.stop();
} while (zeitablauf.beenden==1);
clip.close();
System.exit(0);
}
//-----------------------------------------------------------------------------
//Wenn Sound nicht vorhanden ist
catch(Exception x){
try {
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File("C:/Windows/Media/Der Microsoft-Sound.wav"));
AudioFormat af = audioInputStream.getFormat();
int size = (int) (af.getFrameSize() * audioInputStream.getFrameLength());
byte[] audio = new byte[size];
DataLine.Info info = new DataLine.Info(Clip.class, af, size);
audioInputStream.read(audio, 0, size);
Clip clip = (Clip) AudioSystem.getLine(info);
clip.open(af, audio, 0, size);
Beender ende = new Beender();
ende.start();
do {
clip.start();
while (clip.isActive()){}
clip.stop();
} while (zeitablauf.beenden==1);
clip.close();
System.exit(0);
}
//-----------------------------------------------------------------------------
//Falls keiner der Sounds vorhanden ist
catch(Exception y){
y.printStackTrace();
System.out.println("[Soundfile nicht vorhanden]" );
System.exit(0);
}
}
}
//______________________________________________________________________________
}
/**
* Wartet auf Eingabe von der Komandozeile,
* liefert ein Wert vom Grundtyp int
*/
public static int readint() {
BufferedReader reader = new
BufferedReader(new InputStreamReader(System.in));
String line="";
System.out.flush();
try {
line=reader.readLine();
}
catch (Exception e) { System.out.println(e); }
try {
return (new Integer(line)).intValue();
}
catch (Exception e) { return 0; }
}
}
//-----------------------------------------------------------------------------
//Thread zum beenden der Soundschleife
class Beender extends Thread {
public void run() {
Eieruhr bEnde = new Eieruhr();
bEnde.beenden=bEnde.readint();
}
}