Hallo,
ich baue gerade an einem Schiffchen versenken und möchte auch Sounds drinnen haben. Wenn ich einen Treffer mache soll eine Explosion zu hören sein, wenn man daneben ist, ein Platschen. Manchmal bekomme ich aber folgende Exception:
Exception in thread "PulseAudio Eventloop Thread" java.lang.IllegalStateException: drain failed
at org.classpath.icedtea.pulseaudio.EventLoop.native_iterate(Native Method)
at org.classpath.icedtea.pulseaudio.EventLoop.run(EventLoop.java:141)
at java.lang.Thread.run(Thread.java:636)
Ich weiß momentan nicht einmal wo ich mit der Fehlersuche anfagen soll...
Kann mir da jemand weiterhelfen?
Liebe Grüße,
philipp
ich baue gerade an einem Schiffchen versenken und möchte auch Sounds drinnen haben. Wenn ich einen Treffer mache soll eine Explosion zu hören sein, wenn man daneben ist, ein Platschen. Manchmal bekomme ich aber folgende Exception:
Exception in thread "PulseAudio Eventloop Thread" java.lang.IllegalStateException: drain failed
at org.classpath.icedtea.pulseaudio.EventLoop.native_iterate(Native Method)
at org.classpath.icedtea.pulseaudio.EventLoop.run(EventLoop.java:141)
at java.lang.Thread.run(Thread.java:636)
Ich weiß momentan nicht einmal wo ich mit der Fehlersuche anfagen soll...
Java:
import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.net.URL;
import java.util.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class WUGUI {
private JFrame frame;
private JPanel panelCenter;
private JLabel labelNorth;
private JLabel labelSouth;
private GridLayout gridlayout;
private ArrayList<Battleship> isShip;
private ArrayList<JButton> isEmpty;
private Battleship battleship1;
private Battleship battleship2;
private Battleship battleship3;
private int counter = 0;
private int destroyedParts = 0;
int buttonBoxSize;
//Konstruktor
WUGUI(int n){
this.buttonBoxSize = n;
}
private void mkGUI(String f, String n, String s){
gridlayout = new GridLayout(buttonBoxSize, buttonBoxSize);
this.frame = new JFrame(f);
this.panelCenter = new JPanel();
panelCenter.setLayout(gridlayout);
frame.getContentPane().add(BorderLayout.CENTER, panelCenter);
labelSouth = new JLabel(s);
labelNorth = new JLabel(n+counter);
frame.getContentPane().add(BorderLayout.NORTH, labelNorth);
frame.getContentPane().add(BorderLayout.SOUTH, labelSouth);
frame.setSize(650, 650);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
System.out.println("GUI bereit.");
}
public void go(){
isShip = new ArrayList<Battleship>();
isEmpty = new ArrayList<JButton>();
mkGUI("Let's bomb general WU","Number of exam attempts: ","General WU: Did you prepare well for the battle?");
mkShips(); mkShipLocation(); mkGrid(Color.BLUE);
startGameLoop();
}
private void mkShips(){
battleship1 = new Battleship("A-Em-Ce", new ArrayList<JButton>());
isShip.add(battleship1);
battleship2 = new Battleship("Mar-Ke-Ting", new ArrayList<JButton>());
isShip.add(battleship2);
battleship3 = new Battleship("Ma-The-Ma-Tik", new ArrayList<JButton>());
isShip.add(battleship3);
}
private void mkShipLocation(){
//here the magic happens ;)
battleship1.setJ(new Double(Math.random()*3).intValue());
battleship1.setI(new Double(Math.random()*7).intValue());
battleship2.setI(new Double(Math.random()*10).intValue());
battleship2.setJ(3);
battleship3.setJ(new Double(7+Math.random()*3).intValue());
battleship3.setI(new Double(Math.random()*7).intValue());
}
private void mkGrid(Color c){
for (int i=0; i<buttonBoxSize;i++)
{
for(int j=0; j<buttonBoxSize;j++){
JButton b = new JButton();
putShipsOnGrid(b, j, i, c);
panelCenter.add(b);
}
}
}
public void putShipsOnGrid(JButton b, int j, int i, Color c){
if((battleship1.getJ()==j && battleship1.getI()==i) || (battleship1.getJ()==j && battleship1.getI()+1==i) || (battleship1.getJ()==j && battleship1.getI()+2==i)){
battleship1.add(b);b.addActionListener(new ButtonShipListener());
b.setBackground(c);
}else if((battleship2.getJ()==j && battleship2.getI()==i)||(battleship2.getJ()+1==j && battleship2.getI()==i)||(battleship2.getJ()+2==j && battleship2.getI()==i)||(battleship2.getJ()+3==j && battleship2.getI()==i))
{
battleship2.add(b); b.addActionListener(new ButtonShipListener());
b.setBackground(c);
}else if((battleship3.getJ()==j && battleship3.getI()==i)||(battleship3.getJ()==j && battleship3.getI()+1==i)||(battleship3.getJ()==j && battleship3.getI()+2==i))
{
battleship3.add(b); b.addActionListener(new ButtonShipListener());
b.setBackground(c);
}else{
isEmpty.add(b);b.addActionListener(new ButtonEmptyListener());
b.setBackground(c);}
}
public void startGameLoop(){
while(frame.isVisible()){
try{
//Thread.sleep(10);
if (destroyedParts == 10){
frame.dispose(); mkGUI("YOU DESTROYED GENERAL WU'S FLEET!","You won!", "Congratulations!");
mkGrid(Color.GREEN); playSoundVictory();
Thread.sleep(5000); frame.dispose();
}
if (counter > 5){
frame.dispose(); mkGUI("YOU LOST", "Shame on you! 6 attempts...", "General WU: Whahaha... you lost!");
mkGrid(Color.RED);
Thread.sleep(5000); System.out.println("Spiel verloren!"); frame.dispose();
}
}catch(Exception e){
e.printStackTrace();
}
}
}
private void annonounceDeadShip(Battleship s){
labelSouth.setText("Hurray, we destroyed "+s.getName());
}
//************SOUND CENTER*******************************************************************
private void playSoundVictory(){
Sound deadShipSound = new Sound("gappl02.wav");
deadShipSound.playSoundOnce();
}
private void playSoundExplosion(){
Sound explosion = new Sound("explosion.wav");
explosion.playSoundOnce();
}
private void playSoundCannon(){
Sound cannon = new Sound("cannon.wav");
cannon.playSoundOnce();
}
private void playSoundSplash(){
Sound splash = new Sound("splash.wav");
splash.playSoundOnce();
}
//***************INNER CLASSES***************************************************************
class ButtonEmptyListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
if(isEmpty.contains(e.getSource())){
JButton boing = (JButton) e.getSource();
playSoundSplash();
boing.setBackground(Color.LIGHT_GRAY);
isEmpty.remove(e.getSource()); counter++;
labelNorth.setText("Number of exam attempts: "+counter);
int r = new Double(Math.random()*5).intValue();
switch (r)
{
case 0 :
labelSouth.setText("General WU: You missed, lame duck!");
break;
case 1:
labelSouth.setText("General WU: You are never going to reach the second section...");
break;
case 2:
labelSouth.setText("General WU: Hahaha, you are ridiculous!");
break;
case 3:
labelSouth.setText("General WU: Pathetic.");
break;
case 4:
labelSouth.setText("General WU: Why don't you just leave?");
break;
}
}
}
}
class ButtonShipListener implements ActionListener{
public void actionPerformed(ActionEvent e){
Battleship ship=null;
for (Battleship b:isShip){
if(b.hasButton(((JButton) e.getSource()))){
ship = b;
}
}
if(ship!=null){
ship.removeButton((JButton) e.getSource());
JButton lb = (JButton) e.getSource();
lb.setBackground(Color.GREEN); counter = 0; destroyedParts++;
labelNorth.setText("Number of exam attempts: "+counter);
playSoundExplosion();
if(ship.isDestroyed()){
annonounceDeadShip(ship);
}
}
}
}
class Sound {
private AudioClip song; // Sound player
private URL songPath; // Sound path
Sound(final String filename) {
songPath = getClass().getResource(filename); // Get the Sound URL
song = Applet.newAudioClip(songPath); // Load the Sound
}
public void playSound() {
song.loop(); // Play
}
public void stopSound() {
song.stop(); // Stop
}
public void playSoundOnce() {
song.play(); // Play only once
}
}
}
Kann mir da jemand weiterhelfen?
Liebe Grüße,
philipp
Zuletzt bearbeitet von einem Moderator: