search
HomeJavajavaTutorialHow to use Java to realize romantic meteor confession?

Introduction

The functions implemented in this article are:

1. Play music

2. Customize the number of meteors, flight speed, halo size, and meteor size

3. Customized confession words

The knowledge points used are:

GUI: java implements forms and Swing. In fact, the GUI of JAVA Swing is no longer used in enterprises. It is mainly used by some schools and training institutions to teach students to write some games and small projects and practice their skills.

Multi-threading: Let the CPU handle multiple tasks at the same time (this article involves music, text appearing slowly, and meteor lines moving)

Rendering:

How to use Java to realize romantic meteor confession?

Music class (in fact, you can also use music without music, some people don’t like it):

Core code

import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.player.Player;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
 
public class MusicThread extends Thread{
    @Override
    public void run() {
        //播放音乐
        System.out.println("开始播放");
        //表示音乐文件
        File f = new File("nv.mp3");
        //第三方jar包  Player类
 
        try {
            Player p = new Player(new FileInputStream(f));//参数:文件输入流对象
            // p.play();
        } catch (FileNotFoundException | JavaLayerException e) {
            e.printStackTrace();
        }
 
    }
}

Implementation class:

import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.player.Player;
 
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.Random;
import javax.swing.*;
 
public class MeteorFly extends JFrame {
    int AppletWidth, AppletHeight;
    final int MAX = 6; // (~)流星的个数
    final int SLEEP = 2; // 流星飞行的速度(数值越大,速度越慢)
    final int COLORLV = 1; // (~)色阶(可改变光晕大小)
    final int SIZE = 3 ; // (~)流星大小
    private MyPanel panel;
    public MeteorFly() {
        panel = new MyPanel();
        this.setTitle("LOVE");
        this.getContentPane().add(panel);
        this.setSize(AppletWidth, AppletHeight); // 创建窗体
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
    }
    public static void main(String[] args) {
        new Thread(){
            @Override
            public void run() {
                //声明一个File对象
                File mp3 = new File("nv.mp3");
                //创建一个输入流
                FileInputStream fileInputStream = null;
 
                try {
                    fileInputStream = new FileInputStream(mp3);
                    //创建一个缓冲流
                    BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
                    //创建播放器对象,把文件的缓冲流传入进去
                    Player player = new Player(fileInputStream);
                    //调用播放方法进行播放
                    player.play();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (JavaLayerException e) {
                    e.printStackTrace();
                }
            }
        }.start();
        new MeteorFly();
    }
 
    class MyPanel extends JPanel implements Runnable {
        Meteor p[];
        BufferedImage OffScreen;
        Graphics drawOffScreen;
        Thread pThread;
        Font drawFont = new Font("Arial",0,28);
        public MyPanel() {
            //setBackground(Color.black); //窗体初始化
            AppletWidth = Toolkit.getDefaultToolkit().getScreenSize().width;
            AppletHeight = Toolkit.getDefaultToolkit().getScreenSize().height-200;
            p = new Meteor[MAX];
            for (int i = 0; i < MAX; i++) {
                p[i] = new Meteor();
            }
            OffScreen = new BufferedImage(AppletWidth, AppletHeight,
                    BufferedImage.TYPE_INT_BGR);
            drawOffScreen = OffScreen.getGraphics();
            pThread = new Thread(this);
            pThread.start();
 
            new Thread(){
                @Override
                public void run() {
                    try {
                        sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    str1 = "流星雨是世间宝藏,而你是我的人间理想";
                    while(true){
                            try {
                                sleep(150);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            pos++;
                            if (pos > str1.length() - 1) {
                                pos = str1.length() - 1;
                                break;
                            }
                    }
                }
            }.start();
 
        }
        int pos = 0;
        String str1 = "                                                                   ";
        @Override
        public void paintComponent(Graphics g) {
            super.paintComponents(g);
            g.drawImage(OffScreen, 0, 0, this);
            g.setColor(Color.pink);
            g.setFont(new Font("宋体", Font.BOLD, 50));
            g.drawString(str1.substring(0,pos+1),260,700);
        }
        @Override
         public void run() {
            while (true) {
                for (int i = 0; i < MAX; i++) {
                    drawOffScreen.setColor(p[i].color); // RGB颜色
                    drawOffScreen.fillOval(p[i].x, p[i].y, SIZE, SIZE);
                    p[i].x += p[i].mx;
                    p[i].y += p[i].my;
                    int x = p[i].x;
                    int y = p[i].y;
                    int R = p[i].color.getRed(); // 提取颜色
                    int G = p[i].color.getGreen();
                    int B = p[i].color.getBlue();
                    while (true) {
                        if (R ==0 && G ==0 && B ==0 ) {
                            break;
                        }
                        R -= COLORLV; // 尾部颜色淡化
                        if (R <0 ) {
                            R =0 ;
                        }
                        G -= COLORLV;
                        if (G <0 ) {
                            G =0 ;
                        }
                        B -= COLORLV;
                        if (B < 0) {
                            B =0 ;
                        }
                        Color color = new Color(R, G, B);
                        x -= p[i].mx; // 覆盖尾部
                        y -= p[i].my;
                        drawOffScreen.setColor(color);
                        drawOffScreen.fillOval(x, y, SIZE, SIZE);
                    }
                    if (x > AppletWidth || y > AppletHeight) { // 流星飞出窗口,重置流星
                        p[i].reset();
                    }
                }
                repaint();
                try {
                    Thread.sleep(SLEEP);
                } catch (InterruptedException e) {
                }
            }
        }
    }
    class Meteor { // 流星类
        int x, y; // 流星的位置
        int mx, my; // 下落速度
        Color color; // 流星颜色
        Random r = new Random();
        public Meteor() {
            reset();
        }
        public void reset() {
            int rand = (int) (Math.random() *100 ); //随机生成流星出现位置
            if (rand >35 ) {
                x = (int) (Math.random() *600 );
                y = 0;
            } else {
                y = (int) (Math.random() * 150);
                x =0 ;
            }
            mx = r.nextInt(2)+2; //随机生成下落速度和角度
            my = 1;
            color = new Color(
                        // 随机颜色
                    (new Double(Math.random() *128 )).intValue() +128 ,
                        (new Double(Math.random() *128 )).intValue() +128 ,
                        (new Double(Math.random() * 128)).intValue() + 128);
        }
    }
}

Player here The class needs to import the package by itself. I put the package in this link:

Link Extraction code: v22q

Notes

Some people may have this problem during the package import process Question:

How to use Java to realize romantic meteor confession?

It should actually be open:

How to use Java to realize romantic meteor confession?

How to import the package:

First copy and paste the package into the project package

Then enter: File –> Project Structure

How to use Java to realize romantic meteor confession?

Then click Libraries, Number, Java

How to use Java to realize romantic meteor confession?

#Find the location of the file you want to import, and then keep clicking OK.

The above is the detailed content of How to use Java to realize romantic meteor confession?. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:亿速云. If there is any infringement, please contact admin@php.cn delete
带你搞懂Java结构化数据处理开源库SPL带你搞懂Java结构化数据处理开源库SPLMay 24, 2022 pm 01:34 PM

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于结构化数据处理开源库SPL的相关问题,下面就一起来看一下java下理想的结构化数据处理类库,希望对大家有帮助。

Java集合框架之PriorityQueue优先级队列Java集合框架之PriorityQueue优先级队列Jun 09, 2022 am 11:47 AM

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于PriorityQueue优先级队列的相关知识,Java集合框架中提供了PriorityQueue和PriorityBlockingQueue两种类型的优先级队列,PriorityQueue是线程不安全的,PriorityBlockingQueue是线程安全的,下面一起来看一下,希望对大家有帮助。

完全掌握Java锁(图文解析)完全掌握Java锁(图文解析)Jun 14, 2022 am 11:47 AM

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于java锁的相关问题,包括了独占锁、悲观锁、乐观锁、共享锁等等内容,下面一起来看一下,希望对大家有帮助。

一起聊聊Java多线程之线程安全问题一起聊聊Java多线程之线程安全问题Apr 21, 2022 pm 06:17 PM

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于多线程的相关问题,包括了线程安装、线程加锁与线程不安全的原因、线程安全的标准类等等内容,希望对大家有帮助。

详细解析Java的this和super关键字详细解析Java的this和super关键字Apr 30, 2022 am 09:00 AM

本篇文章给大家带来了关于Java的相关知识,其中主要介绍了关于关键字中this和super的相关问题,以及他们的一些区别,下面一起来看一下,希望对大家有帮助。

Java基础归纳之枚举Java基础归纳之枚举May 26, 2022 am 11:50 AM

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于枚举的相关问题,包括了枚举的基本操作、集合类对枚举的支持等等内容,下面一起来看一下,希望对大家有帮助。

java中封装是什么java中封装是什么May 16, 2019 pm 06:08 PM

封装是一种信息隐藏技术,是指一种将抽象性函式接口的实现细节部分包装、隐藏起来的方法;封装可以被认为是一个保护屏障,防止指定类的代码和数据被外部类定义的代码随机访问。封装可以通过关键字private,protected和public实现。

归纳整理JAVA装饰器模式(实例详解)归纳整理JAVA装饰器模式(实例详解)May 05, 2022 pm 06:48 PM

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于设计模式的相关问题,主要将装饰器模式的相关内容,指在不改变现有对象结构的情况下,动态地给该对象增加一些职责的模式,希望对大家有帮助。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft