听起来像Java。错误

时间:2013-11-22 19:57:58

标签: java noclassdeffounderror audio

我正试图在我的游戏中播放声音,例如当我按下我的UP键时,听到激光的声音,但我收到错误,我不明白为什么。我很可能在我的方法中调用了错误的代码,或者我的错误来源了。

Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: Could not initialize class SpaceInvadersPanel$SoundEffect
at SpaceInvadersPanel$4.keyPressed(SpaceInvadersPanel.java:69)
at java.awt.Component.processKeyEvent(Unknown Source)
at javax.swing.JComponent.processKeyEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.KeyboardFocusManager.redispatchEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

-

 import java.awt.event.*;
import javax.swing.*;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Color;
import javax.sound.sampled.*;
import java.io.*;
import java.net.URL;
import java.applet.*;
import java.awt.*;
import java.io.File;
import java.net.MalformedURLException;


public class SpaceInvadersPanel extends JPanel {
    private static final long serialVersionUID = 1L;
    private Timer timer;
    private Player1 player1;
    private Player1weapon player1weapon;
    private Player2 player2;
    private Player2weapon player2weapon;
    public Image background;
    public ImageIcon bg;
    int width, height;

    public SpaceInvadersPanel() {
        bg = new ImageIcon("src/resources/BG.png");
        background = bg.getImage();
        ActionListener action = new ActionListener() {  
            public void actionPerformed(ActionEvent evt) {
                if (player1 != null && player2 != null) {
                    player1.updateForNewFrame();
                    player1weapon.updateForNewFrame();
                    player2.updateForNewFrame();
                    player2weapon.updateForNewFrame();
                }
                repaint();
            }
        };//end Actionlistener()

        timer = new Timer( 10, action );

        addMouseListener( new MouseAdapter() {
            public void mousePressed(MouseEvent evt) {
                requestFocus();
            }
        } );

        addFocusListener( new FocusListener() {
            public void focusGained(FocusEvent evt) {
                timer.start();
                repaint();
            }
            public void focusLost(FocusEvent evt) {
                timer.stop();
                repaint();
            }
        } );

        addKeyListener( new KeyAdapter() {
            public void keyPressed(KeyEvent evt) {
                int code = evt.getKeyCode();

                if (code == KeyEvent.VK_LEFT) {
                    player1.centerX -= 10;
                }
                if (code == KeyEvent.VK_RIGHT) {
                    player1.centerX += 10;
                }
                if (code == KeyEvent.VK_UP) {
                    if (!player1weapon.isFalling)
                        player1weapon.isFalling = true;
                    SoundEffect.SHOOT.play();
                }

                if (code == KeyEvent.VK_A) {
                    player2.centerX -= 10;
                }
                if (code == KeyEvent.VK_D) {
                    player2.centerX += 10;
                }
                if (code == KeyEvent.VK_S) {
                    if (!player2weapon.isFalling)
                        player2weapon.isFalling = true;
                    SoundEffect.SHOOT.play();
                }
            }//end KeyPressed()
        } );//end KeyAdapter()
    }//end SpaceInvadersPanel


    public void paintComponent(Graphics g) {
        g.drawImage(background,0,0,700,700,null);
            if (player1 == null && player2 == null) {
            width = 700;
            height = 700;
            player1 = new Player1();
            player2 = new Player2();
            player1weapon = new Player1weapon();
            player2weapon = new Player2weapon();
        }

        if (hasFocus())
            g.setColor(Color.BLACK);
        else {
            g.setColor(Color.GREEN); 
            g.drawString("CLICK TO START THE GAME", 270, 350); 
            g.drawString("Player1", 550, 500); 
            g.drawString("Right arrow - Move Right", 550, 515); 
            g.drawString("Left arrow - Move Left", 550, 530);
            g.drawString("Up arrow - Fire", 550, 545);
            g.drawString("Player2", 50, 150); 
            g.drawString("D key - Move Right", 50, 165); 
            g.drawString("A key - Move Left", 50, 180);
            g.drawString("S key - Fire", 50, 195);
        }
        player1weapon.draw(g);
        player2weapon.draw(g);
        player1.draw(g);
        player2.draw(g);    

} // end paintComponent()

    public enum SoundEffect {
           SHOOT("sounds/Laser.wav");       // bullet

