⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 multimidi.java

📁 。使用Java语言来开发一个简单的游戏一直以来是我的想法
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.sound.midi.*;
import java.io.File;

public class MultiMidi extends JFrame 
{
	private String[] midiFile={"betsy.mid","camptown.mid"};
	private MultiMidiPlayer midiPanel;
	private ImagePanel imagePanel;   //绘制图像的面板
	
    public MultiMidi() 
    {
        super("MultiMidiDemo");
    	this.setBounds(150,150,400,400);
        //获取内容窗格
        Container container = getContentPane();
        	    //创建用于绘制图像的面板
	    imagePanel = new ImagePanel();
	    container.add(imagePanel, BorderLayout.CENTER);
        midiPanel = new MultiMidiPlayer(midiFile);
        container.add(midiPanel, BorderLayout.SOUTH);
        
       setVisible(false);
        //setDefaultCloseOperation(EXIT_ON_CLOSE);       
    }
    
/*    public static void main(String[] args)
    {
        MultiMidiDemo application = new MultiMidiDemo();
    }*/
}
 class ImagePanel extends JPanel 
    {
    	private ImageIcon images;
    
    	
        public ImagePanel() 
        {
        	super();
        	setBackground(Color.WHITE);
      
                //载入图像
        //	images= new ImageIcon("boy01.png");
        images= new ImageIcon("music.jpg");
        
        }
        
        public void paintComponent(Graphics g) 
        {
            Graphics2D g2d=(Graphics2D)g;
            g2d.setColor(getBackground());
            g2d.fillRect(0, 0, getWidth(), getHeight());
            //绘制图像,坐标为屏幕中心 
            int x = (this.getWidth() - images.getIconWidth())/2;
            int y = (this.getHeight() - images.getIconHeight())/2;
            images.paintIcon(this, g, x, y);
        }
       
    }

class MultiMidiPlayer extends JPanel implements Runnable, ActionListener 
{
	private Thread runner;
	private JPanel buttonPanel;              //存放按钮的面板
    private JButton play, stop;              //播放, 结束按钮 
    private JLabel label;                    //显示信息
    private Sequence currentSound;           //音序
    private Sequencer player;                //音序器
    private String[] songFile;               //播放文件
    private int songToPlay;

    public MultiMidiPlayer(String[] songs) {
    	super();
    	
        songFile = songs;
        //创建标签
        label = new JLabel("", JLabel.CENTER);
        label.setFont(new Font("Serif", Font.PLAIN, 14));
        label.setPreferredSize(new Dimension(400, 30));
        //创建按钮
        play = new JButton("Play");
        stop = new JButton("Stop");
        stop.setEnabled(false);
        //注册监听器
        play.addActionListener(this);
        stop.addActionListener(this);
        
        setLayout(new BorderLayout());
        //将标签加入内容窗格
        add(label, BorderLayout.NORTH);
        
        buttonPanel = new JPanel();
        //将按钮加入buttonPanel面板
        buttonPanel.add(play);
        buttonPanel.add(stop);
        
        //将buttonPanel面板加入内容窗口
        add(buttonPanel, BorderLayout.CENTER);
        
        if (songFile.length == 0) 
        {
            play.setEnabled(false);
        }
     }
     
      class ImagePanel extends JPanel 
    {
    	private ImageIcon images;
    
    	
        public ImagePanel() 
        {
        	super();
        	setBackground(Color.WHITE);
      
                //载入图像
        //	images= new ImageIcon("boy01.png");
        images= new ImageIcon("d412505582.jpg");
        
        }
        
        public void paintComponent(Graphics g) 
        {
            Graphics2D g2d=(Graphics2D)g;
            g2d.setColor(getBackground());
            g2d.fillRect(0, 0, getWidth(), getHeight());
            //绘制图像,坐标为屏幕中心 
            int x = (this.getWidth() - images.getIconWidth())/2;
            int y = (this.getHeight() - images.getIconHeight())/2;
            images.paintIcon(this, g, x, y);
        }
       
    }
     //处理按钮事件
     public void actionPerformed(ActionEvent event) 
     {
    	 if (event.getSource() == play)
    		 play();
         else
             stop();
     }
     //播放音乐
     public void play() 
     {
         if (runner == null) 
         {
            runner = new Thread(this);
            runner.start();
            play.setEnabled(false);
            stop.setEnabled(true);
         }
     }
     //停止播放
     public void stop() 
     {
        if (runner != null) 
        {
            runner = null;
            stop.setEnabled(false);
            play.setEnabled(true);
        }
     }

     public void run() 
     {
        try 
        {
            player = MidiSystem.getSequencer();      //获取音序器
        } 
        catch (Exception exception) 
        {
            label.setText(exception.toString());
        }
        
        while (Thread.currentThread() == runner) 
        {
            for (songToPlay = 0; songToPlay < songFile.length; songToPlay++) 
            {
                if (songFile[songToPlay] != null) 
                {
                    try 
                    {
                        File song = new File(songFile[songToPlay]);
                        //获取音序
                        currentSound = MidiSystem.getSequence(song);
                        player.open();
                        //设置音序器的音序
                        player.setSequence(currentSound);
                        //开始播放
                        player.start();
                        label.setText("正在播放的音乐文件为: " + song.getName());
                        while (player.isRunning() && runner != null) 
                        {
                            try 
                            {
                                Thread.sleep(1000);
                            } 
                            catch (InterruptedException exception){}
                        }
                        label.setText("");
                        player.close();
                    } 
                    catch (Exception exception) 
                    {
                        label.setText(exception.toString());
                        break;
                    }
                }
            }
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -