Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Adding background music
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
jason27131




PostPosted: Sat May 08, 2010 3:14 pm   Post subject: Adding background music

I created this game in java for my summative. I want the menu to have a background music, and the clip for the music is 3 minutes long. Is there any way to do it?

code:
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.applet.*;
import java.net.*;
import javax.swing.JFrame;
import java.util.*;
import java.util.ArrayList;

public class Game extends JFrame
{
   static final int WIDTH = 700;
   static final int HEIGHT = 400;
   JFrame frame = new JFrame("2D Shooter");
   Drawing drawing = new Drawing();
   Player1 player1 = new Player1();
        Player2 player2 = new Player2();
        ArrayList<Point> bullets2L = new ArrayList<Point>();
        ArrayList<Point> bullets1L = new ArrayList<Point>();
        ArrayList<Point> bullets2R = new ArrayList<Point>();
        ArrayList<Point> bullets1R = new ArrayList<Point>();
        static final int MARGINR = 40;
        static final int MARGINB = 75;
        static final int MAXHEIGHT = 80; //sets max height before falling       
        // declare variable for total height jumped
        int heightJump = 0;
        int heightJump1 = 0;   
        // variable for player hp
        int p1HP = 100;
        int p2HP = 100;
        int bx1L ,by1L, bx1R, by1R, bx2L, by2L, bx2R ,by2R; //sets the variables for the coordinate of the bullets
        boolean p1faceLeft, p2faceRight = true;
        boolean p1faceRight, p2faceLeft = false;
        boolean dead1 = false;
        boolean dead2 = false;
        String p1Name;
        String p2Name;
        int mouseX = 0;
        int mouseY = 0;
        boolean menu = true;
        boolean instruction = false;
       

        KeyboardInput keyboard = new KeyboardInput();
   
         class Player1 extends Thread
    {
      int x = WIDTH - 90, y = HEIGHT - 100;
     
      public void run()
      {
         try
         {
            while (true)
            {
                                        keyboard.poll();
                                     keyPressed();
               sleep(10);
               drawing.repaint();
                      }
         }
         catch (InterruptedException e)
                        {
                        }
            }
     }
       
         class Player2 extends Thread
    {
      int x = 40, y = HEIGHT - 100;
     
      public void run()
      {
      }
   }
       
         public static void main (String[] args)
   { 
      Game s = new Game();
   }
 
