播放和停止音乐Java

时间:2016-05-04 17:46:31

标签: java

我目前正在创建一个带有背景音乐的小型Java游戏,但是我无法按下它停止的按钮。 这是我的菜单代码:

import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Label;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;


public class Menu extends JPanel implements ActionListener,MouseListener {

      private Image img,troll,troll2;
      Audio son = new Audio();
      JButton b1,b2,quit;
      JOptionPane jop1;
      int haut = 50;
      int bas = 50;
      int bas2 = 600;
      int haut2 = 800;

      public Menu(String img) {
        this(new ImageIcon(img).getImage(),new ImageIcon(img).getImage(),new ImageIcon(img).getImage());

      }

      public Menu(Image img,Image troll,Image troll2) {

        this.setLayout(null);

        this.troll = troll;
        this.troll2 = troll2;

        Font f = new Font("Showcard Gothic", Font.BOLD, 16);

        Label la = new Label("Bêta 1.0",Label.CENTER);
        la.setForeground(Color.RED);
        this.add(la);
        la.setBounds(500,800,70,50);
        la.setFont(f);

        b1 = new JButton("Jouer");
        b1.addActionListener(this);
        this.add(b1);
        b1.setBounds(545, 250, 250, 50);
        b1.setForeground(Color.red);
        b1.setBackground(Color.yellow);

        b2 = new JButton("Option");
        b2.addActionListener(this);
        this.add(b2);
        b2.setBounds(545, 400, 250, 50);
        b2.setFont(new java.awt.Font("Serif",1,24));
        b2.setBorderPainted(false); 
        b2.setFocusPainted(false); 

        quit = new JButton(new ImageIcon("images/quit.png"));
        quit.addActionListener(this);
        this.add(quit);
        quit.setBounds(100,100,50,50);
        quit.setBorderPainted(false);
        quit.setFocusPainted(false);     

        this.img = img;
        Dimension size = new Dimension(img.getWidth(null), img.getHeight(null));
        setPreferredSize(size);
        setMinimumSize(size);
        setMaximumSize(size);
        setSize(size); 
      }

    public void paintComponent(Graphics g) {    
        g.drawImage(img, 0, 0, null);

        g.drawImage(troll2, haut2, bas2, null);
        g.drawImage(troll, haut, bas, null);
      }


      public int getHaut2() {
        return haut2;
    }

    public void setHaut2(int haut2) {
        this.haut2 = haut2;
    }

    public int getBas2() {
        return bas2;
    }

    public void setBas2(int bas2) {
        this.bas2 = bas2;
    }

    public int getHaut() {
        return haut;
    }

    public void setHaut(int haut) {
        this.haut = haut;
    }

    public int getBas() {
        return bas;
    }

    public void setBas(int bas) {
        this.bas = bas;
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseEntered(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseExited(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mousePressed(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseReleased(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        if(e.getSource() == quit){
            int option = jop1.showConfirmDialog(null, 
                    "êtes-vous sûr de vouloir quitter le jeu ?", 
                    "Quitter TrollGames", 
                    JOptionPane.YES_NO_OPTION, 
                    JOptionPane.QUESTION_MESSAGE);

             if(option == JOptionPane.YES_OPTION){
                 System.exit(0);
             }  
        }

        if(e.getSource() == b2){
            // How to stop the music ??? //
        }

    }



}

这是我的音乐代码:

import java.io.*;
import java.util.ArrayList;

import javax.sound.sampled.*;
import javax.swing.ImageIcon;

public class Audio extends Thread
{
    String slash = System.getProperty("file.separator");
    File songs_directory = new File(System.getProperty("user.dir") + slash + "musiques"); //You have to specify the directory containing your tracks.
    ArrayList<File> songs_list = new ArrayList<File>();

    public void run()
    {
        for(File file : songs_directory.listFiles())
            if(file.isFile())
                songs_list.add(file);

        while(true)
            Random_selection();
    }

    public void Random_selection()
    {
        int track_number = (int)Math.floor(Math.random()*songs_list.size());
        File song_to_read = songs_list.get(track_number);
        PlaySong(song_to_read);
    }

    public void PlaySong(File song_to_read)
    {
        long song_lenght = 0;

        try{    Clip clip = AudioSystem.getClip();
                clip.open(AudioSystem.getAudioInputStream(song_to_read));
                song_lenght = clip.getMicrosecondLength();
                clip.start();


        }catch (Exception exc) {exc.printStackTrace(System.out);}

        long time_init = System.currentTimeMillis();
        while(System.currentTimeMillis() < time_init + song_lenght/1000)
            try{Thread.sleep(1000);}catch(InterruptedException e){};
    }
}

这是我的JFrame代码:

    import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JFrame;

public class Fenetre extends JFrame implements ActionListener{
     Audio son = new Audio();
    Menu panel = new Menu(new ImageIcon("images/fond.jpg").getImage(),new ImageIcon("images/troll.png").getImage(),new ImageIcon("images/troll2.png").getImage());
    int x,y,x2,y2;

    public Fenetre(){
        this.getContentPane().add(panel);
        this.setUndecorated(true);
        this.setVisible(true);

        this.setLocationRelativeTo(null);
        this.setDefaultLookAndFeelDecorated(false);
        this.setExtendedState(MAXIMIZED_BOTH);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        son.start();
        go();

    }

    private void go(){

        for(int i = x; i<2500;i+=25){
            for(int j = y;j<2500;j+=50){

                    boolean backX = false;
                    boolean backY = false;

                    while (true) {

                      if (x < 1)
                        backX = false;

                      if (x > panel.getWidth() - 85)
                        backX = true;

                      if (y < 1)
                        backY = false;

                      if (y > panel.getHeight() - 85)
                        backY = true;

                      if (!backX)
                        panel.setHaut(++x); 

                      else
                        panel.setHaut(--x);

                      if (!backY)
                        panel.setBas(++y);
                      else
                        panel.setBas(--y);

                repaint();

                try{
                    Thread.sleep(5);
                }catch(InterruptedException e){
                    e.printStackTrace();
                }
            }
        }
        }
    }

    @Override
    public void actionPerformed(ActionEvent arg0) {
        // TODO Auto-generated method stub
        if(arg0.getSource() == panel.b2){
            System.exit(0);
        }
    }}

0 个答案:

没有答案