title: how to add a sound byte to my game?
body: <p>So, I have a working game that is sort of like a retro zombie survival game. I only have 2 zombies, and a single pistol as a weapon. I am looking for a way to add a sound byte to the Jpanel when the keylistener space is released. I have already looked at the question "java generate sound" but found the information to not be helpful, as i would prefer to use my own sound byte file. </p>
  
  <p>here is the partial code, where I would add the audio sample (I know my programming style is very disorganized, sorry :|)</p>
  
  <pre><code>  public void keyReleased(KeyEvent u){
      int key10=u.getKeyCode();
      if (key10==KeyEvent.VK_SPACE){
          shoot=1;
      }
  
  
  }
  </code></pre>
  
  <p>Here is the full code:</p>
  
  <pre><code>package game;
  import java.awt.Color;
  import java.awt.Dimension;
  import java.awt.Font;
  import java.awt.FontMetrics;
  import java.awt.Graphics;
  import java.awt.Image;
  import java.awt.Rectangle;
  import java.awt.Toolkit;
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  import java.awt.event.KeyAdapter;
  import java.awt.event.KeyEvent;
  import java.awt.geom.Ellipse2D;
  import java.awt.geom.Line2D;
  import java.awt.geom.Point2D;
  import java.awt.geom.Rectangle2D;
  import java.util.Random;
  
  import javax.swing.ImageIcon;
  import javax.swing.JPanel;
  import javax.swing.Timer;
  
  public class game extends JPanel implements ActionListener {
  private int key6;
  private int key3;
  private int key1;
  private int key2;
  private int key4;
  private int moveleft;
  private int moveright;
  private int moveup;
  private int movedown;
  private boolean medkitalive;
  private int setx;
  private int sety;
  private int randx;
  private int randy;
  private int medx;
  private int medy;
  private int randx2;
  private int randy2;
  private int randx3;
  private int randy3;
  private Image enemy2;
  private int kills=0;
  private int health=100;
  private int restart=0;
  private boolean enemyalive;
  private boolean enemy2alive;
  private boolean enemy3alive;
  private int enemiesalive=0;
  private int enemiesallowed=0;
  private int timetester=0;
  private int released=0;
  private int released1=0;
  private int released2=0;
  private int released3=0;
  private int shoot=0;
  private int movebullet;
  private int shootang=0;
  private Timer timer;
  private Timer timer1;
  private int Delay=10;
  private final int B_width=1000;
  private final int B_height=600;
  private Image background;
  private Image player;
  private Image playerright;
  private Image playerleft;
  private Image playerup;
  private boolean ingame=true;
  private final int ALL_DOTS=1000;
  private int xvel;
  private int yvel;
  private Image enemy;
  private int key5;
   private final int x[] = new int[ALL_DOTS];
      private final int y[] = new int[ALL_DOTS];
      private Image bullet;
      private Image blood;
      private Image medkit;
      private Image bullet2;
      public game(){
      addKeyListener(new Tadapter1());
      addKeyListener(new Tadapter());
      addKeyListener(new Tadapter2());
      addKeyListener(new Tadapter4());
      addKeyListener(new Tadapter5());
      addKeyListener(new Tadapter6());
      setFocusable(true);
      setPreferredSize(new Dimension(B_width,B_height));
      setBackground(Color.black);
      loadimages();
      initgame();
  
  }
  
  private void loadimages() {
      ImageIcon iia= new ImageIcon("background.jpg");
      background= iia.getImage();
  
      ImageIcon iib= new ImageIcon("player.png");
      player= iib.getImage();
  
      ImageIcon iic=new ImageIcon("bullet.png");
      bullet=iic.getImage();
  
      ImageIcon iid=new ImageIcon("enemy.png");
      enemy=iid.getImage();
  
      ImageIcon iie=new ImageIcon("blood.jpg");
      blood=iie.getImage();
  
      ImageIcon iif=new ImageIcon("playerleft.png");
      playerleft=iif.getImage();
  
      ImageIcon iig=new ImageIcon("playerright.png");
      playerright=iig.getImage();
  
      ImageIcon iih=new ImageIcon("playerup.png");
      playerup=iih.getImage();
  
      ImageIcon iii=new ImageIcon("bullet2.png");
      bullet2=iii.getImage();
  
      ImageIcon iij=new ImageIcon("medkit.png");
      medkit=iij.getImage();
  
      ImageIcon iik=new ImageIcon("enemy2.png");
      enemy2=iik.getImage();
  
  }
  
  
  
  
  
  
  public void doDrawing(Graphics g){
  
      if (health&lt;=0){g.setFont(new Font("Helvetica", Font.BOLD, 60 ));
          g.setColor(Color.GREEN);
          g.drawImage(blood,0,0,this);
          health=0;
          g.drawString("Game Over, press enter to restart", 10, 300);
          if (restart==1){
              ingame=true;
              x[0]=500;
              y[0]=500;
              kills=0;
              health=100;
  
          }
      }
      if (ingame){
          if (health&gt;0){
  
          restart=0;
          Random rand=new Random();
          Random rand2=new Random();
          Random rand3= new Random();
          timetester++;
          if (timetester%150==1&amp;&amp;enemiesalive==0){
          enemyalive=true;
           randx=rand.nextInt(800)+100;
           randy=rand.nextInt(500)+100;
           if (randx-x[0]&lt;100||randy-y[0]&lt;100){
                  randx=rand2.nextInt(800)+100;
                  randy=rand2.nextInt(500)+100;
              }
           if (randx-x[0]&lt;100||randy-y[0]&lt;100){
                  randx=rand2.nextInt(800)+100;
                  randy=rand2.nextInt(500)+100;
              }
           if (randx-x[0]&lt;100||randy-y[0]&lt;100){
                  randx=rand2.nextInt(800)+100;
                  randy=rand2.nextInt(500)+100;
              }
           if (randx-x[0]&lt;100||randy-y[0]&lt;100){
                  randx=rand2.nextInt(800)+100;
                  randy=rand2.nextInt(500)+100;
              }
           if (randx-x[0]&lt;100||randy-y[0]&lt;100){
                  randx=rand2.nextInt(800)+100;
                  randy=rand2.nextInt(500)+100;
              }
  
           enemiesalive=1;
  
          }
          if (timetester%2000==1&amp;&amp;medkitalive==false){
              medkitalive=true;
              medx=rand3.nextInt(800)+100;
              medy=rand3.nextInt(500)+100;
          }
  
          if (timetester%150==1&amp;&amp;enemy2alive==false&amp;&amp;kills&gt;=10){
              enemy2alive=true;
              randx2=rand2.nextInt(800)+100;
              randy2=rand2.nextInt(500)+100;
              if (randx2-x[0]&lt;100||randy2-y[0]&lt;100){
                  randx2=rand2.nextInt(800)+100;
                  randy2=rand2.nextInt(500)+100;
              }
              if (randx2-x[0]&lt;100||randy2-y[0]&lt;100){
                  randx2=rand2.nextInt(800)+100;
                  randy2=rand2.nextInt(500)+100;
              }
              if (randx2-x[0]&lt;100||randy2-y[0]&lt;100){
                  randx2=rand2.nextInt(800)+100;
                  randy2=rand2.nextInt(500)+100;
              }
              if (randx2-x[0]&lt;100||randy2-y[0]&lt;100){
                  randx2=rand2.nextInt(800)+100;
                  randy2=rand2.nextInt(500)+100;
              }
              if (randx2-x[0]&lt;100||randy2-y[0]&lt;100){
                  randx2=rand2.nextInt(800)+100;
                  randy2=rand2.nextInt(500)+100;
              }
  
          }
          if (x[0]-randx&gt;0&amp;&amp;shoot==0){
              randx++;
          }
          if (x[0]-randx&lt;0&amp;&amp;shoot==0){
              randx--;
          }
          if (y[0]-randy&lt;0&amp;&amp;shoot==0){
              randy--;
          }
          if (y[0]-randy&gt;0&amp;&amp;shoot==0){
              randy++;
          }
          if (x[0]-randx2&gt;0&amp;&amp;shoot==0){
              randx2++; if (kills&gt;10){
                  randx2+=((kills-10)/10);
              }if (kills&gt;=30){
                  randx2-=-1+((kills-10)/10);
              }
          }
          if (x[0]-randx2&lt;0&amp;&amp;shoot==0){
              randx2--;if (kills&gt;10){
                  randx2-=((kills-10)/10);
              }if (kills&gt;=30){
                  randx2+=-1+((kills-10)/10);
              }
          }
          if (y[0]-randy2&lt;0&amp;&amp;shoot==0){
              randy2--;if (kills&gt;10){
                  randy2-=((kills-10)/10);
              }if (kills&gt;=30){
                  randy2+=-1+((kills-10)/10);
              }
          }
          if (y[0]-randy2&gt;0&amp;&amp;shoot==0){
              randy2++;if (kills&gt;10){
                  randy2+=((kills-10)/10);
              }if (kills&gt;=30){
                  randy2-=-1+((kills-10)/10);
              }
          }
          if (x[0]-randx&gt;0&amp;&amp;shoot==1&amp;&amp;timetester%5==1){
              randx++;
          }
          if (x[0]-randx&lt;0&amp;&amp;shoot==1&amp;&amp;timetester%5==1){
              randx--;
          }
          if (y[0]-randy&lt;0&amp;&amp;shoot==1&amp;&amp;timetester%5==1){
              randy--;
          }
          if (y[0]-randy&gt;0&amp;&amp;shoot==1&amp;&amp;timetester%5==1){
              randy++;
          }
          if (x[0]-randx2&gt;0&amp;&amp;shoot==1&amp;&amp;timetester%5==1){
              randx2++;if (kills&gt;10){
                  randx2+=((kills-10)/10);
              }if (kills&gt;=30){
                  randy2-=-1+((kills-10)/10);
              }
          }
          if (x[0]-randx2&lt;0&amp;&amp;shoot==1&amp;&amp;timetester%5==1){
              randx2--;if (kills&gt;10){
                  randx2-=((kills-10)/10);
              }
              if (kills&gt;=30){
                  randx2+=-1+((kills-10)/10);
              }
          }
          if (y[0]-randy2&lt;0&amp;&amp;shoot==1&amp;&amp;timetester%5==1){
              randy2--;if (kills&gt;10){
                  randy2-=((kills-10)/10);
              }
              if (kills&gt;=30){
                  randy2+=-1+((kills-10)/10);
              }
          }
          if (y[0]-randy2&gt;0&amp;&amp;shoot==1&amp;&amp;timetester%5==1){
              randy2++;if (kills&gt;10){
                  randy2+=((kills-10)/10);
              }
              if (kills&gt;=30){
                  randy2-=-1+((kills-10)/10);
              }
          }
          if (!enemy2alive){
              randx2=-100;
              randy2=-100;
          }
          if(!enemyalive){
              randx=-100;
              randy=-100;
          }
          g.drawImage(background, 0, 0, this);
          if (shootang==3){
          g.drawImage(player,x[0],y[0],this);}
          if (shootang==0){
              g.drawImage(playerleft,x[0],y[0],this);}
          }
          if (shootang==1){
          g.drawImage(playerup,x[0],y[0],this);}
          if (shootang==2){
              g.drawImage(playerright,x[0],y[0],this);}
          if (enemyalive){
          g.drawImage(enemy,randx, randy, this);}
  
          if (enemy2alive){
              g.drawImage(enemy2, randx2, randy2, this);
  
          }
          if (medkitalive==true){
              g.drawImage(medkit, medx, medy, this);
          }
          Rectangle player=new Rectangle(x[0],y[0],25,25);
          Rectangle enemy2r=new Rectangle(randx2,randy2,25,25);
          Rectangle enemy=new Rectangle(randx,randy,25,25);
          g.drawRect(randx, randy, 25, 25);
          g.drawRect(randx2, randy2, 25, 25);
          Rectangle medkit=new Rectangle(medx,medy,25,25);
  
          if (player.intersects(medkit)&amp;&amp;medkitalive==true){
              health+=10;
              medkitalive=false;
          }
          g.setFont(new Font("Helvetica", Font.BOLD, 60 ));
          g.setColor(Color.RED);
          String Health = Integer.toString(health);
          g.drawString("Health:  "+Health ,1, 50);
  
  
  
          g.setFont(new Font("Helvetica", Font.BOLD, 60 ));
          g.setColor(Color.RED);
          String Kills = Integer.toString(kills);
          g.drawString("Kills:  "+Kills ,600, 50);
          if (enemy2r.intersects(player)&amp;&amp;enemy2alive==true){
  
              if(timetester%5==1){
                  health--;
              }
  
  
          }
          if (enemy.intersects(player)&amp;&amp;enemyalive==true){
  
  
              if(timetester%5==1){
                  health--;
              }
  
  
  
          }
          if (moveleft==0&amp;&amp;moveright==0&amp;&amp;moveup==0&amp;&amp;movedown==0){
              setx=x[0];
              sety=y[0];
          }
  
          if (shoot==0){
              movebullet=0;
          }
  
          if (shoot==1){
              System.out.print(movebullet+"...");
  
              if (shootang==0&amp;&amp;moveright==0&amp;&amp;moveup==0&amp;&amp;movedown==0||moveleft==1){
                  moveleft=1;
                  g.drawImage(bullet, x[0]+movebullet, y[0]+10, this);
                  Rectangle bullet= new Rectangle(x[0]+movebullet,y[0]+10,5,5);
                  movebullet-=2;
                  if (bullet.intersects(enemy)){
  
                      enemiesalive=0;
                      if (enemyalive==true){
                          kills++;
                      }
                      enemyalive=false;
  
                      movebullet=0;moveleft=0;
                      shoot=0;
                  }
                  if (bullet.intersects(enemy2r)){
                      if (enemy2alive==true){
                          kills++;
                      }
                      enemy2alive=false;
  
                      movebullet=0;moveleft=0;
                      shoot=0;
                  }
  
  
                  if (movebullet&lt;-400){movebullet=0;moveleft=0;
                      shoot=0;
                  }
              }
              if (shootang==1&amp;&amp;moveleft==0&amp;&amp;moveright==0&amp;&amp;movedown==0||moveup==1){
                  moveup=1;
                  g.drawImage(bullet2, x[0]+10, y[0]+movebullet, this);
                  Rectangle bullet= new Rectangle(x[0]+10,y[0]+movebullet,5,5);
  
                  movebullet-=2;
                  if (bullet.intersects(enemy)){
  
                      enemiesalive=0;
                      if (enemyalive==true){
                          kills++;
                      }
                      enemyalive=false;
  
                      moveup=0;
                      movebullet=0;
                      shoot=0;
                  }
                  if (bullet.intersects(enemy2r)){
                      if (enemy2alive==true){
                          kills++;
                      }
                      enemy2alive=false;
  
                      moveup=0;
                      movebullet=0;
                      shoot=0;
                  }
  
                  if (movebullet&lt;-400){moveup=0;
                      movebullet=0;
                      shoot=0;
                  }
              }
              if (shootang==3&amp;&amp;moveleft==0&amp;&amp;moveright==0&amp;&amp;moveup==0||movedown==1){movedown=1;
                  g.drawImage(bullet2, x[0]+5, y[0]+movebullet, this);
                  Rectangle bullet= new Rectangle(x[0]+5,y[0]+movebullet,5,5);
  
                  movebullet+=2;
                  if (bullet.intersects(enemy)){
                      if (enemyalive==true){
                          kills++;
                      }
                      enemiesalive=0;
                      enemyalive=false;
  
                      movedown=0;
                      movebullet=0;
                      shoot=0;
                  }
                  if (bullet.intersects(enemy2r)){
                      if (enemy2alive==true){
                          kills++;
                      }
                      enemy2alive=false;
                      movedown=0;
                      movebullet=0;
                      shoot=0;
                  }
  
  
  
                  if (movebullet&gt;400){movedown=0;
                      movebullet=0;
                      shoot=0;
                  }
              }
              if (shootang==2&amp;&amp;moveleft==0&amp;&amp;moveup==0&amp;&amp;movedown==0||moveright==1){moveright=1;
                  g.drawImage(bullet, x[0]+movebullet, y[0]+10, this);
  
                  Rectangle bullet= new Rectangle(x[0]+movebullet,y[0]+10,5,5);
                  movebullet+=2;
                  if (bullet.intersects(enemy)){
                      enemiesalive=0;
                      if (enemyalive==true){
                          kills++;
                      }
                      enemyalive=false;
  
                      moveright=0;
                      movebullet=0;
                      shoot=0;
                  }
                  if (bullet.intersects(enemy2r)){
                      if (enemy2alive==true){
                          kills++;
                      }
                      enemy2alive=false;
  
                      moveright=0;
                      movebullet=0;
                      shoot=0;
                  }
  
                  if (movebullet&gt;400){moveright=0;
                      movebullet=0;
                      shoot=0;
                  }
              }
          }
      }
  }
  
  
  @Override
  public void paintComponent(Graphics g){
      super.paintComponent(g);
      doDrawing(g);
  
  
  
  
  }
  private void initgame() {
  
      timer= new Timer(Delay,this);
      timer.start();  
  
  }
  
  public void movex(){
  
      x[0]+=xvel;
  }
  public void movey(){
      y[0]+=yvel;
  }
  
  @Override
  public void actionPerformed(ActionEvent e) {
      if (health&lt;=0){
          ingame=false;
          repaint();
  
      }
      if (health&gt;0){
      if (ingame){
          if (shoot==0){
          movex();
          movey();}
          if (shoot==1){
              timer.setDelay(0);
              }
              if (shoot==0){
              timer.setDelay(10);
              }
      }
      repaint();}
      if (health&lt;=0){
          ingame=false;
      }
  }
  public class Tadapter extends KeyAdapter{
  
      @Override
      public void keyPressed(KeyEvent f){
  
          int key3=f.getKeyCode();
          if (key3==KeyEvent.VK_A){
              xvel=-3;
              released3=1;
              shootang=0;
  
          }}
  
  
  
  
  
      public void keyReleased(KeyEvent f){
          int key4=f.getKeyCode();
          if (key4==KeyEvent.VK_A){
              released3=0;
          }
          if (key4==KeyEvent.VK_A&amp;&amp;released1!=1){
              xvel=0;
          }
          if (key4==KeyEvent.VK_A&amp;&amp;released1==1){
              xvel=3;
          }
  
      }
  
  
  }
  
  
  
  
  
  
  
  public class Tadapter1 extends KeyAdapter{
  @Override
  public void keyPressed(KeyEvent e){
  
      int key1=e.getKeyCode();
      if (key1==KeyEvent.VK_W){
          yvel=-3;
          released2=1;
          shootang=1;
  
      }
  }
  public void keyReleased(KeyEvent e){
      int key2=e.getKeyCode();
      if (key2==KeyEvent.VK_W){
          released2=0;
  
      }
      if (key2==KeyEvent.VK_W&amp;&amp;released!=1){
          yvel=0;
      }
      if (key2==KeyEvent.VK_W&amp;&amp;released==1){
          yvel=3;
      }
  
  }
  
  
  }
  
  public class Tadapter2 extends KeyAdapter{
  @Override
  public void keyPressed(KeyEvent h){
      key5=h.getKeyCode();
      if (key5==KeyEvent.VK_D){
          xvel=3;
          released1=1;
          shootang=2;
      }
  }
  public void keyReleased(KeyEvent h){
      key6=h.getKeyCode();
      if (key6==KeyEvent.VK_D&amp;&amp;released3==1){
          xvel=-3;
  
  
      }
      if (key6==KeyEvent.VK_D&amp;&amp;released3!=1){
          xvel=0;
  
      }
      if (key6==KeyEvent.VK_D){
  
          released1=0;
      }
  }
  }
  
  public class Tadapter4 extends KeyAdapter{
  @Override
  public void keyPressed(KeyEvent j){
      int key7=j.getKeyCode();
      if (key7==KeyEvent.VK_S){
          yvel=3;
          released=1;
          shootang=3;
      }
  }
  public void keyReleased(KeyEvent j){
      int key8=j.getKeyCode();
      if (key8==KeyEvent.VK_S){
  
          released=0;
      }
      if (key8==KeyEvent.VK_S&amp;&amp;released2==1){
          yvel=-3;
      }
      if (key8==KeyEvent.VK_S&amp;&amp;released2!=1){
          yvel=0;
      }
  }
  }
  
  
  public class Tadapter5 extends KeyAdapter{
  @Override
  public void keyPressed(KeyEvent u){
      int key9=u.getKeyCode();
      if (key9==KeyEvent.VK_SPACE){
  
      }
  }
  public void keyReleased(KeyEvent u){
      int key10=u.getKeyCode();
      if (key10==KeyEvent.VK_SPACE){
          shoot=1;
      }
  
  
  }
  }
  
  public class Tadapter6 extends KeyAdapter{
  @Override
  public void keyPressed(KeyEvent p){
      int key9=p.getKeyCode();
      if (key9==KeyEvent.VK_ENTER){
  
      }
  }
  public void keyReleased(KeyEvent p){
      int key10=p.getKeyCode();
      if (key10==KeyEvent.VK_ENTER){
          restart=1;
      }
  
  
  }
  }
  
  }
  </code></pre>
  
  <p>Also, which types of files would work in Jpanel?</p>