   public Game()
   {
                p1Name = JOptionPane.showInputDialog(frame, "What's the name of player 1?");
                p2Name = JOptionPane.showInputDialog(frame, "What's the name of player 2?");
      frame.getContentPane().add(drawing,"Center");
      frame.setLocation(WIDTH,HEIGHT);
      frame.setSize(WIDTH, HEIGHT);
      frame.setVisible(true);
                frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
                addKeyListener(keyboard);
        frame.addKeyListener(keyboard);
                frame.addMouseListener(new MouseListen());
      player1.start();
    }

       
        //moves the players when the right key is pressed
   public void keyPressed ()
   {
                // player 1
      if(dead1 != true)
      if(keyboard.keyDown( KeyEvent.VK_LEFT))
        {
         player1.x -= 3;
                        p1faceLeft = true;
                        p1faceRight = false;
         if (player1.x < 0) 
                            player1.x = 0;
      }
                if(dead1 != true)
      if( keyboard.keyDown( KeyEvent.VK_RIGHT))
      {
         player1.x += 3;
                        p1faceLeft = false;
                        p1faceRight = true;
         if( player1.x + 3 > WIDTH - MARGINR)
                player1.x = WIDTH - MARGINR;   
           }
                if(dead1 != true)
      if( keyboard.keyDown( KeyEvent.VK_UP))
      {
                        if(heightJump < MAXHEIGHT)
                        {
         player1.y -= 9;
                        heightJump += 5;
                        }
                        else player1.y = player1.y;
         if( player1.y <= 0)
                player1.y = 0;
      }
                if(dead1 != true)
                 if(keyboard.keyDownOnce( KeyEvent.VK_UP))
                 heightJump = 0;
                
                if(dead1 != true)
      if( keyboard.keyDown( KeyEvent.VK_DOWN ) )
      {
         player1.y += 3;
         if (player1.y + 3 > HEIGHT - MARGINB)   
            player1.y = HEIGHT - MARGINB;
      }
               
                // player 2
                if(dead2 != true)
                 if(keyboard.keyDown( KeyEvent.VK_A))
      {
                        p2faceLeft = true;
                        p2faceRight = false;
         player2.x -= 3;
         if (player2.x < 0) 
         {
                            player2.x = 0;
         }
      }
                if(dead2 != true)
      if( keyboard.keyDown( KeyEvent.VK_D))
      {
                        p2faceLeft = false;
                        p2faceRight = true;
         player2.x += 3;
         if( player2.x + 3 > WIDTH - MARGINR)
                player2.x = WIDTH - MARGINR;   
           }
               
                if(dead2 != true)
      if( keyboard.keyDown( KeyEvent.VK_W))
      {
                        if(heightJump1 < MAXHEIGHT)
                        {
         player2.y -= 9;
                        heightJump1 += 5;
                        }
                        else player2.y = player2.y;
         if( player2.y <= 0)
                player2.y = 0;
      }
                if(dead2 != true)
                 if(keyboard.keyDownOnce( KeyEvent.VK_W))              
                 heightJump1 = 0;

                if(dead2 != true)
      if( keyboard.keyDown( KeyEvent.VK_S) )
      {
         player2.y += 3;
         if (player2.y + 3 > HEIGHT - MARGINB)   
            player2.y = HEIGHT - MARGINB;
      }
                        if(keyboard.keyDownOnce( KeyEvent.VK_V))
      {
                        bx2L = player2.x;
                        by2L = player2.y;
                        bullets2L.add((new Point(bx2L, by2L)));
                }
       
                        if(keyboard.keyDownOnce( KeyEvent.VK_B))
      {
                        bx2R = player2.x;
                        by2R = player2.y;
                        bullets2R.add((new Point(bx2R, by2R)));
                }
                       
                        if(keyboard.keyDownOnce( KeyEvent.VK_O))
      {
                        bx1L = player1.x;
                        by1L = player1.y;
                        bullets1L.add((new Point(bx1L, by1L)));
      }   
                        if(keyboard.keyDownOnce( KeyEvent.VK_P))
      {
                        bx1R = player1.x;
                        by1R = player1.y;
                        bullets1R.add((new Point(bx1R, by1R)));
               
      }
               
                // respawn button
                if(keyboard.keyDownOnce(KeyEvent.VK_H))
                {
                        if(dead1 == true || dead2 == true)
                        {
                                player1.x = WIDTH - 90;
                                player1.y = HEIGHT - 100;
                                player2.x = 40;
                                player2.y = HEIGHT - 100;       
                                dead1 = false;
                                dead2 = false;
                                p1HP = 100;
                                p2HP = 100;
                   }
                }
                // end of key movement

                // collision detection for ground and platforms
                player2.y += 4;
                player1.y += 4;
                if (player1.y + 5 > HEIGHT - MARGINB)   
        player1.y = HEIGHT - MARGINB;
           if (player2.y + 5 > HEIGHT - MARGINB)   
         player2.y = HEIGHT - MARGINB;
                if (player1.y < 280 && player1.y > 270 && player1.x > 230 && player1.x < 350)
                        player1.y = 275;
                if (player2.y < 280 && player2.y > 270 && player2.x > 230 && player2.x < 350)
                        player2.y = 275;
                if (player2.y < 220 && player2.y > 210 && player2.x > 380 && player2.x < 530)
                        player2.y = 215;
                if (player1.y < 220 && player1.y > 210 && player1.x > 380 && player1.x < 530)
                        player1.y = 215;
                       
      drawing.repaint();
  }
 
  //Mouse Listener
  class MouseListen extends MouseAdapter
   {
      public void mouseClicked(MouseEvent e)
      {
         mouseX = e.getX() - 11;
         mouseY = e.getY() - 35;
                        drawing.repaint();
      }
   } 

   
           
   class Drawing extends JComponent
   {
                // draws the default sprite of the players and the background
                ImageIcon player1D = new ImageIcon("player1StillL.gif");
                ImageIcon player2D = new ImageIcon("player2StillR.gif");
                ImageIcon background = new ImageIcon("background.gif");
                // sets the objects for player hp
                ImageIcon p1Hp = new ImageIcon();
                ImageIcon p2Hp = new ImageIcon();

      public Drawing()
      {
         repaint();
      }
     
      public void paint(Graphics g)
      {
                        if (menu == false)
                        {
                //draws rectangle around players for bullet collision
                       Rectangle r2 = new Rectangle(player2.x, player2.y, 25,40);
                       Rectangle r1 = new Rectangle(player1.x, player1.y, 25,40);
               
                // directs the player hp objects to the picture if the hp gets to that point    
                                 if(p1HP == 100)
                                        p1Hp = new ImageIcon("P1100.jpg");
                                 if(p1HP == 80)
                                        p1Hp = new ImageIcon("p180.jpg");
                                if(p1HP == 60)
                                        p1Hp = new ImageIcon("p160.jpg");
                                if(p1HP == 40)
                                        p1Hp = new ImageIcon("p140.jpg");
                                if(p1HP == 20)
                                        p1Hp = new ImageIcon("p120.jpg");
                                 if(p1HP == 0)
                                        p1Hp = new ImageIcon("p10.jpg");
                                       
                                 if(p2HP == 100)
                                        p2Hp = new ImageIcon("P2100.jpg");
                                 if(p2HP == 80)
                                        p2Hp = new ImageIcon("p280.jpg");
                                 if(p2HP == 60)
                                        p2Hp = new ImageIcon("p260.jpg");
                                 if(p2HP == 40)
                                        p2Hp = new ImageIcon("p240.jpg");
                                 if(p2HP == 20)
                                        p2Hp = new ImageIcon("p220.jpg");
                                 if(p2HP == 0)
                                        p2Hp = new ImageIcon("p20.jpg");
       
                
                        // resets default sprite
                        if(keyboard.keyDown( KeyEvent.VK_H))
                        {
                                player1D = new ImageIcon("player1StillL.gif");
                                player2D = new ImageIcon("player2StillR.gif");
                        }
       
                        // changes sprite when keys are pressed for player 2
                        if(dead2 != true)
                        {
                         if(keyboard.keyDown( KeyEvent.VK_A) && (keyboard.keyDown( KeyEvent.VK_V) == false))
                         {
                                 player2D = new ImageIcon("player2L.gif");
                         }
                          if(keyboard.keyDownOnce( KeyEvent.VK_V))
                         {
                                player2D = new ImageIcon("player2SL.gif");
                         }
       
                         if(keyboard.keyDown( KeyEvent.VK_D) && (keyboard.keyDown( KeyEvent.VK_B) == false))
                         {
                                player2D = new ImageIcon("player2R.gif");
                         }
                          if(keyboard.keyDownOnce( KeyEvent.VK_B))
                         {
                                player2D = new ImageIcon("player2SR.gif");
                         }     
                         if((keyboard.keyDown(KeyEvent.VK_A)) == false && p2faceLeft == true && keyboard.keyDown( KeyEvent.VK_V) == false && keyboard.keyDown( KeyEvent.VK_B) == false )
                                player2D = new ImageIcon("Player2StillL.gif"); 
                        if((keyboard.keyDown(KeyEvent.VK_D)) == false && p2faceRight == true  && keyboard.keyDown( KeyEvent.VK_V) == false && keyboard.keyDown( KeyEvent.VK_B) == false )
                                player2D = new ImageIcon("Player2StillR.gif");  
                        }
                       
                        // changes sprite when keys are pressed for player 1
                        if(dead1 != true)
                        {
                         if(keyboard.keyDown( KeyEvent.VK_LEFT) && (keyboard.keyDown( KeyEvent.VK_O) == false))
                         {
                                 player1D = new ImageIcon("player1L.gif");
                    }
                          if(keyboard.keyDownOnce( KeyEvent.VK_O))
                         {
                                 player1D = new ImageIcon("player1SL.gif");
                    }
       
                         if(keyboard.keyDown( KeyEvent.VK_RIGHT) && (keyboard.keyDown( KeyEvent.VK_P) == false) )
                         {
                                player1D = new ImageIcon("player1R.gif");
                         }
                          if(keyboard.keyDownOnce( KeyEvent.VK_P))
                         {
                                player1D = new ImageIcon("player1SR.gif");
                         }
                         if((keyboard.keyDown(KeyEvent.VK_LEFT)) == false && p1faceLeft == true && keyboard.keyDown( KeyEvent.VK_O) == false && keyboard.keyDown( KeyEvent.VK_P) == false )
                                player1D = new ImageIcon("Player1StillL.gif"); 
                         if((keyboard.keyDown(KeyEvent.VK_RIGHT)) == false && p1faceRight == true  && keyboard.keyDown( KeyEvent.VK_O) == false && keyboard.keyDown( KeyEvent.VK_P) == false )
                                player1D = new ImageIcon("Player1StillR.gif"); 
                        }
                       
                        // draws death sprite for the players
                        if(dead1 == true)
                                player1D = new ImageIcon("player1Dead.gif");
                        if(dead2 == true)
                                player2D = new ImageIcon("player2Dead.gif");
       
       
                                              
                                //Map 1
                        g.drawImage(background.getImage(),0,0,this);
                        g.setColor(Color.black);
                                g.fillRect(250, 310 , 100, 10);        
                                g.fillRect(400, 250, 130, 10);
                                //End of Map 1
                               
                                //Draws Player and hp
                                g.drawImage(p2Hp.getImage(), 30, 20, this);
                                g.drawImage(p1Hp.getImage(), 450, 20, this);
                                g.setColor(Color.black);
                                g.drawString(p1Name, 620, 40);
                                g.drawString(p2Name, 30, 40);
                                g.drawImage(player2D.getImage(), player2.x, player2.y - 6, this);       
                                g.drawImage(player1D.getImage(), player1.x, player1.y, this);
                                g.setColor(Color.yellow);
                                                               
                               
                               
                        // Draws the bullets and also does bullet collision detection when player 1 aren't dead
                        if(dead1 == false)
                        {
                                if (!bullets1L.isEmpty())
                           {
                                        for (int i = 0; i < bullets1L.size(); i++)
                                        {
                                                Point bullet1L = bullets1L.get(i);
                                                bullet1L.x -= 4;
                                                if(r2.contains(bullet1L.x, bullet1L.y))
                                                {
                                                                p2HP -= 1;
                                                        if(p2HP == 0)
                                                        dead2 = true;   
                                                }
                                                g.fillOval(bullet1L.x, bullet1L.y + 10, 6,3);
                                                if (bullet1L.x <= 0)
                                                {
                                                        bullets1L.remove(bullet1L);
                                                        i--;                       
                                                }
                                        }
                                }                     
                                        if (!bullets1R.isEmpty())
                           {
                                        for (int k = 0; k < bullets1R.size(); k++)
                                        {
                                                Point bullet1R = bullets1R.get(k);
                                                bullet1R.x += 4;
                                                if(r2.contains(bullet1R.x, bullet1R.y))
                                                {
                                                        p2HP -= 1;
                                                        if(p2HP == 0)
                                                        dead2 = true;   
                                                }
                                                g.fillOval(bullet1R.x, bullet1R.y + 10, 6,3);
                                                if (bullet1R.x <= 0)
                                                {
                                                        bullets1R.remove(bullet1R);
                                                        k--;                       
                                                }
                                        }
                                }
                        }
                       
                        // Draws the bullets and also does bullet collision detection when player 2 aren't dead
                        if(dead2 == false)
                        {
                                if (!bullets2L.isEmpty())
                                {
                                        for (int s = 0; s < bullets2L.size(); s++)
                                        {
                                                Point bullet2L = bullets2L.get(s);                                         
                                                bullet2L.x -= 4;
                               if(r1.contains(bullet2L.x, bullet2L.y))
                                                {
                                                        p1HP -= 1;
                                                        if(p1HP == 0)
                                                        dead1 = true;                  
                                                }
                                                g.fillOval(bullet2L.x, bullet2L.y + 10, 6,3);            
                                                if (bullet2L.x >= 1000)
                                                {
                                                bullets2L.remove(bullet2L);
                                                s--;
                                                }                            
                                        }
                              }
                                if (!bullets2R.isEmpty())
                                {
                                        for (int j = 0; j < bullets2R.size(); j++)
                                        {
                                                Point bullet2R = bullets2R.get(j);                                         
                                                bullet2R.x += 4;
                               if(r1.contains(bullet2R.x, bullet2R.y))
                                                {
                                                 p1HP -= 1;
                                                 if(p1HP == 0)
                                                 dead1 = true;
                                                }
                                                
                                                g.fillOval(bullet2R.x, bullet2R.y + 10, 6,3);            
                                                if (bullet2R.x <= 0)
                                                {
                                                bullets2R.remove(bullet2R);
                                                j--;
                                                }                            
                                        }
                                }
                        }
                }
      else if (menu == true)
                {
                        main.start();
                        //draws the menu
                        ImageIcon menuBack = new ImageIcon("menu.jpg");
                        g.drawImage(menuBack.getImage(),0,0,this);
                        ImageIcon play = new ImageIcon("playgame.png");
                        g.drawImage(play.getImage(),300,150,this);
                        ImageIcon instru = new ImageIcon("instru.png");
                        g.drawImage(instru.getImage(),300,200,this);
                        Rectangle b1 = new Rectangle(300, 150,100,30);
                   Rectangle b2 = new Rectangle(300,200,100,30);
                       
                        if (b1.contains(mouseX,mouseY))
                                menu = false;
                        if (b2.contains(mouseX,mouseY))
                                instruction = true;
                }       
                if (instruction == true)
                {
                        ImageIcon instruBack = new ImageIcon("instruback.jpg");
                        g.drawImage(instruBack.getImage(),0,0,this);
                        ImageIcon back = new ImageIcon("back.png");
                        g.drawImage(back.getImage(),290,320,this);
                        Rectangle b3 = new Rectangle(290,320,100,30);
                       
                        if(b3.contains(mouseX,mouseY))
                        instruction = false;           
                }                                                                                                                                                                                                                                
   }         
 }
}



Sponsor
Sponsor
Sponsor
sponsor
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 1 Posts ]
Jump to:   


Style:  
Search: