背景音乐的添加

  • 背景音乐的添加
    • [1. 创建音乐类](#1. 创建音乐类)
    • [2. 音乐类的代码](#2. 音乐类的代码)

背景音乐的添加


学习于bilibili 尚学堂官方

1. 创建音乐类


在model中创建Music类

创建一个文件夹lib存放jar包

创建文件夹存放背景音乐

将背景音乐和jar包放入其中

点击文件(F)->项目构建->模块->依赖-> + ->JARs or ...->选择jar包->勾选

2. 音乐类的代码


java 复制代码
package com.zy.model;

/*
背景音乐
 */


import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.player.Player;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

public class Music {
    public void music() throws FileNotFoundException, JavaLayerException {
        //获取当前音乐地址
        String str = System.getProperty("user.dir") + "/music/music.mp3";
        BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(str));


        Player player = new Player(bufferedInputStream);
        player.play();


    }
}

在TestRenderMap中

java 复制代码
package com.zy.test;

/*
测试渲染一个地图类
 */

import com.zy.model.*;
import com.zy.util.LayerUtil;
import com.zy.util.MapUtil;
import javazoom.jl.decoder.JavaLayerException;

import javax.swing.*;
import java.io.FileNotFoundException;
import java.util.List;

public class TestRenderMap extends JFrame {

    public static Map map = MapUtil.build(3);
    public TestRenderMap() throws FileNotFoundException, JavaLayerException {
        //初始化窗口
        init();

        //渲染图层
       List<Layer> list = map.getList();
        for (int i = 0; i < list.size(); i++) {
            renderLayer(list.get(i));
        }

        map.compareAll();//游戏开始时判定置灰


        //自动刷新
        autoRefresh();

        new Music().music();
    }

    private void renderLayer(Layer layer){
        //渲染图层
        //默认坐标是(0,0)
        //布局方式  默认swing 添加组件 提供了多种布局方式  网格 流
        Cell[][] cells = layer.getCells();
        for (int row = 0; row < cells.length; row++) {
            for (int col = 0; col < cells[row].length; col++) {
                Brand brands1 = cells[row][col].getBrand();

                int x = col * 50 + layer.getOffsetx();//0 50 100 150 ...
                int y = row * 50 + layer.getOffsety();//0 50 100 ...
                brands1.setBounds(x,y,50,50);


                this.getContentPane().add(brands1);
            }
            System.out.println();
        }
    }

    private void init(){
        this.setTitle("易-java-羊了个羊");//设置弹出游戏窗口的标题
        this.setSize(450,800);//设置窗口大小

        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭的同时也推出java进程


        //设置绝对布局
        this.setLayout(null);
        this.setBounds(0,0,450,800);

        this.setLocationRelativeTo(null);//居中
        this.setVisible(true);//让窗口显示 默认隐藏
    }
    //线程
    private void autoRefresh(){

        JFrame start = this;

        new Thread(new Runnable() {
            @Override
            public void run() {
                //用来刷新
                while (true){
                    start.repaint();//调用的还是当前窗口的repaint
                    try {
                        Thread.sleep(40);
                        start.setVisible(true);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }

                }
            }
        }).start();
    }



    public static void main(String[] args) throws FileNotFoundException, JavaLayerException {
        new TestRenderMap();
    }
}
相关推荐
是枚小菜鸡儿吖37 分钟前
把视频教程变成可复习的笔记:Docker 部署 BiliNote,自动转写与总结
笔记·docker·容器
运维全栈笔记1 小时前
Vue + Spring Boot 前后端分离项目部署笔记(若依 RuoYi-Vue 3.9.2)
运维·服务器·vue.js·spring boot·笔记·开源·开源软件
吴声子夜歌6 小时前
Java面试——算法
java·算法·面试
evans在进步6 小时前
LeetCode 64:最小路径和——Java 原地动态规划详解
java·leetcode·动态规划
凤山老林7 小时前
Spring Boot 3.x AOT 编译实战:从原理、踩坑到生产落地
java·spring boot·后端
xian_wwq7 小时前
【学习笔记】-深度认知系列-第1讲-AI不是魔法——三句话讲清楚人工智能到底是什么
笔记·学习·深度认知
博傅8 小时前
Spring 核心原理
java·后端·spring
yurenpai(27届找实习中)8 小时前
从零读懂 AI 智能客服(一):模块职责与 SSE 聊天链路(后端架构)
java·人工智能·架构·langchain4j
M78佐菲8 小时前
c语言学习笔记:排序与查找方法整理
linux·c语言·笔记·学习·算法
陈皮波比茶8 小时前
Swagger
java