久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

Java實現浪漫流星表白的示例代碼

本文將利用Java語言實現浪漫流星表白,可以實現這些功能:播放音樂、自定義流星數量、飛行速度、光暈大小、流星大小,自定義表白話語,感興趣的可以學習一下

介紹

本文實現的功能有:

1、播放音樂

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

3、自定義表白話語 

運用到的知識點有:

GUI:java實現窗體、Swing。其實JAVA Swing的GUI目前企業中已經不用了,主要是一些學校和培訓機構用來教導學生寫一些游戲、小項目,練練手的。 

多線程:讓cpu同一時間處理多個任務(本文中涉及到音樂、文字緩慢出現、流星線條移動)

效果圖:

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

核心代碼

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

實現類: 

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類需要自己導包,包我放在這個鏈接里了:

鏈接  提取碼: v22q

注意事項

導包過程中可能有些人會出現這種問題:

實際應該是可以打開的: 

如何導包:

先要將包復制粘貼到項目包下

然后進入:File –> Project Structure

然后依次點擊Libraries、+號、Java

找到你要導的文件的位置,然后一直點OK就行了。

以上就是Java實現浪漫流星表白的示例代碼的詳細內容,更多關于Java流星的資料請關注html5模板網其它相關文章!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

主站蜘蛛池模板: 日韩午夜在线观看 | 国产精品久久a | 中文字幕一区二区视频 | 午夜影院在线观看 | 精品乱码一区二区三四区 | 国产精品欧美一区二区 | 久久久亚洲综合 | 久久久免费电影 | 雨宫琴音一区二区在线 | 日韩成人免费视频 | 超碰97免费观看 | 欧美日韩一区二区三区不卡视频 | 国产福利免费视频 | 成人三级视频 | 四虎在线观看 | 中文字幕免费观看 | 国产一区二区影院 | av影音资源| 亚洲精品乱码久久久久久按摩观 | 国产91久久精品一区二区 | 精品国产一区一区二区三亚瑟 | 亚洲精品中文在线观看 | 天天拍天天草 | 成人三级视频在线观看 | 不卡的av在线 | 一区二区三区欧美在线观看 | 亚洲国产精品一区 | 亚洲精品免费在线观看 | 国产综合久久久久久鬼色 | 国产久| 伊人久久国产 | 99精品国产一区二区三区 | 日韩久久久久 | 久久天堂 | 免费中文字幕日韩欧美 | 97伦理电影网 | 亚洲人成在线播放 | 中文字幕日韩一区二区 | 一区二区福利视频 | 亚洲自拍偷拍欧美 | 久久久久久久国产精品影院 |