过年了,用java放个烟花

主要是根据物理运动轨迹画点或者画线,纯java描点绘制有点丑哈哈:

在这里插入图片描述
githubhttps://github.com/zhao458114067/fire-flower

FireFlower.class

import javazoom.jl.decoder.JavaLayerException;

import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Arrays;
import javax.swing.*;

/**
 * @author zhaoxu
 */
public class FireFlower extends Applet implements MouseListener, Runnable {
    int xClick, yClick;
    static int panelLength = 1600;
    static int panelHeight = 800;
    //烟花上升速度
    static int upSpeed = 5;
    //爆炸条数
    static int boomNum = 100;
    //重力加速度
    static double G = 9.8;
    //摩擦力加速度
    static double GM = 5;
    //半径
    static int d = 1500;
    //频率
    static double freq = 0.08;
    //烟花炸开时保留长度
    static int boomLength = 7;
    //上升图形宽度
    static int upWidth = 5;
    //上升高度
    static int upHeight = 5;
    //爆炸点宽度
    static int boomWidth = 3;
    //爆炸点高度
    static int boomHeight = 3;
    //水平速度
    static int horV = 50;
    //竖直速度
    static int verV = 40;
    //总速度
    static int sumV = 60;


    FireFlower() {
        addMouseListener(this);
    }

    @Override
    public void paint(Graphics g) {

        ImageIcon image = new ImageIcon("src\\main\\resources\\image\\1.jpg");
        getGraphics().drawImage(image.getImage(), 0, 0, getSize().width, getSize().height, this);
        super.paint(g);
    }

    @Override
    public void paintComponents(Graphics g) {
        super.paintComponents(g);
    }

    /**
     * 使该程序能够作为应用程序执行。
     */
    public static void main(String args[]) {
        FireFlower fireFlower = new FireFlower();
        JFrame frame = new JFrame("FireFlower by:zx");
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        frame.getContentPane().add(fireFlower, BorderLayout.CENTER);
        frame.setSize(panelLength, panelHeight);
        //背景色黑色
        fireFlower.setBackground(Color.black);
        fireFlower.init();
        fireFlower.start();
        frame.setVisible(true);
        AudioPlayer audioPlayer = new AudioPlayer(new File("src/main/resources/1.mp3"));
        try {
            audioPlayer.play();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (JavaLayerException e) {
            e.printStackTrace();
        }
    }

    /**
     * 点击会产生一个线程来执行烟花升空
     */
    public void run() {

        //已移动量,会递减,直到大于鼠标点击的y坐标
        int hasMoved = panelHeight;
        //需要一个线程级变量来存储单个线程的坐标
        int threadyClick = yClick;
        int threadxClick = xClick;
        //新建一个Graphics变量
        Graphics graphics = getGraphics();
        int v;
        v = 3;
        //rgb颜色变量
        int r, g, b;
        //烟花上升过程
        while (threadyClick < hasMoved) {
            hasMoved -= upSpeed;
            graphics.setColor(new Color(247, 247, 248));
            graphics.fillOval(threadxClick, hasMoved, upWidth, upHeight);
            for (int j = 0; j <= 10; j++) {
                graphics.setColor(new Color(247, 247, 248));
                graphics.fillOval(threadxClick, hasMoved + j * upSpeed, upWidth, upHeight);
            }
            graphics.setColor(Color.black);
            graphics.fillOval(threadxClick, hasMoved + upSpeed * 10, upWidth, upHeight);
            try {
                Thread.currentThread().sleep(v++);
            } catch (InterruptedException e) {
            }
        }
        //置黑色
        for (int j = 10; j >= 0; j--) {
            graphics.setColor(Color.black);
            graphics.fillOval(threadxClick, hasMoved + (j * upSpeed), upWidth, upHeight);
            try {
                Thread.currentThread().sleep((v++) / 3);
            } catch (InterruptedException e) {
            }
        }

        hasMoved = panelHeight;
        while (hasMoved > threadyClick) {
            graphics.setColor(Color.black);
            graphics.fillOval(threadxClick - 2, hasMoved, upWidth, upHeight);
            hasMoved -= upSpeed;
        }
        int atX = threadxClick;
        int atY = threadyClick;

        //初始化x、y方向初速度
        int x0 = 0;
        int y0 = 0;
        int[][] xPoints = new int[boomNum][400];
        int[][] yPoints = new int[boomNum][400];
        int[] usedSize = new int[boomNum];
        for (int j = 0; j < boomNum; j++) {
            x0 = (int) (Math.random() * (sumV * 2 + 1)) - sumV;
//            y0 = (int) (Math.random() * verV) - verV / 2;
            int yinit = (int) Math.sqrt(sumV * sumV - x0 * x0);
            y0 = yinit >= 0 ? yinit : -yinit;
            y0 = (int) (Math.random() * y0);
            for (int i = 0; i < 400; i++) {
                int y = (int) (y0 * i * freq - 0.5 * G * i * i * freq * freq - 0.5 * GM * i * i * freq * freq);
                int x = (int) (x0 * i * freq - 0.5 * GM * i * i * freq * freq);

//                x = Math.abs(x) > Math.abs(atX - xPoints[j][i - 1]) ? x : atX - xPoints[j][i - 1];
                if (x * x + y * y <= d * d) {
                    xPoints[j][i] = atX - x;
                    yPoints[j][i] = atY - y;
                    usedSize[j]++;
                } else {
                    break;
                }
            }
        }

        v = 20;

        r = (int) (Math.random() * (255 - 200 + 1) + 200);
        g = (int) (Math.random() * (255 - 150 + 1) + 150);
        b = (int) (Math.random() * (255 - 10 + 1) + 10);
        for (int j = 0; j <= 30; j++) {
            for (int i = 0; i < boomNum; i++) {
                //剔除空值
                int pointSize = 0;
                int[] thisPointsx = new int[400];
                int[] thisPointsy = new int[400];
                for (int size = 0; size < xPoints[i].length; size++) {
                    if (xPoints[i][size] != 0 && yPoints[i][size] != 0) {
                        thisPointsx[pointSize] = xPoints[i][size];
                        thisPointsy[pointSize] = yPoints[i][size];
                        pointSize++;
                    }
                }
                if (j < boomLength) {
                    graphics.setColor(new Color(247, 247, 248));
                    graphics.fillOval(thisPointsx[j], thisPointsy[j], boomWidth + 10, boomHeight);
                } else {
                    graphics.setColor(new Color(r, g, b));
                    graphics.fillOval(thisPointsx[j], thisPointsy[j], boomWidth, boomHeight);
                }

//                graphics.drawPolyline(thisPointsx, thisPointsy, usedSize[i]);

                if (j >= boomLength) {
                    graphics.setColor(Color.black);
//                    graphics.drawPolyline(thisPointsx, thisPointsy, j - boomLength);
                    graphics.fillOval(thisPointsx[j - boomLength], thisPointsy[j - boomLength], boomWidth + 10, boomHeight);
                }
            }
            v++;
            v = Math.min(v, 150);
            try {
                Thread.currentThread().sleep(v);
            } catch (InterruptedException e) {
            }
        }

        for (int i = 0; i < boomNum; i++) {
            for (int j = 0; j < 100; j++) {
                graphics.setColor(Color.black);
//                graphics.drawPolyline(xPoints[i], yPoints[i], 100);
                graphics.fillOval(xPoints[i][j], yPoints[i][j], boomWidth, boomHeight);
            }
        }
    }

    public void mouseClicked(MouseEvent e) {

    }

    /**
     * 监听鼠标按键
     *
     * @param e
     */
    public void mousePressed(MouseEvent e) {
        xClick = e.getX();
        yClick = e.getY();
        Thread thread = new Thread(this);
        thread.start();
    }

    public void mouseReleased(MouseEvent e) {

    }

    public void mouseEntered(MouseEvent e) {

    }

    public void mouseExited(MouseEvent e) {

    }
}

这个是播放音乐的AudioPlayer.class:

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

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

/**
 * @author ZhaoXu
 * @date 2022/2/1 15:06
 */
public class AudioPlayer{
    Player player;
    File music;
    //构造方法  参数是一个.mp3音频文件
    public AudioPlayer(File file) {
        this.music = file;
    }
    //播放方法
    public void play() throws FileNotFoundException, JavaLayerException {

        BufferedInputStream buffer = new BufferedInputStream(new FileInputStream(music));
        player = new Player(buffer);
        player.play();
    }
}
以下是一个简单的Java代码示例,用于放烟花: ```java import java.awt.Color; import java.awt.Graphics; import java.util.Random; import javax.swing.JFrame; import javax.swing.JPanel; public class Fireworks extends JPanel implements Runnable { private static final long serialVersionUID = 1L; private int xPos; private int yPos; private int[] xPoints = new int[5]; private int[] yPoints = new int[5]; private Color color; private boolean exploded; private Random random; public Fireworks() { random = new Random(); xPos = random.nextInt(800); yPos = random.nextInt(600); color = new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)); exploded = false; for (int i = 0; i < 5; i++) { xPoints[i] = xPos; yPoints[i] = yPos; } } public void run() { while (!exploded) { try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } yPos += random.nextInt(5) + 1; if (yPos > 500) { explode(); } repaint(); } } private void explode() { exploded = true; for (int i = 0; i < 5; i++) { xPoints[i] = xPos; yPoints[i] = yPos; Thread thread = new Thread(new Explosion(xPoints[i], yPoints[i], color)); thread.start(); try { thread.join(); } catch (InterruptedException e) { e.printStackTrace(); } } } public void paintComponent(Graphics g) { super.paintComponent(g); g.setColor(color); g.fillOval(xPos, yPos, 10, 10); } public static void main(String[] args) { JFrame frame = new JFrame("Fireworks"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(800, 600); JPanel panel = new JPanel(); panel.setLayout(null); for (int i = 0; i < 20; i++) { Fireworks fireworks = new Fireworks(); fireworks.setBounds(0, 0, 800, 600); panel.add(fireworks); Thread thread = new Thread(fireworks); thread.start(); } frame.add(panel); frame.setVisible(true); } } class Explosion implements Runnable { private int xPos; private int yPos; private Color color; private Random random; public Explosion(int xPos, int yPos, Color color) { this.xPos = xPos; this.yPos = yPos; this.color = color; random = new Random(); } public void run() { Graphics g = Fireworks.getGraphics(); for (int i = 0; i < 50; i++) { int x = xPos + random.nextInt(101) - 50; int y = yPos + random.nextInt(101) - 50; g.setColor(color); g.fillOval(x, y, 10, 10); try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } } } } ``` 这个代码会创建一个显示烟花的窗口。在窗口中,会同时放置多个烟花,并且每个烟花都会按照一定的速度向下移动,直到达到一定高度时就会爆炸成多个小烟花。小烟花会随机生成,并且会以一定的速度向四周扩散。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值