春节将至,给身边的人放一场烟花(java)

简介: 春节将至,给身边的人放一场烟花(java)

新春将至,使用Java送祝福,给亲友们放个烟花。

效果展示

核心逻辑

加载背景

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);
    }

编写线程的run函数,使得烟花升起,爆炸,消失

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 mousePressed(MouseEvent e) {
        xClick = e.getX();
        yClick = e.getY();
        Thread thread = new Thread(this);
        thread.start();
    }

主函数中创建frame类,将烟花类添加进页面中

public static void main(String args[]) throws InterruptedException {
        FireFlower fireFlower = new FireFlower();
        JFrame frame = new JFrame("新年快乐");
        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);
        Random rand = new Random();
        for(int i=0;i<1000;i++){
            int sec = rand.nextInt(1000);
            fireFlower.xClick = rand.nextInt(1600);
            fireFlower.yClick = rand.nextInt(800);
            Thread thread = new Thread(fireFlower);
            thread.sleep(sec);
            thread.start();
        }
    }

完整代码

import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.*;
public class FireFlower extends Applet implements MouseListener, Runnable {
    int xClick = 0, yClick = 0;
    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[]) throws InterruptedException {
        FireFlower fireFlower = new FireFlower();
        JFrame frame = new JFrame("新年快乐");
        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);
        Random rand = new Random();
        for(int i=0;i<1000;i++){
            int sec = rand.nextInt(1000);
            fireFlower.xClick = rand.nextInt(1600);
            fireFlower.yClick = rand.nextInt(800);
            Thread thread = new Thread(fireFlower);
            thread.sleep(sec);
            thread.start();
        }
    }
    /**
     * 点击会产生一个线程来执行烟花升空
     */
    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) {
    }
}

项目链接

地址

目录
相关文章
|
存储 安全 NoSQL
问遍了身边的面试官朋友,我整理出这份 Java 集合高频面试题(2021年最新版)
今天我们继续下一个重要的面试内容:集合框架。HashMap作为 Java 中最靓的仔,毋庸置疑将是本文的主角。
141 0
问遍了身边的面试官朋友,我整理出这份 Java 集合高频面试题(2021年最新版)
|
5天前
|
Java 数据库
【Java多线程】对线程池的理解并模拟实现线程池
【Java多线程】对线程池的理解并模拟实现线程池
13 1
|
1天前
|
Java
Java中的多线程编程:基础知识与实践
【5月更文挑战第13天】在计算机科学中,多线程是一种使得程序可以同时执行多个任务的技术。在Java语言中,多线程的实现主要依赖于java.lang.Thread类和java.lang.Runnable接口。本文将深入探讨Java中的多线程编程,包括其基本概念、实现方法以及一些常见的问题和解决方案。
|
1天前
|
安全 算法 Java
深入理解Java并发编程:线程安全与性能优化
【5月更文挑战第13天】 在Java开发中,并发编程是一个复杂且重要的领域。它不仅关系到程序的线程安全性,也直接影响到系统的性能表现。本文将探讨Java并发编程的核心概念,包括线程同步机制、锁优化技术以及如何平衡线程安全和性能。通过分析具体案例,我们将提供实用的编程技巧和最佳实践,帮助开发者在确保线程安全的同时,提升应用性能。
8 1
|
2天前
|
Java 调度
Java一分钟之线程池:ExecutorService与Future
【5月更文挑战第12天】Java并发编程中,`ExecutorService`和`Future`是关键组件,简化多线程并提供异步执行能力。`ExecutorService`是线程池接口,用于提交任务到线程池,如`ThreadPoolExecutor`和`ScheduledThreadPoolExecutor`。通过`submit()`提交任务并返回`Future`对象,可检查任务状态、获取结果或取消任务。注意处理`ExecutionException`和避免无限等待。实战示例展示了如何异步执行任务并获取结果。理解这些概念对提升并发性能至关重要。
16 5
|
2天前
|
安全 Java 调度
深入理解Java并发编程:线程安全与性能优化
【5月更文挑战第12天】 在现代软件开发中,多线程编程是提升应用程序性能和响应能力的关键手段之一。特别是在Java语言中,由于其内置的跨平台线程支持,开发者可以轻松地创建和管理线程。然而,随之而来的并发问题也不容小觑。本文将探讨Java并发编程的核心概念,包括线程安全策略、锁机制以及性能优化技巧。通过实例分析与性能比较,我们旨在为读者提供一套既确保线程安全又兼顾性能的编程指导。
|
2天前
|
Java
Java一分钟:线程协作:wait(), notify(), notifyAll()
【5月更文挑战第11天】本文介绍了Java多线程编程中的`wait()`, `notify()`, `notifyAll()`方法,它们用于线程间通信和同步。这些方法在`synchronized`代码块中使用,控制线程执行和资源访问。文章讨论了常见问题,如死锁、未捕获异常、同步使用错误及通知错误,并提供了生产者-消费者模型的示例代码,强调理解并正确使用这些方法对实现线程协作的重要性。
13 3
|
2天前
|
安全 算法 Java
Java一分钟:线程同步:synchronized关键字
【5月更文挑战第11天】Java中的`synchronized`关键字用于线程同步,防止竞态条件,确保数据一致性。本文介绍了其工作原理、常见问题及避免策略。同步方法和同步代码块是两种使用形式,需注意避免死锁、过度使用导致的性能影响以及理解锁的可重入性和升级降级机制。示例展示了同步方法和代码块的运用,以及如何避免死锁。正确使用`synchronized`是编写多线程安全代码的核心。
55 2
|
2天前
|
安全 Java 调度
Java一分钟:多线程编程初步:Thread类与Runnable接口
【5月更文挑战第11天】本文介绍了Java中创建线程的两种方式:继承Thread类和实现Runnable接口,并讨论了多线程编程中的常见问题,如资源浪费、线程安全、死锁和优先级问题,提出了解决策略。示例展示了线程通信的生产者-消费者模型,强调理解和掌握线程操作对编写高效并发程序的重要性。
42 3
|
3天前
|
安全 Java
深入理解Java并发编程:线程安全与性能优化
【5月更文挑战第11天】在Java并发编程中,线程安全和性能优化是两个重要的主题。本文将深入探讨这两个方面,包括线程安全的基本概念,如何实现线程安全,以及如何在保证线程安全的同时进行性能优化。我们将通过实例和代码片段来说明这些概念和技术。
3 0