package audiotest;
import java.awt.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
import java.awt.event.*;
import java.applet.*;
public class AudioTest extends JFrame {
int breite = 600;
int hoehe = 700;
public AudioTest(File f) {
setSize(breite, hoehe);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
new Hauptfenster();
}
}
class Hauptfenster extends JFrame implements ActionListener {
JButton start = new JButton("Start");
public Hauptfenster() {
setSize(300, 300);
setLocation(600, 100);
setLayout(new GridLayout(1, 1));
start.addActionListener(this);
add(start);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == start) {
new Methode();
}
}
}
class Methode extends JFrame implements Runnable {
File file;
AudioTest sm;
boolean change = false;
boolean flag = false;
AudioClip richtig,falsch;
public Methode() {
richtig = Applet.newAudioClip(getClass().getResource("richtig.wav"));
falsch = Applet.newAudioClip(getClass().getResource("falsch.wav"));
file = ladeDatei();
sm = new AudioTest(file);
Thread th=new Thread(this);
th.start();
}
public void run() {
while(true) {
boolean check;
Random rand = new Random();
int a=rand.nextInt(2);
if(a==1) check = true;
else check = false;
if(check) {
if(!flag) {
change = true;
flag = true;
}
}
else {
if(flag) {
change = true;
flag = false;
}
}
System.out.println("läuft");
if(change) {
if(flag) {
falsch.play();
}
else {
richtig.play();
}
change = false;
}
try{Thread.sleep(200);}catch(InterruptedException err){System.out.println(err.toString());}
System.out.print(" -----> Methode läuft noch.. <-----");
}
}
public File ladeDatei() {
JFileChooser jfc = new JFileChooser();
int returnVal = jfc.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = jfc.getSelectedFile();
return file;
}
return null;
}
}