溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

Java如何實(shí)現(xiàn)浪漫流星表白

發(fā)布時(shí)間:2022-05-20 11:23:10 來(lái)源:億速云 閱讀:378 作者:iii 欄目:開(kāi)發(fā)技術(shù)

本篇內(nèi)容主要講解“Java如何實(shí)現(xiàn)浪漫流星表白”,感興趣的朋友不妨來(lái)看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來(lái)帶大家學(xué)習(xí)“Java如何實(shí)現(xiàn)浪漫流星表白”吧!

介紹

本文實(shí)現(xiàn)的功能有:

1、播放音樂(lè)

2、自定義流星數(shù)量、飛行速度、光暈大小、流星大小

3、自定義表白話(huà)語(yǔ) 

運(yùn)用到的知識(shí)點(diǎn)有:

GUI:java實(shí)現(xiàn)窗體、Swing。其實(shí)JAVA Swing的GUI目前企業(yè)中已經(jīng)不用了,主要是一些學(xué)校和培訓(xùn)機(jī)構(gòu)用來(lái)教導(dǎo)學(xué)生寫(xiě)一些游戲、小項(xiàng)目,練練手的。 

多線(xiàn)程:讓cpu同一時(shí)間處理多個(gè)任務(wù)(本文中涉及到音樂(lè)、文字緩慢出現(xiàn)、流星線(xiàn)條移動(dòng))

效果圖:

Java如何實(shí)現(xiàn)浪漫流星表白

音樂(lè)類(lèi)(其實(shí)也可以不用音樂(lè),有些人并不喜歡): 

核心代碼

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() {
        //播放音樂(lè)
        System.out.println("開(kāi)始播放");
        //表示音樂(lè)文件
        File f = new File("nv.mp3");
        //第三方j(luò)ar包  Player類(lèi)
 
        try {
            Player p = new Player(new FileInputStream(f));//參數(shù):文件輸入流對(duì)象
            // p.play();
        } catch (FileNotFoundException | JavaLayerException e) {
            e.printStackTrace();
        }
 
    }
}

實(shí)現(xiàn)類(lèi): 

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; // (~)流星的個(gè)數(shù)
    final int SLEEP = 2; // 流星飛行的速度(數(shù)值越大,速度越慢)
    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); // 創(chuàng)建窗體
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
    }
    public static void main(String[] args) {
        new Thread(){
            @Override
            public void run() {
                //聲明一個(gè)File對(duì)象
                File mp3 = new File("nv.mp3");
                //創(chuàng)建一個(gè)輸入流
                FileInputStream fileInputStream = null;
 
                try {
                    fileInputStream = new FileInputStream(mp3);
                    //創(chuàng)建一個(gè)緩沖流
                    BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
                    //創(chuàng)建播放器對(duì)象,把文件的緩沖流傳入進(jìn)去
                    Player player = new Player(fileInputStream);
                    //調(diào)用播放方法進(jìn)行播放
                    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 { // 流星類(lèi)
        int x, y; // 流星的位置
        int mx, my; // 下落速度
        Color color; // 流星顏色
        Random r = new Random();
        public Meteor() {
            reset();
        }
        public void reset() {
            int rand = (int) (Math.random() *100 ); //隨機(jī)生成流星出現(xiàn)位置
            if (rand >35 ) {
                x = (int) (Math.random() *600 );
                y = 0;
            } else {
                y = (int) (Math.random() * 150);
                x =0 ;
            }
            mx = r.nextInt(2)+2; //隨機(jī)生成下落速度和角度
            my = 1;
            color = new Color(
                        // 隨機(jī)顏色
                    (new Double(Math.random() *128 )).intValue() +128 ,
                        (new Double(Math.random() *128 )).intValue() +128 ,
                        (new Double(Math.random() * 128)).intValue() + 128);
        }
    }
}

這里的Player類(lèi)需要自己導(dǎo)包,包我放在這個(gè)鏈接里了:

鏈接  提取碼: v22q

注意事項(xiàng)

導(dǎo)包過(guò)程中可能有些人會(huì)出現(xiàn)這種問(wèn)題:

Java如何實(shí)現(xiàn)浪漫流星表白

實(shí)際應(yīng)該是可以打開(kāi)的: 

Java如何實(shí)現(xiàn)浪漫流星表白

如何導(dǎo)包:

先要將包復(fù)制粘貼到項(xiàng)目包下

然后進(jìn)入:File &ndash;> Project Structure

Java如何實(shí)現(xiàn)浪漫流星表白

然后依次點(diǎn)擊Libraries、+號(hào)、Java

Java如何實(shí)現(xiàn)浪漫流星表白

找到你要導(dǎo)的文件的位置,然后一直點(diǎn)OK就行了。

到此,相信大家對(duì)“Java如何實(shí)現(xiàn)浪漫流星表白”有了更深的了解,不妨來(lái)實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢(xún),關(guān)注我們,繼續(xù)學(xué)習(xí)!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI