T
thunder
Gast
Hallo Leute, ich hab ein kleines Applet programmiert.
Man kann eine Zeit einstellen und das Applet zählt die Zeit runter und spielt am Ende eine Sound-Datei ab.
Ich hab das mit Eclipse programmiert und normalerweise starte ich die Anwendungen auch immer aus Eclipse.
Ein Applet wird ja normalerweise über den Browser angezeigt, das habe ich dann mal auch versucht, aber es geht nicht. Im Browser in der Statusleiste steht: NullPointerException, aber mehr Info erhalte ich nicht, auch über den Befehl appletviewer -debug meinprog.class kommt nix sinnvolles. Ich weiß absolut nicht woran es liegen könnte, weil es in Eclipse ja eben perfekt funktioniert.
Ich hoffe jemand kann mir helfen, hier der Quelltext
Code
config.txt
Man kann eine Zeit einstellen und das Applet zählt die Zeit runter und spielt am Ende eine Sound-Datei ab.
Ich hab das mit Eclipse programmiert und normalerweise starte ich die Anwendungen auch immer aus Eclipse.
Ein Applet wird ja normalerweise über den Browser angezeigt, das habe ich dann mal auch versucht, aber es geht nicht. Im Browser in der Statusleiste steht: NullPointerException, aber mehr Info erhalte ich nicht, auch über den Befehl appletviewer -debug meinprog.class kommt nix sinnvolles. Ich weiß absolut nicht woran es liegen könnte, weil es in Eclipse ja eben perfekt funktioniert.
Ich hoffe jemand kann mir helfen, hier der Quelltext
Code
Code:
import java.applet.AudioClip;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.FlowLayout;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Random;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class LoadAudioAndPlay extends JApplet {
private AudioClip currentSound;
private JButton startJButton, stopJButton, randJButton;
private JTextField inputSound, inputTime;
private JLabel counterLabel, statusLabel, errLabel;
private int counter;
private Runnable r;
private Thread test;
private String DEFAULTTIME, MINTIME, MAXTIME, SOUNDFILE, COUNTDOWN;
private boolean loadcfg[];
// Initializing
public void init() {
loadcfg = new boolean[5];
/*
* load default values and then override if config.txt exists
*/
DEFAULTTIME = "2.50";
MINTIME = "2.50";
MAXTIME = "4.00";
SOUNDFILE = "welcome.wav";
COUNTDOWN = "1";
readConfig();
setLayout(new FlowLayout());
add (new JLabel("Welcome 2 Sabacc"));
inputTime = new JTextField(DEFAULTTIME);
inputSound = new JTextField(SOUNDFILE);
counterLabel = new JLabel("");
statusLabel = new JLabel("ready");
errLabel = new JLabel("");
add (new JLabel("Sounddatei:"));
add (inputSound);
add (new JLabel("Zeit:" ));
add (inputTime);
add (new JLabel("Countdown: "));
if (COUNTDOWN.equals("0")) {
add (new JLabel("(aus)"));
}
add (counterLabel);
add (new JLabel("Status:"));
add (statusLabel);
add (errLabel);
/*
* check config values
*/
for (int i=0; i<loadcfg.length; i++) {
if (!loadcfg[i]) {
errLabel.setText("\"config.txt\" nicht korrekt geladen\n");
}
}
// set up button event handler and buttons
ButtonHandler handler = new ButtonHandler();
// create Start JButton
startJButton = new JButton( "Start" );
startJButton.addActionListener( handler );
add(startJButton);
// create Stop JButton
stopJButton = new JButton( "Stop" );
stopJButton.addActionListener( handler );
add(stopJButton);
// create Random JButton
randJButton = new JButton( "Random" );
randJButton.addActionListener( handler );
add(randJButton);
}
/*
* playing sound
*/
private void playing() {
String s1 = inputSound.getText();
currentSound = getAudioClip( getDocumentBase(), s1);
currentSound.play(); // play AudioClip once
}
// private inner class to handle button events
private class ButtonHandler implements ActionListener {
// process play, loop and stop button events
@SuppressWarnings("deprecation")
public void actionPerformed( ActionEvent actionEvent ) {
if ( actionEvent.getSource() == startJButton ) {
// reset error label
errLabel.setText("");
// check if already started
if (statusLabel.getText().equals("running...")) {
errLabel.setText("already running");
return;
}
/*
* new parallel thread for counting down the time
*/
r = new Runnable() {
public void run() {
/*
* Parse Time
*/
String parse = inputTime.getText().trim();
int komma = parse.indexOf(".", 0);
String nachkomma = parse.substring(komma+1);
if (komma == -1 || nachkomma.length() != 2) {
errLabel.setText("Zeit im Format (M)M.SS eingeben\n");
return;
}
statusLabel.setText("running...");
String min = parse.substring(0, komma);
String sec = parse.substring(komma+1, komma+3);
long timerM = Integer.valueOf( min ).intValue();
double timerS = Integer.valueOf( sec ).intValue();
long timerM1 = (timerM * 60);
double timerS1 = ((timerS / 100.0) * 60.0);
long timer = timerM1 + (long) (timerS1);
counter = (int) (timer);
// count every sec down and refresh label
for (int i=0; i<timer; i++) {
counter--;
if (COUNTDOWN.equals("1")) {
counterLabel.setText(""+counter);
if (counter == 0) {
counterLabel.setText("*BoOoOoM*");
}
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
playing();
}
};
// start the thread
//new Thread(r).start();
test = new Thread(r);
test.start();
}
else if ( actionEvent.getSource() == stopJButton ) {
// reset error label and stop the thread
errLabel.setText("");
if (test != null) {
test.stop();
statusLabel.setText("stopped");
}
}
else if ( actionEvent.getSource() == randJButton ) {
// generate random time
double max = Double.valueOf(MAXTIME).doubleValue();
double min = Double.valueOf(MINTIME).doubleValue();
errLabel.setText("");
double tmp = 0;
Random zufall = new Random();
tmp = zufall.nextDouble() * max;
tmp+= min;
String s2 = String.valueOf(tmp).substring(0, 4);
inputTime.setText(s2);
}
}
}
/*
* read config.txt lines
*/
private void readConfig() {
String lesepuffer;
try {
FileReader freader = new FileReader("config.txt");
BufferedReader in = new BufferedReader(freader);
while((lesepuffer = in.readLine()) != null) {
String tmpString = lesepuffer.toString();
if (tmpString.contains("SOUND=")) {
SOUNDFILE = tmpString.substring(6);
loadcfg[0] = true;
} else if (tmpString.contains("DEFAULTTIME=")) {
if (tmpString.length() > 12 && tmpString.contains(".")) {
DEFAULTTIME = tmpString.substring(12);
loadcfg[1] = true;
}
} else if (tmpString.contains("MINTIME=")) {
if (tmpString.length() > 8 && tmpString.contains(".")) {
MINTIME = tmpString.substring(8);
loadcfg[2] = true;
}
} else if (tmpString.contains("MAXTIME=")) {
if (tmpString.length() > 8 && tmpString.contains(".")) {
MAXTIME = tmpString.substring(8);
loadcfg[3] = true;
}
} else if (tmpString.contains("COUNTDOWN=")) {
if (tmpString.length() == 11 && (tmpString.substring(10).endsWith("1") || tmpString.substring(10).endsWith("0"))) {
COUNTDOWN = tmpString.substring(10);
loadcfg[4] = true;
}
}
}
freader.close();
}
catch(Exception e) {
e.printStackTrace();
errLabel.setText("\"config.txt\" nicht gefunden\n");
}
}
}
config.txt
Code:
SOUND=welcome.wav
DEFAULTTIME=3.50
MINTIME=2.50
MAXTIME=4.00
COUNTDOWN=1