           // Nested class for specifying volume
           public static enum Volume {
              MUTE, LOW, MEDIUM, HIGH
           }

           public static Volume volume = Volume.LOW;

           // Each sound effect has its own clip, loaded with its own sound file.
           private Clip clip;

           // Constructor to construct each element of the enum with its own sound file.
           SoundEffect(String soundFileName) {
              try {
                 // Use URL (instead of File) to read from disk and JAR.
                 URL url = this.getClass().getClassLoader().getResource(soundFileName);
                 // Set up an audio input stream piped from the sound file.
                 AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(url);
                 // Get a clip resource.
                 clip = AudioSystem.getClip();
                 // Open audio clip and load samples from the audio input stream.
                 clip.open(audioInputStream);
              } catch (UnsupportedAudioFileException e) {
                 e.printStackTrace();
              } catch (IOException e) {
                 e.printStackTrace();
              } catch (LineUnavailableException e) {
                 e.printStackTrace();
              }
           }

           // Play or Re-play the sound effect from the beginning, by rewinding.
           public void play() {
              if (volume != Volume.MUTE) {
                 if (clip.isRunning())
                    clip.stop();   // Stop the player if it is still running
                 clip.setFramePosition(0); // rewind to the beginning
                 clip.start();     // Start playing
              }
           }

           // Optional static method to pre-load all the sound files.
           static void init() {
              values(); // calls the constructor for all the elements
           }
        }


    public class Player1{
        int centerX, centerY;
        int width, height;
        int explosionFrameNumber;
        int score = 0;
        boolean isExploding;
        public Image p1;
        public ImageIcon p1stock;
        public Image explodep1;
        public ImageIcon explodep1icon;

        Player1() {
            centerX = 500;
            centerY = 600;
            isExploding = false;
        }

        void updateForNewFrame() {
            if (isExploding) {
                    explosionFrameNumber++;
                    if (explosionFrameNumber == 15) {
                        width = 700;
                        centerX = (int)(width*Math.random());
                        centerY = 600;
                        isExploding = false;
                    }
                }
            if(centerX <= -30)
                centerX = -10;
            else if(centerX > 640)
                centerX = 630;
        }//end updateForNewFrame

        void draw(Graphics g) {
            p1stock = new ImageIcon("src/resources/player1.png");
            p1 = p1stock.getImage();
            explodep1icon = new ImageIcon("src/resources/explosion.png");
            explodep1 = explodep1icon.getImage();  
            g.setColor(Color.RED);
            g.drawString("Score: " + score, 20, 450);
            g.drawImage(p1, centerX, centerY, p1.getWidth(null), p1.getHeight(null),null);
            if (isExploding) {
                g.drawImage(explodep1, centerX - 4*explosionFrameNumber + 40, centerY - 2*explosionFrameNumber + 10, 
                            8*explosionFrameNumber, 4*explosionFrameNumber,null);
            }
        }//end draw()

    }//end Player1


    public class Player2{
        int centerX, centerY;
        int width, height;
        boolean isExploding;
        int explosionFrameNumber;
        int score = 0;
        public Image p2;
        public Image explodep2;
        public ImageIcon p2stock;
        public ImageIcon explodep2icon;

        Player2() {
            centerX = 50;
            centerY = 30;
            isExploding = false;
        }

        void updateForNewFrame() {
            if (isExploding) {
                explosionFrameNumber++;
                if (explosionFrameNumber == 15) {
                    isExploding = false;
                    width = 700;
                    centerX = (int)(width*Math.random());
                    centerY = 30;
                }
            }
            if(centerX <= -30)
                centerX = -10;
            else if(centerX > 640)
                centerX = 630;
        }//end updateForNewFrame()

        void draw(Graphics g) {
            p2stock = new ImageIcon("src/resources/player2.png");
            p2 = p2stock.getImage();
            explodep2icon = new ImageIcon("src/resources/explosion.png");
            explodep2 = explodep2icon.getImage();
            g.setColor(Color.RED);
            g.drawString("Score: " + (score), 20, 250);
            g.drawImage(p2, centerX, centerY, p2.getWidth(null), p2.getHeight(null),null);
            if (isExploding) {
                g.drawImage(explodep2, centerX - 4*explosionFrameNumber + 40, centerY - 2*explosionFrameNumber + 10, 
                            8*explosionFrameNumber, 4*explosionFrameNumber,null);
            }
        }//end draw()

    } //end Player2


    public class Player1weapon{
        int centerX, centerY;
        boolean isFalling;
        public Image laserp1;
        public ImageIcon lp1icon;


        public Player1weapon() {
            isFalling = false;
        }

        void updateForNewFrame() {
            if (isFalling) {
                if (centerY < player2.centerY - 50) {
                    isFalling = false;
                }
                else 
                    if (Math.abs(centerX - player2.centerX - 40) <= 30 && Math.abs(centerY - player2.centerY + 35) <= 70) {
                        player2.isExploding = true;
                        player2.explosionFrameNumber = 1;
                        isFalling = false;
                        player1.score+=10;
                    }
                    else
                        centerY -= 20;
            }//end if  
        }//end updateForNewFrame()

        void draw(Graphics g) {
            lp1icon = new ImageIcon("src/resources/laserplayer1.png");
            laserp1 = lp1icon.getImage();
            if (!isFalling) {
                centerX = player1.centerX + 37;
                centerY = player1.centerY + 23;
            }
            g.drawImage(laserp1, centerX, centerY - 8, laserp1.getWidth(null), laserp1.getHeight(null),null);
        }//end Draw()

    }//end player1weapon


    public class Player2weapon{
            int centerX, centerY;
            boolean isFalling;
            public Image laserp2;
            public ImageIcon lp2icon;

            public Player2weapon() {
                isFalling = false;
            }

            void updateForNewFrame() {
                if (isFalling) {
                    if (centerY > player1.centerY + 50) {
                        isFalling = false;
                    }
                    else 
                        if (Math.abs(centerX - player1.centerX) <= 20 && Math.abs(centerY - player1.centerY + 20) <= 20) {
                            player1.isExploding = true;
                            player1.explosionFrameNumber = 1;
                            isFalling = false;
                            player2.score+=10;
                        }
                        else
                            centerY += 20;
                }//end if   
            }//end updateForNewFrame()

            void draw(Graphics g) {
                lp2icon = new ImageIcon("src/resources/laserplayer2.png");
                laserp2 = lp2icon.getImage();
                if (!isFalling) {
                    centerX = player2.centerX + 1;
                    centerY = player2.centerY + 15;
                }
                if (isFalling) {
                    g.drawImage(laserp2, centerX + 36, centerY - 8, laserp2.getWidth(null), laserp2.getHeight(null),null);
                }
            }//end Draw()

        }//end player2weapon


    /*public class Asteroid{
        int centerX, centerY;
        boolean isFalling;
        public Image asteroid;
        public ImageIcon icon;

        public Asteroid() {
            isFalling = false;
            icon = new ImageIcon("src/resources/laserplayer2.png");
            asteroid = icon.getImage();
        }

        void updateForNewFrame() {
            if (isFalling) {
                if (centerY > player1.centerY + 50) {
                    isFalling = false;
                }
                else 
                    if (Math.abs(centerX - player1.centerX) <= 20 && Math.abs(centerY - player1.centerY + 20) <= 20) {
                        player1.isExploding = true;
                        player1.explosionFrameNumber = 1;
                        isFalling = false;
                        player2.score+=10;
                    }
                    else
                        centerY += 50;
            }//end if   
        }//end updateForNewFrame()

        void draw(Graphics g) {
            if (!isFalling) {
                centerX = player2.centerX + 1;
                centerY = player2.centerY + 15;
                g.drawImage(asteroid, centerX + 36, centerY - 8, asteroid.getWidth(null), asteroid.getHeight(null),null);
            }
            if (isFalling) {
                g.drawImage(asteroid, centerX + 36, centerY - 8, asteroid.getWidth(null), asteroid.getHeight(null),null);
            }
        }//end Draw()

    }//end asteroid*/

}//end SpaceInvadersPanel

1 个答案:

答案 0 :(得分:1)

stacktrace指出当程序在运行时尝试访问它时,类未被初始化。在这种情况下,堆栈跟踪中也应该有ExceptionInInitializerError。如果是,您可以在SoundEffect构造函数中放置一个Exception或Throwable块来查看确切的错误:

catch (Exception e) {
  e.printStackTrace();
}

如有疑问,可以查看 - Debugging a NoClassDefFoundError